/** 
 * 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 = new Array();
Calendar.setup = function (params) {
	var now = (typeof BookingBuddy != 'undefined') ? new Date(BookingBuddy.StringsUncached.ServerTime) : new Date();
	
	function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };
	param_default("button",         null);
	param_default("eventName",      "click");
	param_default("align",          "Br");
	param_default("onSelect",       null);
	param_default("onClose",        null);
	param_default("onUpdate",       null);
	param_default("date",           now);
	param_default("position",       null);
	param_default("cache",          false);
	
	var target_date = now;
	target_date.setDate(1);
	target_date.setFullYear(target_date.getFullYear() + 1);
	param_default('maxDate', target_date);
	params.button = document.getElementById(params.button);
	
	function onSelect(cal) {
		var p = cal.params;
		if (cal.dateClicked && typeof p.onUpdate == "function") {
			p.onUpdate(cal);
		}
		if (cal.dateClicked && cal.dateClicked) {
			cal.callCloseHandler();
		}
	};

	var trigger = params.button;
	var handler_key = params.button.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;
			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;
		}
		
		cal.setDate(params.date);
		if (must_create) {
			cal.create();
		}
		if (!params.position) {
			cal.showAtElement(params.showAt || params.button, params.align);
		} 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;
	};
};
