window.status = "The Camping and Caravanning Club - Destination Guides";

//search help hover text
var WOWdatefromHelp = "<b>Date from:</b><br /><br /><div style=\'text-align:left;\'>You can narrow your search for an event by selecting a start date using the calendar to the right.</div>";
var WOWdatetoHelp = "<b>Date to:</b><br /><br /><div style=\'text-align:left;\'>You can narrow your search for an event by selecting an end date using the calendar to the right.</div>";
var WOWlocationHelp = "<b>Location:</b><br /><br /><div style=\'text-align:left;\'>Enter the first few letters of the name of a nearby town or city to find events by location, town names will automatically appear in a drop down list for selection.</div>";
var WOWradiusHelp = "<b>Radius:</b><br /><br /><div style=\'text-align:left;\'>Choose a radius from the drop down selection to find events within a certain radius of your location.</div>";
var WOWeventtypeHelp = "<b>Event type:</b><br /><br /><div style=\'text-align:left;\'>If you are looking for a particular type of event choose one from this drop down list.</div>";
var WOWkeywordHelp = "<b>Keyword:</b><br /><br /><div style=\'text-align:left;\'>If you have a particular event you are looking for e.g. Highland Games you can enter keywords in this box. </div>";

$(document).ready(function() { 

	// Table sorting
	$("table") 
	.tablesorter({widthFixed: true, widgets: ['zebra']}) 
	.tablesorterPager({container: $("#pager")}); 

	// Clear search box when clicked
	$('#searchinputbox').focus(function(){
		if(this.value=='Event keyword (s)'){this.value='';}
	});

	// Initialise the (YAHOO) Start and End Date Calendars
	YAHOO.wow.EventSearchForm.init({
		startCalendarTitle:"Start Date",
		endCalendarTitle:"End Date",
		startDate:"$val0",
		endDate:"$val1",
		clearableFields:[
			{id:"keywordInput2", value:"Enter keyword"},
			{id:"locationInput", value:"Enter location"}
		]
	});

	// Set up the location auto completer Data source
	YAHOO.example.ItemSelectHandler = function() {
		// Use a LocalDataSource
		var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.accounts);
		oDS.responseSchema = {fields : ["name", "id"]};

		// Instantiate the AutoComplete
		var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
		oAC.resultTypeList = false;
		
		// Define an event handler to populate a hidden form field
		// when an item gets selected
		var myHiddenField = YAHOO.util.Dom.get("myHidden");
		var myHandler = function(sType, aArgs) {
			var myAC = aArgs[0]; // reference back to the AC instance
			var elLI = aArgs[1]; // reference to the selected LI element
			var oData = aArgs[2]; // object literal of selected item's result data
			
			// update hidden form field with the selected item's ID
			myHiddenField.value = oData.id;
		};
		oAC.itemSelectEvent.subscribe(myHandler);

		return {
			oDS: oDS,
			oAC: oAC
		};
	}();	
	
	// Keep the previous search params if supplied in query string
	var search_val = $.query.get('search');
	var start_val = $.query.get('display_start_date');
	var end_val = $.query.get('display_end_date');
	var loc_id = $.query.get('loc_id');
	var cat_id = $.query.get('cat_id');
	var radius = $.query.get('radius');
	
	// If previous search params exist, then use the values to populate form
	if (search_val && search_val.length > 0) $('#searchinputbox').val(search_val);
	if (start_val && start_val.length > 0) $('#display_start_date').val(start_val);
	if (end_val && end_val.length > 0) $('#display_end_date').val(end_val);
	if (cat_id && cat_id > 0) $('#cat_id').val(cat_id);
	if (radius && radius > 0) $('#radius').val(radius);
	
	// Need to set the AutoComplete field as well
	if (loc_id && loc_id > 0) {
		YAHOO.example.ItemSelectHandler.oDS.sendRequest(null, {
			success:function(req, res, pay) {
				for (var i=0; i<res.results.length; i++) {
					if (res.results[i].id == loc_id) {
						$('#myInput').val(res.results[i].name);
						$('#myHidden').val(res.results[i].id);
					}
				}
			}
		});
	}

	// Clear 'default text from search input field
	validateFormOnSubmit = function(form) {
		if ( $('#searchinputbox').val() == 'Event keyword (s)') {
			$('#searchinputbox').val('');
		}
		return true;
	}
});
