// functions to be used in conjunction with Yahoo UI compontents
// jase@jaseroberts.net, 2/7/08

// deal with a select
// arguments:
// event_type: of event passed (click, etc.)
// args_array: any args coming from the event
// config_object: an arbitrary object passed for config purposes
function handleCalSelect(event_type,args_array,config_object) {
	var dates = args_array[0]; 
	var date = dates[0]; 
	var year = date[0], month = date[1], day = date[2]; 
	// get a handle to the form field which we are writing the date to
	date_form_field = document.getElementById(config_object.field_id);
	date_form_field.value = month + "/" + day + "/" + year;

	// close the calendar
	var calendar_object = window[config_object.Y_calendar_object];
	calendar_object.hide();
} // end function

// funtion called when the user clicks the trigger that pops the calendar up
function handleCalTriggerClick(event_type,config_object) {
	// get a handle on the appropriate calendar object
	var calendar_object = window[config_object.Y_calendar_object];

	// get the actual form field object
	var date_form_field = document.getElementById(config_object.field_id);

	// set the value from the text field, if there is one
	if (date_form_field.value != "") {
		// select the date from the form field
		calendar_object.select(date_form_field.value);
		// now use the selected dates to pick the appropriate page(month) to display
		var selectedDates = calendar_object.getSelectedDates();
		if (selectedDates.length > 0) {
			var firstDate = selectedDates[0];
			calendar_object.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
			calendar_object.render();
		} // end if dates set
	} // end if form field populated
	// show the calendar
	calendar_object.show();
} // end function

// generate a yahoo calendar
// calendar_config_object = the Yahoo-defined configuration object that can be passed as a parameter when creating a new calendar
// behavior_config_object = an object we create to specify ids of objects we'll create/interact with and any other config options unique to each calendar
function addCalendar(calendar_config_object, behavior_config_object) {
	// create a new calendar using the config options passed
	window[behavior_config_object.Y_calendar_object] = new YAHOO.widget.Calendar(behavior_config_object.Y_calendar_object, behavior_config_object.container_id, calendar_config_object);
	// get a handle on the calendar object
	var calendar_object = window[behavior_config_object.Y_calendar_object];
	// add a title
	calendar_object.cfg.setProperty('title', behavior_config_object.calendar_title);
	// generate the calendar
	calendar_object.render();
	// register a function to be called when a date is selected on the calendar
	calendar_object.selectEvent.subscribe(handleCalSelect, behavior_config_object, true);
	// register a listener on the element that will be clicked to pop-up the calendar
	YAHOO.util.Event.addListener(behavior_config_object.trigger_id, "click", handleCalTriggerClick, behavior_config_object, true);
	// register a listener on the actual field that will be populated by the calendar
	YAHOO.util.Event.addListener(behavior_config_object.field_id, "focus", calendar_object.show, calendar_object, true);
	
	// return a reference to the calendar object
	return calendar_object;
} // end function