/// <summary>Manage our search criteria callback data.</summary>
/******************************************************************************/
$(document).ready(function() {
	//Bind any callback elements to update the search and pull down new values.
	if ($("form#searchCriteriaForm"))
	{
		bindFilterEvents($("form#searchCriteriaForm"));	
		
		//Initialise our loading swf movie.
		var soLoader = new SWFObject('/Arbury/assets/images/datafinder/loadingPanel.swf', 'loadingAnimation', '160', '95', '8', '#fff');
		soLoader.addParam("wmode", "transparent"); 
		//soLoader.addVariable("loadingText", "Updating Information");
		soLoader.write('searchCriteriaLoadingFlash');
		
		//Hide the pre-loader.
		$("form#searchCriteriaForm div#searchCriteriaLoading").hide();
	}
});
/******************************************************************************/


/******************************************************************************/
function bindFilterEvents(form)
{
	// Hide a bunch of stuff...if needed...	
	var advOptionsChosen = 0;
	//Are any dropdown selections made?
	form.find('fieldset:gt(0) > div.filter.dropdownlist > select > option').each(function(index) {		
		if (($(this).attr("selected") == true) && ($(this).val() != "")) advOptionsChosen++;
	});
	//Are any radio button selections made?
	form.find('fieldset:gt(0) > div.filter.radiobuttonlist > span').each(function(index) {
		$(this).find('input:gt(0)').each(function(index) {
			if ($(this).attr("checked") == true) advOptionsChosen++;	
		});
	});
	//Are any checkbox selections made?
	form.find('fieldset:gt(0) > div.filter.checkboxlist').each(function(index) {
		$(this).find('div:gt(0) > input').each(function(index) {
			if ($(this).attr("checked") == true) advOptionsChosen++;
		});
	});
	//if any selections have been made from the 'advanced' section then show these.
	(advOptionsChosen <= 0) ? form.find('fieldset:gt(0)').hide() : form.find('fieldset:gt(0)').show();

	// Handle the change status of a select drop down.
	form.find("div.filter.dropdownlist select").change(
		function() {
			//Make our ajax request to update the status.
			var ctrl = $(this);
			var ctrlName = stringAfterLast(ctrl.attr("id"), "_");
			var ctrlValue = ctrl.val();			
			ajaxGetResult(ctrlName + "=" + ctrlValue);
		}
	);

	// Handle the change status of a radio button.
	form.find("div.filter.radiobuttonlist input").click(
		function() {
			//Make our ajax request to update the status.
			var ctrl = $(this);
			var ctrlName = stringBeforeLast(ctrl.attr("id"), "_");			
			//Store reference to default 'all/any' option.
			var ctrlDefaultAll = ctrl.parent('span.radio').find("input:eq(1)");
			//If the selection is the default 'all' option, then pass back an empty string otherwise pass the required value.
			var ctrlValue;
			ctrlValue = (ctrl.attr("checked") == true) ? ((ctrl.attr("id") == ctrlDefaultAll.attr("id")) ? "" : ctrl.val()) : "";
			ajaxGetResult(ctrlName + "=" + ctrlValue);
		}
	);

	// Handle the change status of a checkbox list.
	form.find("div.filter.checkboxlist input").click(
		function() {
			//Make our ajax request to update the status.
			var ctrl = $(this);
			var ctrlName = stringBeforeLast(ctrl.attr("id"), "_");
			//Store reference to default 'all/any' option.
			var ctrlDefaultAll = ctrl.parent('div').parent('div.checkbox').find("input:eq(1)");			
			//If the selection is the default 'all' option, then pass back an empty string otherwise pass the required value.
			var ctrlValue;
			if (ctrl.attr("id") == ctrlDefaultAll.attr("id")) //If the selection is the default 'all' option, then pass back an empty string...
			{
				ctrlValue = "";
				ajaxGetResult(ctrlName + "=" + ctrlValue);
			}
			else
			{
				var container = ctrl.parent('div').parent('div.checkbox');
				setGroupValue(container, ctrl.val());
				ctrlValue = container.children("input:hidden").val();
				ajaxGetResult(ctrlName + "=" + ctrlValue);
			}
			//ctrlValue = (ctrl.attr("checked") == true) ? ((ctrl.attr("id") == ctrlDefaultAll.attr("id")) ? "" : ctrl.val()) : "";
			ajaxGetResult(ctrlName + "=" + ctrlValue);			
		}
	);
	
	//Handle our form submit for getting results.
	form.find("input#searchCriteriaSubmit").click(
		function() {
			//Show our loading movie while we are getting our results.
			$("form#searchCriteriaForm div#searchCriteria").hide();
			//$("#loadingAnimation").sendEvent("loadingText", "Getting Results");
			$("form#searchCriteriaForm div#searchCriteriaLoading").show();
		}
	);
}
/******************************************************************************/

/******************************************************************************/
function stringBeforeLast(data, character)
{
	if (data.lastIndexOf(character) > 0)
	{
		var tmpData = data.substring(0, data.lastIndexOf(character));
		tmpData = tmpData.substring(tmpData.lastIndexOf(character) + 1, tmpData.length);
		return tmpData;
	}
	else
	{
		return data;
	}	
}
/******************************************************************************/

/******************************************************************************/
function ajaxGetResult(args)
{
	//Show our loading movie while the ajax request is made.
	$("form#searchCriteriaForm div#searchCriteria").hide();
	$("form#searchCriteriaForm div#searchCriteriaLoading").show();
	//Set our stock count to nothing.
	$("h2#h2ResultCount").html("----");

	$.ajax({
		 type: "GET",
		 url: "/Arbury/layouts/locator/GetCriteria.ashx",
		 data: ((args) ? args + "&" : "") + "sid=" + $("input#searchCriteriaSid").val() + "&dfItm=" + $("input#searchCriteriaDataFinderItem").val(),
		 dataType: "json",
		 success: function(search) {
			//Now we have the result, set our html and turn our loading sequence off.
			$("h2#h2ResultCount").html(search.ResultCount + " Vehicles");
			$("div#searchCriteria").html(search.SearchPanel);
			$("form#searchCriteriaForm div#searchCriteriaLoading").hide();
			$("form#searchCriteriaForm div#searchCriteria").show();
			//Bind any callback elements to update the search and pull down new values.
			bindFilterEvents($("form#searchCriteriaForm"));
		 },
		 error: function() {
			 alert("SearchCriteria : There was a problem with your request.");
		 }
	 });
}
/******************************************************************************/


/******************************************************************************/
function setGroupValue(container, toggleValue)
{
	//get our hidden field that stores the data.
	var hiddenField = container.children("input:hidden");
	//Get what format the hidden field data needs to be stored as.
	var valueFormat = stringAfterLast(container.attr("class"), " ");
	
	switch(valueFormat)
	{
		case "sum" :
			//Convert our value to an integer.
			toggleValue = parseInt(toggleValue);
			//If our value is undefined then set it to a string value.
			if(!hiddenField.attr("value"))
			{
				hiddenField.attr("value", toggleValue);
			}
			else
			{
				//Check if the bit has already been set and add or remove it accordingly.
				if((parseInt(hiddenField.attr("value")) & toggleValue) == toggleValue)
				{
					hiddenField.attr("value", parseInt(hiddenField.attr("value")) - toggleValue);
				}
				else
				{
					hiddenField.attr("value", parseInt(hiddenField.attr("value")) + toggleValue);
				}
			}
			//If we have no selected items clear the value as 0 is the equivelent of none selected.
			if(hiddenField.attr("value") == "0") hiddenField.attr("value", "");
			break;
			
		default :
			if(!hiddenField.attr("value"))
			{
				hiddenField.attr("value", toggleValue);
			}
			else
			{
				var valueArray = hiddenField.attr("value").split(",");
				
				if(jQuery.inArray(toggleValue, valueArray) > -1)
				{
					valueArray.splice(jQuery.inArray(toggleValue, valueArray),1);
					hiddenField.attr("value", valueArray.join(","));
				}
				else
				{
					valueArray.push(toggleValue)
					hiddenField.attr("value", valueArray.join(","));
				}
			}
			break;
	}	
}
/******************************************************************************/
