/// <summary>Manage our search criteria callback data.</summary>
/******************************************************************************/
$(document).ready(function() {
	//Bind any callback elements to update the search and pull down new values.
	bindFilterEvents($("form#searchCriteriaForm"));	
	
	//Initialise our loading swf movie.
	var soLoader = new SWFObject('/assets/web/images/dataFinderElements/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)
{
	// 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 click status of the image selector.
	form.find("div.filter.imagelist fieldset > img").click(
		function () {
		
			var container = $(this).parent("fieldset.imagegroup");
			var image = $(this);
			if($(this).attr("class") == "selected")
			{
				//De-Select the clicked item.
				setImageGroupValue(container, stringAfterLast(image.attr("id"), "_"));
				image.removeClass("selected");
				image.attr("src", image.attr("src").replace("down","up"));
			}
			else
			{
				//Set the clicked item to selected.
				setImageGroupValue(container, stringAfterLast(image.attr("id"), "_"));
				image.addClass("selected");
				image.attr("src", image.attr("src").replace("up","down"));
			}
			
			//Now our hidden field has been set set-up our ajax request.
			var ctrl = container.children("input:hidden");
			var ctrlName = stringAfterLast(ctrl.attr("id"), "_");
			var ctrlValue = 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 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: "/layouts/web/datafinder/locator/GetCriteria.ashx",
		 data: ((args) ? args + "&" : "") + "sid=" + $("input#searchCriteriaSid").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 + " <span class='descriptor'>vehicles in stock</span>");
			$("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("There was a problem with your request.");
		 }
	 });
}
/******************************************************************************/


/******************************************************************************/
function setImageGroupValue(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;
	}	
}
/******************************************************************************/
