/**
 * EventSearchForm.js
 * -------------------
 * JavaScript library for working with the Eurostar Event Search Form
 * It uses a WoW Date Picker and standard form to submit date based event
 * searches to the fx=event.search page component
 * The EventSearchForm accepts the following arguments, some of which related to the
 * encapsulated DatePicker.js
 * 
 * 	formId:			The DOM id of the main form
 * 	clearableFields:	The JSON array of field ids and their default text that are clearable on submit
 				For example: clearableFields:{field1Id:"Enter Text",field2Id:"Enter Location"}
 * 	startDateHiddenFieldId: The DOM id of the hidden input field for the start Date 
 * 	endDateHiddenFieldId:	The DOM id of the hidden input field for the end Date
 * 	minDate:		The Date string of the minimum date (default is today) format is MM/DD/YYYY
 *	maxDate:		The Date string of the maximum date (default is today+2yrs) format is MM/DD/YYYY
 * 	startDate:		The start date (string) to be set in the Calendar - format is WoW style YYYYMMDD 
 *	endDate:		The end date (string) to be set in the Calendar - format is WoW style YYYYMMDD
 *	location:		The name of the location
 * 	category:		The name of the event category
 * 	startCalendarDiv:	The DOM id of the start calendar DIV
 * 	endCalendarDiv:		The DOM id of the end calendar DIV
 * 	startDateFieldId:	The DOM id of the start date inout text field
 * 	endDateFieldId:		The DOM id of the end date input text field 
 * 	startCalendarTitle:	The text associated with the start Calendar pop-up
 * 	endCalendarTitle:	The text associated with the end Calendar pop-up
 * 
 */
YAHOO.namespace("wow");
YAHOO.wow.EventSearchForm = function() {

	// Handy YUI Shortcuts
	var Event = YAHOO.util.Event;
	var Dom = YAHOO.util.Dom;

	//The DOM id of the event search form
	var formId = "advancedsearch_id";

	// The array of clearableFields
	var clearableFields = [];
	
	//The DatePicker
	var datePicker;
	
	//The location and category variables
	var loc;
	var category;

	//The DOM id values for hidden input fields
	var startDateHiddenFieldId = "start_date";
	var endDateHiddenFieldId = "end_date";
	
	//The (default) Dom id for the actual location input field
	var locFieldId = "locationInput";
	
	/**
	 * Sets the various class attributes and adds all the
	 * necessary handlers to our form
	 */
	var createForm = function(args) {
	
		//Initialise our EventSearchForm with required arguments
    	parseOptions(args);
    	
    	//Creates the DatePicker object using the supplied arguments
    	datePicker = YAHOO.wow.DatePicker.init(args);

		//Add the event Handlers to our objects
		addHandlers();
		
		//Set any supplied values (location category etc)
		setInitialValues();
	};

	/**
	 * Parse the options and set the start and end selection dates
	 */
	 var parseOptions = function(options) {
	 	
	 	//Set the various options
	 	if (options.formId) { formId = options.formId; }
	 	if (options.clearableFields) { buildClearableFieldList(options.clearableFields); }
	 	if (options.startDateHiddenFieldId) { startDateHiddenFieldId = options.startDateHiddenFieldId; }
	 	if (options.endDateHiddenFieldId) { endDateHiddenFieldId = options.endDateHiddenFieldId; }	 	
	 	if (options.category) { category = options.category; }
	 	if (options.location) { loc = options.location; }
	 		 	
	 };
	 
	 /**
	  * Adds the submit handler for our event search form
	  */
	  var addHandlers = function() {
	  	
	  	//The handle submit function basically updates the hidden form values with the
	  	// current calendar values and then submits the form
		var handleSubmit = function(e, obj) {
			
			//Clear the contents of any clearables like Keyword or Location fields
			checkClearableFields();
			
			//Force an update of the Calendar objects as we may be submitting
			datePicker.updateCalendars();
			
			//Update the hidden field values
			Dom.get(startDateHiddenFieldId).value = datePicker.getDateStrings()[0];
			Dom.get(endDateHiddenFieldId).value = datePicker.getDateStrings()[1];
			
			//Now we can submit the event search form			
			this.submit();
		};		
	  	
	 	//Now we can wire up the submit listener
	 	Event.addListener(formId, "submit", handleSubmit);
	 };

	 /**
	  * Sets the input field values to any supplied defaults (location, category) etc 
	  */
	  var setInitialValues = function() {
	  	
	  	// loc search field
	  	if (loc) {
	  		var f = Dom.get(locFieldId)
	  		f.value = loc;
	  	}
	  }	  	
	 /**
	  * Using the supplied JSON array build the array of input fields and their default values
	  */	 
	 var buildClearableFieldList = function(fieldList) {
	 	
	 	for (var i=0; i<fieldList.length; i++) {
	 		var field = Dom.get(fieldList[i].id);
	 		if (field) {
	 			
	 			//Set the wowDefaultValue
	 			field.wowDefault = fieldList[i].value;
	 			
				//Add the clear field listener for each focus event and the form submit method	 		 	
			 	Event.addListener(field, "focus", textboxFocusHandler);
			 	Event.addListener(field, "blur", textboxBlurHandler);
			 	
			 	//Add it to the array
		 		clearableFields.push(field);	 			
	 		}
	 	}
	 };
	 
	 /**
	  * Clears the contents of the keyword field
	  * if it contains the default text
	  */
	 var checkClearableFields = function() {
	  	
	  	for (var i=0; i<clearableFields.length; i++) {
	  		var field = clearableFields[i];
		  	if (field.value == field.wowDefault) {
		  		field.value = "";
		  	}
	  	}
	  };
	  
	 /**
	  * The standard wow textBox blur handler that sets the field back to its default value
	  */
	 var textboxBlurHandler = function() {
	  	var val = this.wowDefault;
	  	if (val && this.value=="") this.value=val;
	 };
	  
	 /**
	  * Standard wow focus handler for text-boxes with defualt values
	  */
	 var textboxFocusHandler = function() {
	  	var val = this.wowDefault;
	  	if (this.value==val) this.value="";
	  	else this.select();
	 };

	/**
     * Initialiser for our EventSearchForm widget
     * which requires and argument object to be supplied
     */
    return {

		// Initialise our EventSearchForm with the necessary arguments
        init: function(args) {
        	createForm(args);
        }
    };
}();