/* +------------------------------------------------------+
 * |                      pPPa                            |
 * |                    pPPAPPp                           |
 * |                   APP  PPa                           |
 * |                  APA  pPP  PapA  PapA                |
 * |                 PPA   APA pP  P pP  P                |
 * |             APPPPPPPA PPp Ap Ap Ap Ap                |
 * |             apPPA    aPP  P     P                    |
 * |              APA     pPP  p     p                    |
 * |             pPP      PPA                             |
 * |             PPp      PPPp                            |
 * |                                                      |
 * | Created by:    App Software                          |
 * | Email:         info@appsoftware.nl                   |
 * | Web:           http://www.appsoftware.nl/            |
 * |                                                      |
 * +------------------------------------------------------+
 * 
 * CMS Main
 * 
 * 
 */

var BART = BART || {};

BART.Calender = {};

BART.Calender = function() {

	var select_callback;
	var cal_config;
	return {
		
		/**
		 * 
		 */
		init: function(config) {
			
			cal_config = config;
			
			if (config.noweeknr) {
				$('.agenda_date').addClass('noweeknr');
			}
			
			if (config.dayname_length) {
				$('.day_type > div').each(function(item){
					if ($(this).html() != '&nbsp;') {
						if ($(this).html().length > parseInt(config.dayname_length)) {
							$(this).html($(this).html().substring(0, parseInt(config.dayname_length)));
						}
					}
				});
			}
			
			if (config.selectCallBack) {
				select_callback = config.selectCallBack;
			}
		},
		/**
		 * 
		 */
		nextMonth: function() {
			this.changeMonth('next');
		},	
		/**
		 * 
		 */
		prevMonth: function() {
			this.changeMonth('prev');
		},	
		/**
		 * 
		 * @param {Object} action
		 * @param {Object} url
		 */
		changeMonth: function(action, url) 
		{
			if (action == 'next') {
				this.getNextMonth();
				var cal_action = 'date';
			} else if (action == 'prev') {
				this.getPrevMonth();
				var cal_action = 'date';
			}
			
			var d = new Date();
			var curr_month = (d.getMonth()+1);

			if (curr_month != $('#cal_month').val()) {
				$('#cal_active_day').val('day1');
			} else {
				$('#cal_active_day').val('day'+d.getDate());
			}

			if (cal_config.reloadCallback) {
				cal_config.reloadCallback.call(this, $('#cal_active_month_pos').val());
			} else {
				window.location = '?id='+ $('#cal_page_id').val() + 				
					'&'+cal_action+'='+$('#cal_active_date').val()+
					($('#cal_active_month_pos').val() != '' ? '&fm='+$('#cal_active_month_pos').val() : '');
			}
		},
		/**
		 * 
		 */
		refreshAgenda: function(url, action) 
		{
			if (action == null) {
				var action 	= 'date';
			}
			
			window.location = '?id='+ $('#cal_detail_id').val() + 				
				'&'+action+'='+$('#cal_active_date').val()+
				($('#cal_active_month_pos').val() != '' ? '&fm='+$('#cal_active_month_pos').val() : '');
		},
		/**
		 * 
		 * 
		 */
		gotoMonth: function(month) {
			$('#cal_active_month_pos').val( month );

			this.refreshAgenda();
		},
		/**
		 * 
		 * @param {Object} new_day
		 * @param {Object} url
		 * @param {Object} special
		 */
		selectDay: function(obj, url, special) 
		{
			var new_date = $(obj).parent().attr('id').replace('day', '');
			
			if (new_date != null && new_date != undefined) {
				this.clearAll();
				
				if (special != undefined) {
					if (special == 'extra_begin') {
						this.getPrevMonth();
					}
					else 
						if (special == 'extra_end') {
							this.getNextMonth();
						}
				}
				
				$('#day' + new_date).addClass('current');
				
				
				
				$('#cal_active_date').val(new_date);
				
				if (select_callback) {
					select_callback.call(this, {
						day: $('#cal_active_date').val().replace('day', ''),
						month: $('#cal_month').val(),
						year: $('#cal_year').val()
					});
				}
				else {
					this.refreshAgenda(url, 'date');
				}
			}
		},
		/**
		 * 
		 */
		selectWeek: function(week, url) 
		{
			this.clearAll();
		
			$('#cal_week'+week).addClass('current_week');			
			$('#cal_week').val(week);
				
			this.redirectAgenda(url, 3)
		},
		/**
		 * 
		 */
		selectMonth: function(month, url) 
		{
			clearAll();
		
			$('#cal_month').val(month);
			$('#cal_week').val('');
			
			this.redirectAgenda(url, 4)
		},
		/**
		 * 
		 */
		clearAll: function()
		{
			//Clear currently selected day
			var old_td 			= document.getElementById( $('#cal_active_day').val() );
		
			if (old_td != null) {
				old_td.className 	= old_td.className.replace(' current', '');
			}
		
			//Clear currently selected week

			var old_week = document.getElementById('week' + $('#cal_week').val());
		
			if (old_week != null) {
				old_week.className 	= old_week.className.replace(' current_week', '');
			}
		},
		/**
		 * 
		 */
		getNextMonth: function()
		{
			var date_parts = $('#cal_active_date').val().split('-');
			
			if (date_parts) {
				var new_month = parseInt($('#cal_active_month_pos').val()) + 1;
				var corrected_month = (new_month <= 12 ? new_month : 1);
				
				$('#cal_active_month_pos').val( corrected_month );

				$('#cal_active_date').val(date_parts[0]+'-'+corrected_month+'-'+(corrected_month != new_month ? (parseInt(date_parts[2])+1) : date_parts[2]));
			}
		},
		/**
		 * 
		 */
		getPrevMonth: function()
		{
			var date_parts = $('#cal_active_date').val().split('-');
			
			if (date_parts) {
				var new_month = parseInt($('#cal_active_month_pos').val()) - 1;
				var corrected_month = (new_month > 0 ? new_month : 12);
				
				$('#cal_active_month_pos').val( corrected_month );

				$('#cal_active_date').val(date_parts[0]+'-'+corrected_month+'-'+(corrected_month != new_month ? (parseInt(date_parts[2])-1) : date_parts[2]));
			}
		},
		/**
		 * 
		 */
		getMonth: function(int_id) {
			var months = new Array('januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december');

			return (months[(int_id-1)] ? months[(int_id-1)] : '');
		}
	};   
}();