/*
* File: product_search_template_functions.js
* Author: Brian Schemp
* 
* Purpose:	Configure events in the product search bar. Set the product search form for entering in a product number or keyword.  Also set the 
*			vehicle and vehicle year pull-downs in the product search bar.
*
* History: 10/21/2009 Brian - Created.
*		
*
*/

//global variable to store the productYear object.
var productYearObj = null;

//called when the page is ready
$(document).ready(function() {
	//get values from cookie.
	var cookieCatalogId = ($.cookie("CATALOGID") == undefined) ? 0 : $.cookie("CATALOGID");
	var cookieVehicleModelId = ($.cookie("VEHICLEMODELID") == undefined) ? 0 : $.cookie("VEHICLEMODELID");
	var cookieYearId = ($.cookie("YEARID") == undefined) ? 0 : $.cookie("YEARID");
	
	//set the vehicle and year pull-downs.
	getCatalogs(cookieCatalogId);
	getCatalogYears(cookieCatalogId, cookieYearId);
	if (cookieCatalogId == 12) {
		getVehicleModels(cookieCatalogId, cookieVehicleModelId);
	} else {
		$("#moparId").hide();
		$("#yearId").show();
	}
	
	//set onChange event for the vehicle pull-down.
	$("#vehicleId").change(function(evt) {
		evt.preventDefault();
		var selectedVehicleId = $(this).val();		
		getCatalogYears(selectedVehicleId, 0);
		getVehicleModels(selectedVehicleId, cookieVehicleModelId);
	});
	
	//set the submit event for the product search form.
	$("#vehicleSearchForm").bind('submit', function(evt) {
		//prevent default submit event.
		evt.preventDefault();
		
		//get form values.
		//var keywordValue = $("#keyword").val();
		//var partNumberValue = $("#partNumber").val();
		var selectedCatalog = $("#vehicleId").val();
		var selectedCatalogYear = $("#yearId").val();
		var selectedVehicleModel = $("#moparId").val();
		var searchTermValue = $("#searchTerm").val();
		var outletCenterSearchPath = $.url.attr('path')
		
		var urlSearchRoot = '';
		var urlString =  '';
		if (searchTermValue != 0) {
			var urlString = urlString + '&q=' + searchTermValue;
		}
		if (selectedCatalogYear != 0) {
			var urlString = urlString + '&y=' + selectedCatalogYear;
		}
		if (selectedCatalog != 0) {
			var urlString = urlString + '&m=' + selectedCatalog;
		}
		if (selectedVehicleModel != 0) {
			var urlString = urlString + '&sm=' + selectedVehicleModel;
		}
		if (outletCenterSearchPath.indexOf('outlet') > 0) {
			if ($.url.attr('path').indexOf('parts') == -1 || $.url.attr('path').indexOf('parts') == 8) {
				urlSearchRoot = outletCenterSearchPath + "parts/search";
			} else {
				urlSearchRoot = outletCenterSearchPath + "search";
			}
		} else {
			urlSearchRoot = '/search';
		}
		
		
		if(searchTermValue.length == 0 || searchTermValue == 'Part Number or Keyword'){
			alert("Please enter a Keyword or Product Number");	
		} else {
			window.location = urlSearchRoot + urlString.replace(/[&]/,'?');			
		}
	});	
});

/*
* Get the active catalogs and load them into the vehicle pull-down.  Select the vehicle
* that was chosen.
*
* @param {Object} catalogId
*/
function getCatalogs(catalogId) {
	$.post(
		"/object/vehicle.cfc",
		{
			method: "getVehicleCatalogs",
			returnFormat: "json"
		},
		function(response){
			var catalogData = response.DATA;
			
			//cache the catalog pull-down.
			var catalogPulldown = $("#vehicleId");
			
			//remove all pull-down values.
			catalogPulldown.children().remove();
			
			//set default value.
			catalogPulldown.append('<option value="0">Select A Model</option>');
			
			//loop over the catalogs and set the pull-down options.
			$.each(catalogData, function(index, optionData) {
				var responseCatalogId = optionData[0];
				var catalogName = optionData[1];
				var catalogSESUrlName = optionData[3];
				// catId of 20 is Bel Air
				if (catalogName != "El Camino" && responseCatalogId != '20') {
					if (responseCatalogId == catalogId) {
						catalogPulldown.append('<option value="' + catalogSESUrlName + '" selected="selected">' + catalogName + '</option>');
					} else {
						catalogPulldown.append('<option value="' + catalogSESUrlName + '">' + catalogName + '</option>');
					}
				}
			});
		},
		"json"
	);	
}

/*
* Get the Vehicle Models for Mopar catalog and call a helper function to 
* load the data into the years pull-down.
*
* @param {Object} catalogId
*/
function getVehicleModels(catalog,vehicleModelId) {
	
	if (!parseInt(catalog) && catalog != 0) {
		myData = $.ajax({
			url: "/object/catalog.cfc?method=getCatalogIdFromName&catalogName=" + catalog,
			async: false
		}).responseText;
		catalogId = parseInt(myData);
	} else {
		catalogId = catalog;
	}
	
	if(catalogId == '12') {
		$("#moparId").show();
		$("#yearId").hide();
	} else {
		$("#moparId").hide();
		$("#yearId").show();
	}
	//set default value for the catalogId if it's zero.
	
	if(catalogId == 0) {
		catalogId = 1;
	}

	if(catalogId==12) {
		$.post(
			"/object/vehicle.cfc",
			{
				method: "getVehicleModels",
				catalogId: 	catalogId,
				returnFormat: "json"			
			},
			function(response) {
				var vehicleData = response.DATA;
				var vehiclePullDown = $("#moparId");
				//remove all pull-down values.
				vehiclePullDown.children().remove();
				//set default value.
				vehiclePullDown.append('<option value="0">All Mopar Models</option>');
				$.each(vehicleData, function(index, optionData) {			
					var responseVehicleModelId = optionData[0];
					var name = optionData[1];
						//cache the catalog pull-down.
					if (responseVehicleModelId == vehicleModelId) {
						vehiclePullDown.append('<option value="' + responseVehicleModelId + '" selected="selected">' + name + '</option>');
					} else {
						vehiclePullDown.append('<option value="' + responseVehicleModelId + '">' + name + '</option>');
					}
				});
			},
			"json"
		);	
	}
}

/*
* Get the catalog years for a specific catalog and call a helper function to 
* load the data into the years pull-down.
*
* @param {Object} catalogId
* @param {Object} catalogYearId
*/
function getCatalogYears(catalog, catalogYearId) {
	//set default value for the catalogId if it's zero.
	var mydata = 0;
	var catalogId = 0;
	if (!parseInt(catalog) && catalog != 0) {
		myData = $.ajax({
			url: "/object/catalog.cfc?method=getCatalogIdFromName&catalogName=" + catalog,
			async: false
		}).responseText;
		catalogId = parseInt(myData);
	} else {
		catalogId = catalog;
	} 
	
	if(catalogId == 0) {
		catalogId = 1;
	}
	
	$.post(
		"/object/vehicle.cfc",
		{
			method: "getAllVehicleYears",
			returnFormat: "json"			
		},
		function(response) {
			$.each(response, function(index, optionData) {							
				var responseCatalogId = optionData['catalogId'];
				
				if(responseCatalogId == catalogId) {
					productYearObj = optionData['years'];	
				}								
			});
			
			//call helper function.
			loadCatalogYears(catalogYearId);
		},
		"json"
	);	
}

/*
* Load the catalog years into the year pull-down.  Select the year that
* was chosen.
*
* @param {Object} catalogYearId
*/
function loadCatalogYears(catalogYearId){
	var productYearPulldown = $("#yearId");
	
	//remove all pull-down values.
	productYearPulldown.children().remove();
	
	//variable to store the product years array.
	var productYearArray = [];
	
	//loop over the object and create a year array.
	$.each(productYearObj, function(index, productYearData) {
		productYearArray.push({
			yearId: productYearData.yearId,
			year: productYearData.year
		});
	});
	
	//sort by year.
	productYearArray.sort(function(a,b){
		return a.year - b.year;							   
	});
			
	//add default value.
	productYearPulldown.append('<option value="0">All Years</option>');
	
	//add each value to the year pull-down.
	$.each(productYearArray, function(index, productYearData) {
		if(catalogYearId == productYearData.yearId) {
			productYearPulldown.append('<option value="' + productYearData.year + '" selected="selected">' + productYearData.year + '</option>');	
		} else {
			productYearPulldown.append('<option value="' + productYearData.year + '">' + productYearData.year + '</option>');	
		}
	});
}

/*
* Set the image rollovers.
*
*/
function setImageRollovers() {
	var startProductSearchImage = $("#startProductSearch");
	var startCategorySearchImage = $("#startProductCategorySearch");
	
	//set mouse-over and mouse-out events for 'Go' image.
	startProductSearchImage.hover(
		function(){
			swapImage(startProductSearchImage, "go_PUSH.jpg", "/images/HomePage/");
		},
		
		function(){
			swapImage(startProductSearchImage, "go.jpg", "/images/HomePage/");
		}		
	);
	
	//set mouse-over and mouse-out events for 'Search By Category' image.
	startCategorySearchImage.hover(
		function(){
			swapImage(startCategorySearchImage, "ORANGE_SEARCH_PUSH.jpg", "/images/HomePage/");
		},
		
		function(){
			swapImage(startCategorySearchImage, "ORANGE_SEARCH.jpg", "/images/HomePage/");
		}		
	);	
}

/*
* Called to open the multiple vehicle popup window. Set the cfwindow with it's initialization
* parameters and setup the function to be called when the user closes the popup window.
*
*/
function displayCategoryCatalogChoiceWindow(evt) {
	evt.preventDefault();
	var backgroundPopup = $("#backgroundPopup");		
	backgroundPopup.css({"opacity": "0.7"});  
	backgroundPopup.fadeIn("slow");		
	
	$("#dialog").load("/controller.cfm?type=product&action=getCatalogsForCategorySearch");
	$("#dialog").dialog({
		title: "Please Choose A Vehicle Line",
		resizable: false,
		close: function(evt, ui) {$("#backgroundPopup").fadeOut("slow");}
	});
}
