/*!
 * MultiDatesPicker v1.2
 * http://dubrox.blogspot.com/2010/09/multiple-dates-picker-for-jquery-ui.html
 *
 * Copyright 2010, Luca Lauretta
 * Dual licensed under the MIT or GPL Version 2 licenses.
 */
(function( $ ){
	$.fn.multiDatesPicker = function(method) {
		var mdp_arguments = arguments;
		var ret = this;
		var today_date = new Date();
		
		function datesArraySwitch(type){ return type == 'avoided' ? 'avoidedDates' : 'selectedDates'};
		function removeDate(index, type) { this.multiDatesPicker[datesArraySwitch(type)].splice(index, 1); }
		function addDate(date, type) {
			if(methods.gotDate.call(this, date, type) === false)
				this.multiDatesPicker[datesArraySwitch(type)].push(dateConvert(date, 'object')); 
		}
		function dateConvert(date, desired_type) {
			switch(typeof date) {
				case 'object':
					if (desired_type != 'string') return date;
					else return $.datepicker.formatDate($.datepicker._defaults.dateFormat, date);
				case 'string':
					if (desired_type != 'string') return $.datepicker.parseDate($.datepicker._defaults.dateFormat, date);
					else return date;
				default: 
					$.error('Date format '+ typeof date +' not allowed on jQuery.multiDatesPicker');
			}
		}
		
		var methods = {
			init : function( options ) {
				$this = $(this);
				
				if(options) {
					this.multiDatesPicker.originalOnSelect = options.onSelect;
					this.multiDatesPicker.originalBeforeShowDay = options.beforeShowDay;
					
					this.multiDatesPicker.minDate = $.datepicker._determineDate(this, options.minDate, today_date);
					this.multiDatesPicker.firstAvailableDay = methods.compareDates(this.multiDatesPicker.minDate, today_date);
					
					if(options.addDates) methods.addDates.call(this, options.addDates);
					if(options.addAvoidedDates) methods.addDates.call(this, options.addAvoidedDates, 'avoided');
					
					if(options.mode) methods.setMode.call(this, options.mode);
				}
				
				$this.datepicker(options);
				
				options = {
					onSelect : function(dateText, inst) {
						$(this).multiDatesPicker('toggleDate', dateText);
						if (this.multiDatesPicker.mode.modeName == 'normal') {
							var dates_picked = $(this).multiDatesPicker('getDates');
							var datos = this.multiDatesPicker.mode.options;
							if (datos.pickableRange) {
								var first_date_delay = (dates_picked.length) ? methods.compareDates(dates_picked[0], this.multiDatesPicker.minDate) : datos.pickableRangeDelay;
								var appliable_delay = (dates_picked.length && first_date_delay < datos.pickableRangeDelay)
									? first_date_delay
									: datos.pickableRangeDelay; // + this.multiDatesPicker.firstAvailableDay;
								$(this).datepicker("option", "maxDate", appliable_delay + datos.pickableRange); // todo: perfeccionar la logica
							} 
						}
			
						if(this.multiDatesPicker.originalOnSelect) this.multiDatesPicker.originalOnSelect.call(this, dateText, inst);
					},
					beforeShowDay : function(date) {
						var highlight_class = ($(this).multiDatesPicker('gotDate', date) !== false)
							? 'ui-state-highlight'
							: '';
						var selectable_date = (
							$(this).multiDatesPicker('gotDate', date, 'avoided') !== false || 
								((this.multiDatesPicker.mode.options.maxPicks == $(this).multiDatesPicker('getDates').length)
									&& highlight_class == ''))
								? false
								: true;
						return [selectable_date, highlight_class];
					}
				};
				
				$this.datepicker('option', options);
			},
			compareDates : function(date1, date2) {
				var i_dates = [date1, date2];
				var o_dates = new Array();
				var one_day = 1000*60*60*24;
				
				for(i in i_dates) o_dates.push(dateConvert(i_dates[i], 'object'));
				
				// return > 0 means date1 is later than date2 
				// return == 0 means date1 is the same day as date2 
				// return < 0 means date1 is earlier than date2 
				return Math.ceil( (o_dates[0].getTime() - o_dates[1].getTime()) / one_day);
			},
			sumDays : function( date, n_days ) {
				var origDateType = typeof date;
				obj_date = dateConvert(date, 'object');
				obj_date.setDate(obj_date.getDate() + n_days);
				return dateConvert(obj_date, origDateType);
			},
			gotDate : function( date, type ) {
				for(var i = 0; i < this.multiDatesPicker[datesArraySwitch(type)].length; i++) {
					if(methods.compareDates(this.multiDatesPicker[datesArraySwitch(type)][i], date) == 0) {
						return i;
					}
				}
				return false;
			},
			getDates : function( format, type ) {
				switch (format) {
					case 'object':
						return this.multiDatesPicker[datesArraySwitch(type)];
					default:
						var o_dates = new Array();
						for(i in this.multiDatesPicker[datesArraySwitch(type)]) o_dates.push(dateConvert(this.multiDatesPicker[datesArraySwitch(type)][i], 'string'));
						return o_dates;
				}
			},
			addDates : function( dates, type ) {
				if(typeof dates != 'string')
					for(i in dates) addDate.call(this, dates[i], type);
				else
					addDate.call(this, dates, type);
			},
			removeDates : function( indexes, type ) {
				if(typeof index == 'array')
					for(i in indexes) removeDate.call(this, i, type);
				else
					removeDate.call(this, indexes, type);
			},
			toggleDate : function( date, type ) {
				var index = methods.gotDate.call(this, date);
				var mode = this.multiDatesPicker.mode;
				
				switch(mode.modeName) {
					case 'daysRange':
						this.multiDatesPicker.selectedDates = []; // deletes all selected dates
						var end = mode.options.autoselectRange[1];
						var begin = mode.options.autoselectRange[0];
						// todo: check if end is greater than begin to avoid ethernal loops
						for(var i = begin; i < end; i++) 
							methods.addDates.call(this, methods.sumDays(date, i));
						break;
					default:
						if(index === false) // adds dates
							methods.addDates.call(this, date);
						else // removes dates
							methods.removeDates.call(this, index);
						break;
				}
			}, 
			setMode : function( mode ) {
				this.multiDatesPicker.mode.modeName = mode.modeName;
				switch(mode.modeName) {
					case 'normal':
						for (option in mode.options)
							switch(option) {
								case 'maxPicks':
								case 'minPicks':
								case 'pickableRange':
								case 'pickableRangeDelay':
									this.multiDatesPicker.mode.options[option] = mode.options[option];
									break;
								default: $.error('Option ' + option + ' does not exist for setMode on jQuery.multiDatesPicker');
							}
					break;
					case 'daysRange':
					case 'weeksRange':
						var mandatory = 1;
						for (option in mode.options)
							switch(option) {
								case 'autoselectRange':
									mandatory--;
								case 'pickableRange':
								case 'pickableRangeDelay':
									this.multiDatesPicker.mode.options[option] = mode.options[option];
									break;
								default: $.error('Option ' + option + ' does not exist for setMode on jQuery.multiDatesPicker');
							}
						if(mandatory > 0) $.error('Some mandatory options not specified!');
					break;
				}
				
				if(mode.options.pickableRange) {
					$(this).datepicker("option", "maxDate", mode.options.pickableRange + (mode.options.pickableRangeDelay || 0));
				}
			}
		};
		
		this.each(function() {
			if (!this.multiDatesPicker) 
				this.multiDatesPicker = {
					selectedDates: [],
					avoidedDates: [],
					mode: {
						modeName: 'normal',
						options: {}
					}
				};
			
			if(methods[method]) {
				var exec_result = methods[method].apply(this, Array.prototype.slice.call(mdp_arguments, 1));
				switch(method) {
					case 'getDates':
					case 'gotDate':
						ret = exec_result;
				}
				return exec_result;
			} else if( typeof method === 'object' || ! method ) {
				return methods.init.apply(this, mdp_arguments);
			} else {
				$.error('Method ' +  method + ' does not exist on jQuery.multiDatesPicker');
			}
			
		});
		
		return ret;
	};
})( jQuery );
