/**
* File: productSearchSettingsForMCategorySearch.js
* Author: Brian Schemp
* 
* Purpose:	Set the product search form that will be used to refine the category search results.
*
*
* History: 	02/03/2010 Brian - Created.
*			05/15/2010 Brian - Added year pull-down onChange event.  Re-load the page when the user changes the model or year.
*
*
*/


//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");
	
	//build new SES url
	var urlEnd = $.url.attr("path").length-1;
	var sesUrlArray = $.url.attr("path").substring(1,urlEnd).split('/').slice(2);
	var sesUrl = sesUrlArray.toString().replace(/,/g,'/');
	if (sesUrl.length) {
		sesUrl = sesUrl + '/';
	}
	
	//set the vehicle and year pull-downs.
	getCatalogs(cookieCatalogId);
	getVehicleModels(cookieCatalogId,cookievehicleModelId);
	getCatalogYears(cookieCatalogId, cookieYearId);
	
	//set onChange event for the vehicle pull-down.
	$("#vehicleId").change(function(evt) {
		evt.preventDefault();
		var selectedVehicleId = $(this).val();
		//var selectedYearId = $("#yearId").val();
		//getCatalogYears(selectedVehicleId, 0);
		onChangeVehicleCategorySearch(selectedVehicleId, 0, 0, 0);
	});
	
	//set onChange event for the mopar pull-down.
	$("#moparId").change(function(evt) {
		evt.preventDefault();
		var selectedMoparId = $(this).val();
		var selectedYearId = $("#yearId").val();
		var selectedVehicleId = $("#vehicleId").val();
		
		onChangeVehicleCategorySearch(selectedVehicleId, selectedYearId, selectedMoparId, sesUrl);
	});		
	
	//set onChange event for the year pull-down.
	$("#yearId").change(function(evt) {
		evt.preventDefault();
		var selectedYearId = $("#yearId").val();
		var selectedVehicleId = $("#vehicleId").val();
		var selectedMoparId = $("#moparId").val();
		onChangeVehicleCategorySearch(selectedVehicleId, selectedYearId, selectedMoparId, sesUrl);
	});		
		
	/* TODO: */
		
	//set refine search submit event.
	$("#vehicleSearchForm").bind('submit', refineProductSearch);
});


/*
* Called when the vehicle pull-down has changed.
*
* @param {Object} catalogId
* @param {Object} yearId
* @param {Object} vehiclemodelId
*
*/
function onChangeVehicleCategorySearch(catalogName, yearId, vehiclemodelId, sesUrl) {
	//window.location = "/controller.cfm?type=product&action=displayProductCategories&productSearchCatalogId=" + catalogId + "&yearId=" + yearId + "&vehiclemodelId=" + vehiclemodelId;
	var urlString =  '';
	if (yearId != 0 && sesUrl != '0') {
		var urlString = urlString + '&y=' + yearId;
	}
	if (vehiclemodelId != 0 && sesUrl != '0') {
		var urlString = urlString + '&sm=' + vehiclemodelId;
	}
	
	if (sesUrl == '0') {sesUrl = '';}

	window.location = "/" + catalogName + "/parts/" + sesUrl +  urlString.replace(/[&]/,'?');  //controller.cfm?type=product&action=displayProductCategories&productSearchCatalogId=" +  + "&yearId=" + yearId + "&vehiclemodelId=" + vehiclemodelId;
}

/*
* Called when the vehicle pull-down has changed and a part number has been entered.
*
* @param {Object} catalogId
* @param {Object} yearId
* @param {Object} vehiclemodelId
* @param {Object} partNumber
*
*/
function onChangeVehicleCategorySearchPartNumber(catalogId, yearId, partNumber, vehiclemodelId) {
	window.location = "/controller.cfm?type=product&action=displayProductCategories&productSearchCatalogId=" + catalogId + "&yearId=" + yearId + "&vehiclemodelId=" + vehiclemodelId + "&partNumber=" + partNumber;
}

/*
* Called when the vehicle pull-down has changed and a keyword has been entered.
*
* @param {Object} catalogId
* @param {Object} yearId
* @param {Object} vehiclemodelId
* @param {Object} keyword
*
*/
function onChangeVehicleCategorySearchKeyword(catalogName, yearId, keyword,vehiclemodelId) {
	//window.location = "/controller.cfm?type=product&action=displayProductCategories&productSearchCatalogId=" + catalogId + "&yearId=" + yearId + "&vehiclemodelId=" + vehiclemodelId + "&keyword=" + keyword;
}

/*
* The year pull-down has been changed.  Products have been returned but the user would like to filter
* the results by year.
*
* @param {Object} catalogId
* @param {Object} yearId
* @param {Object} moparId
* @param {Object} categoryId
* @param {Object} subCategoryId
* @param {Object} subSubCategoryId
*
*/
function onChangeVehicleYearGetProducts(catalogId, yearId, categoryId, subCategoryId,subSubCategoryId,vehiclemodelId) {
	//window.location = "/controller.cfm?type=product&action=getProductsCategorySearch&productSearchCatalogId=" + catalogId + "&yearId=" + yearId + "&vehiclemodelId=" + vehiclemodelId + "&categoryId=" + categoryId + "&subCategoryId=" + subCategoryId + "&subSubCategoryId=" + subSubCategoryId;
}

/**
* 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();
					
			//loop over the catalogs and set the pull-down options.
			$.each(catalogData, function(index, optionData) {
				var responseCatalogId = optionData[0];
				var catalogName = optionData[1];
				// catId of 20 is Bel Air
				if (catalogName != "El Camino" && responseCatalogId != '20') {
					if (catalogName == 'GM Truck') {
						var catalogSEOName = 'truck';
					} else {
						var catalogSEOName = catalogName;
					}
					if (responseCatalogId == catalogId) {
						catalogPulldown.append('<option value="' + catalogSEOName.toLowerCase().replace(' ','-') + '" selected="selected">' + catalogName + '</option>');
					}
					
					else {
						catalogPulldown.append('<option value="' + catalogSEOName.toLowerCase().replace(' ','-') + '">' + catalogName + '</option>');
					}
				}
			});			
		},
		"json"
	);	
}
/**
* Get the Vehicle Models for Mopar catalog 
*
* @param {Object} catalogId
*/
function getVehicleModels(catalogId, vehicleModelId) {
	//set default value for the catalogId if it's zero.
	if(catalogId==12) {
		$("#moparId").show();
		$("#yearId").hide();
	}
	else {
		$("#moparId").hide();
		$("#yearId").show();
	}
	/*
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];
					
					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(catalogId, catalogYearId) {
	//set default value for the catalogId if it's zero.
	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"
	);	
}

function getCatalogVehicleYearId (catalogId, year) {
	
	
	
}

/**
* 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>');	
		}
	});
}

/**
* Called when the user would like to refine the search results.
*
* @param {Object} evt - event object.
*/
function refineProductSearch(evt) {
	evt.preventDefault();
	//get form values.
	var selectedVehicleId = $("#vehicleId").val();
	var selectedYearId = $("#yearId").val();
	var searchTermValue = $("#searchTerm").val();
	var searchMoparId = $("#moparId").val();
	
	//build new SES url
	var urlEnd = $.url.attr("path").length-1;
	var sesUrlArray = $.url.attr("path").substring(1,urlEnd).split('/');
	
	var sesUrl = sesUrlArray.toString().replace(',','/');
	if (sesUrl.length) {
		sesUrl = sesUrl + '/';
	}						

	if(searchTermValue.length > 0) {
		//get categories, sub-categories, sub-sub-categories filtered by keyword and year.
		window.location = "/" + sesUrlArray[0] + "/parts/?q=" + searchTermValue + "&y=" + selectedYearId + "&sm=" + searchMoparId; 
	} else {
		//get the categories and sub-categories filtered by the year.
		onChangeVehicleCategorySearch(selectedVehicleId, selectedYearId);
	}
}
