/** 
 * based on http://dynarch.com/mishoo/calendar.epl
 * This script is distributed under the GNU Lesser General Public License.
 * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
 */

/**
 *  The "params" is a single object that can have the following properties:
 *
 *    prop. name   | description
 *  -------------------------------------------------------------------------------------------------
 *   inputField    | the ID of an input field to store the date
 *   displayArea   | the ID of a DIV or other element to show the date
 *   button        | ID of a button or other element that will trigger the calendar
 *   eventName     | event that will trigger the calendar, without the "on" prefix (default: "click")
 *   singleClick   | (true/false) wether the calendar is in single click mode or not (default: true)
 *   align         | alignment (default: "Br"); if you don't know what's this see the calendar documentation
 *   onSelect      | function that gets called when a date is selected.  You don't _have_ to supply this (the default is generally okay)
 *   onClose       | function that gets called when the calendar is closed.  [default]
 *   onUpdate      | function that gets called after the date is updated in the input field.  Receives a reference to the calendar.
 *   date          | the date that the calendar will be initially displayed to
 *   position      | configures the calendar absolute position; default: null
 *   cache         | if "true" it will reuse the same calendar object, where possible
 */
Calendar.savedHandlers = [];
Calendar.setup = function (params) {
	var now = (typeof BookingBuddy != 'undefined') ? new Date(BookingBuddy.Strings.ServerTime) : new Date();
	function param_default(pname, def) { 
		if (typeof params[pname] == "undefined") { 
			params[pname] = def; 
		} 
	}
	param_default("input",         null);
	param_default("icon",         null);
	param_default("eventName",      "click");
	param_default("align",          "Br");
	param_default("offsets",        null);
	param_default("onSelect",       null);
	param_default("onClose",        null);
	param_default("onUpdate",       null);
	param_default("getDate",       null);
	param_default("date",           now);
	param_default("position",       null);
	param_default("cache",          false);
	param_default("numMonths",      2);
	
	var target_date = now;
	target_date.setDate(1);
	target_date.setFullYear(target_date.getFullYear() + 1);
	param_default('maxDate', target_date);
	params.input = $j("#"+params.input).get(0);
	if(params.input) {
		
		
		var trigger = params.input;
		var handler_key = params.input.id + '_' + 'on' + params.eventName;
		Calendar.savedHandlers[handler_key] = trigger['on' + params.eventName];
		trigger["on" + params.eventName] = function() {
			var must_create = false;
			var cal = window._sl_calendar;
			params.date = params.getDate();
			if (!(cal && params.cache)) {
				must_create = true;
				if (window._sl_calendar) {
					window._sl_calendar.destroy();
				}
				window._sl_calendar = cal = new Calendar(params.date,
													params.onSelect || onSelect,
													params.onClose || function(cal) { cal.hide(); });
				cal.setMaxDate(params.maxDate);
				cal.params = params;
			} else {
				cal.hide();
			}
			
			if(params.numMonths) {
				cal.numMonths = params.numMonths;
			}
			
			if(!params.date) {
				params.date = new Date();
			}
			
			if (must_create) {
				cal.create();
			}
			
			var _date = params.getDate();
			if($j.isNullOrUndefined(_date)) {
				_date = params.date;
			}
			cal.setDate(_date);
			
			if (!params.position) {
				cal.showAtElement(params.showAt || params.button, params.align, params.offsets);
			} else {
				cal.showAt(params.position[0], params.position[1]);
			}
			if (typeof Calendar.savedHandlers[handler_key] == 'function') {
				window.setTimeout('Calendar.savedHandlers["' + handler_key + '"]()', 10);
			}
			return false;
		};
		
		// check for a containing div if no image exists
		if (!$j('#'+params.icon).length) {
			params.icon = params.icon + '_div';
		}

		params.icon = $j("#"+params.icon).click( function(e) {
			$j(trigger).click();
		});
		$j(params.input).addClass("calendarUIInitialized");
	}
};
