// JavaScript Document
if (typeof SophioAjaxCatalog === "undefined") {
	SophioAjaxCatalog = {};
}

SophioAjaxCatalog.catalogCrumb=function(){
var me=this;	
this.supplier="whd";
this.drillManager = new SophioAjaxCatalog.drillManager();
this.cApplicationName="defpage-parts";
this.cTitle="Shop by Vehicle";
this.cFormURL="ShopbyVehicle.epc?sup=WHD&mode=PA";
this.cDrillURL="ShopbyVehicle.epc?";
this.cSearchButtonCaption="Search";
this.cSearchButtonId="";
this.mainContainerId="";
this.lAutoLoadDrills=true;
this.lcreateSearchButton=true;
this.lVehicleOnly=false;
this.createFullMarkup=function() {
if (jQuery('#'+this.supplier+'-'+this.cApplicationName+'-lookup').length==0){return false;}
var cVehicleMarkup="";
if (this.supplier!=="unv")
{
	if (this.supplier==="oec")
	{
	cVehicleMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-catalog_lookup" class="ui-defpage-lookup"></div>'
	}
	cVehicleMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-year_lookup" class="ui-defpage-lookup"></div>\
			<div id="'+this.supplier+'-'+this.cApplicationName+'-make_lookup" class="ui-defpage-lookup"></div>\
			<div id="'+this.supplier+'-'+this.cApplicationName+'-model_lookup" class="ui-defpage-lookup"></div>';
	if (this.supplier==="oec") {		
			cVehicleMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-submodel_lookup" class="ui-defpage-lookup"></div>';
		} else {
			cVehicleMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-engine_lookup" class="ui-defpage-lookup"></div>';
	}
}

var cProdMarkup=""
if (!this.lVehicleOnly) {
	if (this.supplier==="oec") {
	cProdMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-section_lookup" class="ui-defpage-lookup"></div>'
	}
	cProdMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-group_lookup" class="ui-defpage-lookup"></div>\
		<div id="'+this.supplier+'-'+this.cApplicationName+'-subgroup_lookup" class="ui-defpage-lookup"></div>';
	if (this.supplier==="oec") {
		cProdMarkup+='<div id="'+this.supplier+'-'+this.cApplicationName+'-component_lookup" class="ui-defpage-lookup"></div>\
		<div id="'+this.supplier+'-'+this.cApplicationName+'-qualifier_lookup" class="ui-defpage-lookup"></div>';
	}	
}


var cButtonMarkup=(this.lcreateSearchButton)?'<div id="'+this.supplier+'-'+this.cApplicationName+'-go-button" class="ui-defpage-vehicle-go-button"></div>':'';
var cMarkup='\
    <div id="'+this.supplier+'-'+this.cApplicationName+'-lookup-lookup-form" class="ui-helper-reset ui-widget-content ui-defpage-vehicle-lookup-form">\
      <div id="'+this.supplier+'-'+this.cApplicationName+'-lookup-title" class="ui-helper-clearfix ui-widget-header ui-corner-bottom ui-defpage-vehicle-lookup-title" style="padding-left:4px;">\
	  '+this.cTitle+'</div>\
      <form name="'+this.supplier+'-'+this.cApplicationName+'-form" id="'+this.supplier+'-'+this.cApplicationName+'-form" action="'+this.cFormURL+'" method="post">\
		<div id="'+this.supplier+'-'+this.cApplicationName+'-vehicle-section" class="ui-defpage-lookup">'+cVehicleMarkup+'</div>\
		<div id="'+this.supplier+'-'+this.cApplicationName+'-product-section" class="ui-defpage-lookup">'+cProdMarkup+'</div>'+
		cButtonMarkup+'</form>\
    </div>';
jQuery(cMarkup).appendTo('#'+this.mainContainerId);
jQuery('#'+this.mainContainerId).show();

}

this.toggleSection=function(cSection,toggleType) {
	var mySection=jQuery('#'+this.supplier+'-'+this.cApplicationName+'-'+cSection+'-section');
	if (toggleType==="hide") {
		mySection.hide();
	} else {
		mySection.show();
	}
	
	}

this.setup=function(cSelSize,csupplier,uiID){
	if (typeof csupplier!=='undefined' && csupplier!==null) {this.supplier=csupplier.toLowerCase();}	
	setSupplierDefaults(this);
	this.mainContainerId=uiID?uiID:this.supplier+'-'+this.cApplicationName+'-lookup';
	this.drillManager.supplier=this.supplier;
	this.drillManager.lAutoLoadDrills=this.lAutoLoadDrills;
	this.createFullMarkup();
	
	// part drill  ( just  dummy drill here )
	var oDrillPart= new SophioAjaxCatalog.drillDefinitions.drillPart();
	
	if (this.supplier==='oec' ) {
		// catalog selector drill
	var oDrillCatalog= new SophioAjaxCatalog.drillDefinitions.drillCatalog(this.supplier);
	this.drillManager.addDrill(oDrillCatalog);
	}
	
	if (this.supplier!=='unv' ) {
	// year drill
	var oDrillYear= new SophioAjaxCatalog.drillDefinitions.drillYear(this.supplier);
	this.drillManager.addDrill(oDrillYear);
	// make drill
	var oDrillMake= new SophioAjaxCatalog.drillDefinitions.drillMake(this.supplier);
	this.drillManager.addDrill(oDrillMake);	
	//model drill
	var oDrillModel= new SophioAjaxCatalog.drillDefinitions.drillModel(this.supplier);	
	this.drillManager.addDrill(oDrillModel);
	}
	if (this.supplier==='whd' || this.supplier==='imc') {
		var oDrillEngine= new SophioAjaxCatalog.drillDefinitions.drillEngine(this.supplier);
		this.drillManager.addDrill(oDrillEngine);
		if (this.lVehicleOnly) {
		this.drillManager.setNextDrill(oDrillEngine,oDrillPart);
		oDrillEngine.lDisableOnChange=false;	
		}
	}
	
	if (this.supplier==='oec' ) {
	var oDrillTrim= new SophioAjaxCatalog.drillDefinitions.drillTrim(this.supplier);
	this.drillManager.addDrill(oDrillTrim);
	if (this.lVehicleOnly) {
	this.drillManager.setNextDrill(oDrillTrim,oDrillPart);
	oDrillTrim.lDisableOnChange=false;
	}
	}
	
	if (this.supplier==='tir' || this.supplier==='key' || this.supplier==='aaa') {
		if (this.lVehicleOnly) {
		this.drillManager.setNextDrill(oDrillModel,oDrillPart); 
		oDrillModel.lDisableOnChange=false;
		}
	}
	
	if (!this.lVehicleOnly ) {	
		if (this.supplier==='oec' ) {
			var oDrillSection= new SophioAjaxCatalog.drillDefinitions.drillSection(this.supplier);
			oDrillSection.beforeloadData=function() {me.vehicleSelected();}
			this.drillManager.addDrill(oDrillSection);
		}
		//group drill
		var oDrillGroup= new SophioAjaxCatalog.drillDefinitions.drillGroup(this.supplier);
		this.drillManager.addDrill(oDrillGroup);
		//group drill
		var oDrillSubgroup= new SophioAjaxCatalog.drillDefinitions.drillSubgroup(this.supplier);
		this.drillManager.addDrill(oDrillSubgroup);
		
		if (this.supplier!=='oec' ) {
		oDrillGroup.beforeloadData=function() {me.vehicleSelected();}
		this.drillManager.setNextDrill(oDrillSubgroup,oDrillPart);
		oDrillSubgroup.lDisableOnChange=false;
		}
		
		if (this.supplier==='oec' ) {
			var oDrillComponent= new SophioAjaxCatalog.drillDefinitions.drillComponent(this.supplier);
			this.drillManager.addDrill(oDrillComponent);
			var oDrillQualifier= new SophioAjaxCatalog.drillDefinitions.drillQualifier(this.supplier);
			this.drillManager.addDrill(oDrillQualifier);
			this.drillManager.setNextDrill(oDrillQualifier,oDrillPart);
			oDrillQualifier.lDisableOnChange=false;
		}
		
	}
	
	
	this.drillManager.setProperty("cURL",this.cDrillURL);
	this.drillManager.setProperty("parentDIVprefix",this.supplier+'-'+this.cApplicationName+"-");
	this.drillManager.setProperty("cSelectIDprefix",this.supplier+'-'+this.cApplicationName+"-");
	
	if (this.supplier ==='imc') {  // imc needs special handling
	oDrillModel.cDrillMode="MG";
	oDrillEngine.cDrillMode="MG";
	oDrillModel.cURL=oDrillModel.cURL+"&modelonly=yes";
	oDrillEngine.cURL=oDrillEngine.cURL+"&engineonly=yes";
	}
	

	oDrillPart.loadData=function(oSelect){me.endlookup(oSelect);};
	oDrillPart.beforeloadData=function(oSelect){me.productSelected(oSelect);};
	
	
	if (jQuery("#"+this.supplier+"-"+this.cApplicationName+"-go-button").length>0){
	this.cSearchButtonId=this.supplier+'-'+this.cApplicationName+"-search-button";	
	var cMarkup='<span style="margin-top:4px;">\
						<button id="'+this.supplier+'-'+this.cApplicationName+'-search-button"  type="button"\
						class="fg-button ui-state-default fg-button-icon-right ui-corner-all">\
						<span class="ui-icon ui-icon-search"></span>'+this.cSearchButtonCaption+'</button></span>'
						jQuery(cMarkup).appendTo("#"+this.supplier+"-"+this.cApplicationName+"-go-button");
	}
	if (jQuery('#'+this.supplier+'-'+this.cApplicationName+'-search-button').length>0){
	jQuery('#'+this.supplier+'-'+this.cApplicationName+'-search-button').click(function(e){
	   if (me.drillManager.isComplete()===false) {
		    e.preventDefault();
			return false;
	   }
	   e.preventDefault();
	   me.endlookup()
	   return false;
	   
		if (typeof SophioAjaxCatalog.endPartsLookup !=="undefined"){			
			SophioAjaxCatalog.endPartsLookup();
			e.preventDefault();
			return false;
		}
		
		if (typeof SophioAjaxCatalog.preventPageLeave !=='undefined') 
		SophioAjaxCatalog.preventPageLeave=false;
		var form = jQuery(this).parents('form:first');
		form.submit();
		
		  });
	}
	
	function setSupplierDefaults(oParent) {
		switch (oParent.supplier) {
			case 'whd':	{
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopbyVehicle.epc?sup=WHD&mode=PG";
					} else {
				oParent.cFormURL="ShopbyVehicle.epc?sup=WHD&mode=PA&waitdisplay=true";
					}
				oParent.cDrillURL="ShopbyVehicle.epc?sup=WHD";
				break;
			}
			case 'aaa':	{
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopbyVehicle.epc?sup=AAA&mode=PG";
				} else {
					oParent.cFormURL="ShopbyVehicle.epc?sup=AAA&mode=PA&waitdisplay=true";
				}
				oParent.cDrillURL="ecat.epc?_cmd=epccat_vehicleAAA";
				break;
			}
			case 'imc':	{
				
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopbyVehicle.epc?sup=IMC&mode=PJ";
				} else {
					oParent.cFormURL="ShopbyVehicle.epc?sup=IMC&mode=PA&waitdisplay=true";
				}
				oParent.cDrillURL="ShopbyVehicle.epc?sup=IMC";
				break;
			}
			case 'tir':	{
				oParent.cFormURL="ShopforTires.epc?sup=TIR&mode=PA";
				oParent.cDrillURL="ShopforTires.epc?sup=TIR";
				break;
			}
			case 'oec':	{
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopbyVehicle.epc?sup=OEC&mode=SC";
				} else {
					oParent.cFormURL="ShopbyVehicle.epc?sup=OEC&mode=PA&waitdisplay=true";
				}
				oParent.cDrillURL="ShopforOEParts.epc?sup=OEC";
				break;
			}
			case 'oea':	{
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopbyVehicle.epc?sup=OEA&mode=PG";
				} else {
					oParent.cFormURL="ShopbyVehicle.epc?sup=OEA&mode=PA&waitdisplay=true";
				}
				oParent.cDrillURL="ShopbyVehicle.epc?sup=OEA";
				break;
			}
			case 'unv':	{
				if (oParent.lVehicleOnly) {
					oParent.cFormURL="ShopUniversalParts.epc?sup=UNV&mode=PG";
				} else {
					oParent.cFormURL="ShopUniversalParts.epc?sup=UNV&mode=PA&waitdisplay=true";
				}
				oParent.cDrillURL="ShopUniversalParts.epc?sup=UNV";
				break;
			}
			case 'key':	{
				oParent.cFormURL="PerformanceParts.epc?mode=PG";
				oParent.cDrillURL="PerformanceParts.epc?";
				break;
			}
		}
	}
	
	this.vehicleSelected=function() {jQuery(this).triggerHandler('vehicleSelected',[this])};
	this.productSelected=function() {jQuery(this).triggerHandler('productSelected',[this])};
	
	this.endlookup=function(oSelect) {
		

			if (jQuery('#sophioDynamicContent').length>0) {			
				jQuery('#sophioDynamicContent').html("")
				.block({ 
				theme:     true, 
				title:    'Please Wait...loading parts!', 
				message:  '<center><p><img src="sophio-shared-assets/images/catalog/ajax-loader-blue-bar.gif" width="220" height="19"/></p></center>'
        		}); 
				jQuery.post(me.cFormURL+"&useAjaxOnPartListing=true&isAjax=true",me.drillManager.getDrillsValues(99),
							function (HTMLdata, textStatus) {
								if (textStatus==='success'){									
									jQuery('#sophioDynamicContent').html(HTMLdata).unblock();
									}
								}
							)	
			}else{
				
				oSelect.attr("disabled",false);
			if (typeof SophioAjaxCatalog.preventPageLeave !=='undefined') 
			SophioAjaxCatalog.preventPageLeave=false;
			if (me.cSearchButtonId!=="")
			jQuery("#"+me.cSearchButtonId).attr('disabled', 'disabled');
			jQuery.blockUI({ 
				theme:     true, 
				title:    'Please Wait...loading parts!', 
				message:  '<center><p><img src="sophio-shared-assets/images/catalog/ajax-loader-blue-bar.gif" width="220" height="19"/></p></center>'
        	}); 
				
				jQuery('#'+me.supplier+'-'+me.cApplicationName+'-form').submit();		
			}
		}
		
	
		// setup each drill
	this.drillManager.setupDrills(cSelSize);
	this.drillManager.loadFirstDrill();
	/*
	if (oDrillYear.oAjaxData==='')
	oDrillYear.loadData();
	*/
	
	// setup hover for buttons
jQuery('button.ui-state-default').hover(
   function(){jQuery(this).addClass('ui-state-hover');}, 
   function(){jQuery(this).removeClass('ui-state-hover');}
   );
	
	}
}

