// Add the default callBack-handler:
Widget.addCallBack("DatePicker", function(params) {
	// Add the widget to the DatePicker handler:
	Widget.getClass("DatePicker").addWidget(params.widgetId);
});

/**
 * The date picker static class.
 * 
 * @version 2011-02-10
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=DatePicker script.js">R. ten Napel, ing.</a>
 **/
function DatePicker() {}

DatePicker.widgets = Array();
DatePicker.handlers = Array();

/**
 * Add a widget to the list of DatePicker widgets to be handled when a date is selected. If the ID is already
 * stored, it won't be added.
 * 
 * @param string widgetId			The widget ID of the widget to handle.
 * 
 * @return int						The index of the widget ID.
 **/
DatePicker.addWidget = function(widgetId) {
	// Retrieve the index of the given widget:
	var i = this.widgets.indexOf(widgetId);
	
	// If no index, add the widget and handler array:
	if (i == -1) {
		this.widgets.add(widgetId);
		this.handlers.add(Array());
		
		i = this.widgets.length - 1;
	}
	
	return i;
};

/**
 * Add a handler to the list. If a widget ID already exists, it'll be used.
 * 
 * @param string widgetId			The widget ID of the widget to handler.
 * @param function handler			The handler corresponding to the widget.
 **/
DatePicker.addHandler = function(widgetId, handler) {
	// Add the widget and retrieve it's index:
	var i = this.addWidget(widgetId);
	
	// If the widget exists add the handler:
	this.handlers[i].add(handler);
};

/**
 * Select a date, update this widget and all the widgets in the handler.
 * 
 * @param string widgetId			The ID of the DatePicker widget that triggers the selection.
 * @param string date				The date that is selected.
 * @param string update				The ID of the input element to update it's value.
 * @param string wrapper			The ID of the wrapper to toggle when selecting a value.
 **/
DatePicker.selectDate = function(widgetId, date, update, wrapper) {
	// Retrieve the right handlers which belong to the given widget:
	var i = this.widgets.indexOf(widgetId);
	
	// Add widget if not yet exists:
	if (i == -1) {
		i = this.addWidget(widgetId);
	}
	
	// Call each handler:
	var handlers = this.handlers[i];
	for (var j = 0; j < handlers.length; j++) {
		handlers[j].apply(this, new Array(date));
	}
	
	// Update the input element's value:
	if (update != null && update != "") {
		//Debugger.write("$('" + update + "').$().value = " + date);
		
		//alert(update);
		$(update).$().value = date;
	}
	
	// Toggle the wrapper element:
	//if (wrapper != null && wrapper != "") {
	//	$(wrapper).toggle();
	//}
	
	// Define the params-string for the widget URL:
	var params = "widgetId=" + widgetId + "&date=" + date + "&update=" + ((update != null) ? update : "") + "&wrapper=" + ((wrapper != null) ? wrapper : "");
	//Debugger.write(params);
	
	// Refresh the widget:
	new Widget($(widgetId).$().parentNode, "DatePicker", params).refresh(params);
};

//Store the DatePicker static JS class:
Widget.addClass("DatePicker", DatePicker);
