// JavaScript Document

if (typeof SophioAjaxCatalog === "undefined") {
SophioAjaxCatalog = {};
}


if (typeof SophioAjaxCatalog.garageCrumb === "undefined") {
	SophioAjaxCatalog.garageCrumb=function() {
	return new function() {
		this.hasGarage=false;
		this.garageData=new Array();
		this.timeOut=30000;
		this.cURL="GetSavedVehicles.epc";
		this.cTitle="Saved Vehicles";
		this.cMoreLink="See more...";
		this.cLessLink="See less...";
		this.cMainContainerID="defpage-garage";
		this.setup=function(cMainContainerID) {
				this.cMainContainerID=(cMainContainerID?cMainContainerID:this.cMainContainerID);
				this.createMarkup();
				if (!this.hasGarage)
					return
					this.loadGarage();
				}
		this.createMarkup=function() {
				if (jQuery('#'+this.cMainContainerID).length==0) { return true;}		
				var cMarkup='\
				         <div id="defpage-garage-content" class="ui-widget-content">\
                      	<div id="defpage-garage-header" class="ui-widget-header ui-corner-bottom">'+this.cTitle+'</div>\
                         <ul></ul>\
                        <a id="defpage-garage-seemore" class="fg-button fg-button-icon-left">\
						<span class="ui-icon ui-icon-circle-triangle-s"></span>'+this.cMoreLink+'</a>\
                      	<a id="defpage-garage-seeless" class="fg-button fg-button-icon-left">\
						<span class="ui-icon ui-icon-circle-triangle-n"></span>'+this.cLessLink+'</a>\
						</div>'
				jQuery(cMarkup).appendTo('#'+this.cMainContainerID);
				this.hasGarage=true;
				var self=this;
				jQuery("#defpage-garage-seemore").click(function(){self.showMore();});
				jQuery("#defpage-garage-seeless").click(function(){self.showLess();});
			  }				
			this.loadGarage=function(){
				if (!this.hasGarage) {return true;}
				var self=this;
				jQuery.ajax({
					url: this.cURL,
					data:"ajax=true&nooutput=true&isAjax=true" ,
					dataType: "json",
					cache: false,
					timeout: this.timeOut,
					success: function(data, status){						
						if (data.success){ 							
							self.garageData=data.data;
							self.parseGarageData();
						}
					},
					error: function(XHR, textStatus, errorThrown){					
						// show the error
						//self.showError('There was an error getting data from our server.Please try again!');
						//jQuery('#OEproduct_lookup_smartsearch input[name=keyword]').attr('disabled',false).removeClass('busy').val(self.cValue).focus();
						//alert("ERREUR: " + textStatus);
						//alert("ERREUR: " + errorThrown);
						
					}
				});
				
			}
			// adds a vehicle to the garage
			this.addVehicleToGarage=function(aVehicle){
				if (typeof aVehicle==="object"){
					// add vehicle to the top
					var lIsInGarage=false;
					jQuery.each(this.garageData,function(i,val){
						if (aVehicle.vehicle.year.id===val.vehicle.year.id && 
							aVehicle.vehicle.make.id===val.vehicle.make.id && 
							aVehicle.vehicle.model.id===val.vehicle.model.id && 
							aVehicle.vehicle.submodel.id===val.vehicle.submodel.id )
						{lIsInGarage=true;}
								})
					if (!lIsInGarage) {
					this.garageData.splice(0,0,aVehicle);
					// re-parse garage for display					
					this.parseGarageData();
					}
					
				}
			}
			this.showMore=function(){
				jQuery("#defpage-garage-seemore").hide("fast",function(){
					jQuery("#defpage-garage-content li:gt(0)")
					.show("fast",function(){
					jQuery("#defpage-garage-seeless").show()
					});
					});
				}
			this.showLess=function(){
				jQuery("#defpage-garage-seeless").hide("fast",function(){
					jQuery("#defpage-garage-content li:gt(0)")
					.hide("fast",function(){
					jQuery("#defpage-garage-seemore").show()
					});
					});
				}		
			this.loadVehicle=function(nCarIndex){
				var cURL='loadVehicle.epc?mode=PL&yearid='+this.garageData[nCarIndex].vehicle.year.id+
	 				'&makeid='+this.garageData[nCarIndex].vehicle.make.id+
					'&modelid='+this.garageData[nCarIndex].vehicle.model.id+
	 				'&submodelid='+this.garageData[nCarIndex].vehicle.submodel.id+
	 				'&engineid='+this.garageData[nCarIndex].vehicle.engine.id+
					'&carsup='+this.garageData[nCarIndex].vehicle.carsup.id;
				if (typeof SophioAjaxCatalog.preventPageLeave !=='undefined') SophioAjaxCatalog.preventPageLeave=false;
	 			window.location.href=cURL;
				//jQuery(this).trigger('carloaded',[this.garageData[nCarIndex]]);
				return false;
				}
			// parses data from this.garageData into the display	
			this.parseGarageData=function(){
				var cars="";
				var self=this;
				jQuery("#defpage-garage-content ul").html("");
				jQuery.each(this.garageData,function(i,val){					
				if (i<=4) {
					jQuery("#defpage-garage-content ul").append(
					jQuery('<li class="ui-widget-content"><a href="#">'+val.vehicle.year.desc+" "+val.vehicle.make.desc+" "+val.vehicle.model.desc+(val.vehicle.submodel.desc!==''?' '+val.vehicle.submodel.desc:'')+(val.vehicle.engine.desc!==''?' '+val.vehicle.engine.desc:'')+"</a></li>")
						.click(function(){
						self.loadVehicle(i);
						return false;										
						}));
						}								 						
					});	
				

				
				if (this.garageData.length>1){
				jQuery("#defpage-garage-content li:gt(0)").hide();	
				jQuery("#defpage-garage-seemore").show();
				}
				
				// show garace crumb
				if (this.garageData.length>0)
				jQuery('#'+this.cMainContainerID).show();
				
				}								
			
	}
	}
}




