$(function(){

	/* My garage */
	var garage_form 	= $("#carpicker-garage");
	var garage_year 	= document.getElementById("garage-year");
	var garage_make 	= document.getElementById("garage-make");
	var garage_model 	= document.getElementById("garage-model");
	var garage_submodel = document.getElementById("garage-submodel");
	var garage_engine 	= document.getElementById("garage-engine");

	/* how-to carpicker */
	var how_to_form 	= $("#carpicker-how-to");
	var how_to_year 	= document.getElementById("how-to-year");
	var how_to_make 	= document.getElementById("how-to-make"); 
	var how_to_model 	= document.getElementById("how-to-model");
	var how_to_submodel = document.getElementById("how-to-submodel");
	var how_to_engine 	= document.getElementById("how-to-engine");
	
 
	/* estimate cost */
	var estimate_cost_form 	= $("#carpicker-estimate-costs");
	var estimate_cost_year = document.getElementById("estimate-cost-year");
	var estimate_cost_make = document.getElementById("estimate-cost-make");
	var estimate_cost_model = document.getElementById("estimate-cost-model");
	var estimate_cost_submodel = document.getElementById("estimate-cost-submodel");
	var estimate_cost_engine = document.getElementById("estimate-cost-engine");
	
	var current_vehicle = $('input.current-vehicle').val();
	var split_vehicle = []; 
	var current_year = '';  
	var current_make = ''; 
	var current_model = ''; 
	var current_submodel = ''; 
	var current_engine = ''; 

	if(current_vehicle){
		split_vehicle = current_vehicle.split('*'); 
		current_year = split_vehicle[0]; 
		current_make = split_vehicle[1];
		current_model = split_vehicle[2]; 
		current_submodel = (undefined != split_vehicle[3]) ? split_vehicle[3] : '';
		current_engine = (undefined != split_vehicle[4]) ? split_vehicle[4] : '';
	}	
	
	
	/* widget carpicker */
	var widget_form 	= $("#carpicker-widget");
	var widget_year 	= document.getElementById("widget-year");
	var widget_make 	= document.getElementById("widget-make"); 
	var widget_model 	= document.getElementById("widget-model");
	var widget_submodel = document.getElementById("widget-submodel");
	var widget_engine 	= document.getElementById("widget-engine");
	
	
	
	var enableDisableElement = function (element, options, text) {
		if (element)
		{
			if (options)
			{
				$(element).html('<option value="">Select '+ text +'</option>').attr({disabled:'disabled'});
			}
			else
			{
				$(element).removeAttr('disabled').find("option:eq('0')").attr({selected:"selected"});
			}
		}
	}

	/* Events */
	/*onload*/
	enableDisableElement(garage_year,true,'Year');
	enableDisableElement(garage_make,true,'Make');
	enableDisableElement(garage_model,true,'Model');
	enableDisableElement(garage_submodel,true,'Submodel');
	enableDisableElement(garage_engine,true,'Engine');		
	
	
	$(garage_year).change(function(){ 
			if(this.value == ''){
				enableDisableElement(garage_make,true,'Make');				
			}else{
				$(garage_make).html("<option value=''>Loading Makes...</option>");
				load_makes(garage_year,garage_make);
			}
			$(garage_submodel).hide(); 
			$(garage_engine).hide(); 
			enableDisableElement(garage_model,true,'Model');
			enableDisableElement(garage_submodel,true,'Submodel');
			enableDisableElement(garage_engine,true,'Engine');
		});
	
		/* load models */
		$(garage_make).change(function(){
			if(this.value == ''){
				enableDisableElement(garage_model,true,'Model');				
			}else{
				$(garage_model).html("<option value=''>Loading Models...</option>");
				load_models(garage_year,garage_make,garage_model,garage_submodel,garage_engine);
			}
			$(garage_submodel).hide(); 
			$(garage_engine).hide(); 
			enableDisableElement(garage_submodel,true,'Submodel');
			enableDisableElement(garage_engine,true,'Engine');
		});
		/* load submodel */
		$(garage_model).change(function(){
			if(this.value == ''){				
				enableDisableElement(garage_submodel,true,'Submodel');
				enableDisableElement(garage_engine,true,'Engine');
				$(garage_submodel).hide(); 
			}else{
				$(garage_submodel).show(); 
				$(garage_submodel).html("<option value=''>Loading Submodels...</option>");
				load_submodels(garage_year,garage_make,garage_model,garage_submodel,garage_engine);
			}			
			$(garage_engine).hide(); 
			enableDisableElement(garage_engine,true,'Engine');
		});
		/* load engine */
		$(garage_submodel).change(function(){
			if(this.value == ''){ 
				$(garage_engine).hide(); 					
				enableDisableElement(garage_engine,true,'Engine');
			}else{
				$(garage_engine).show(); 			
				$(garage_engine).html("<option value=''>Loading Sub Engines...</option>");
				load_engines(garage_year,garage_make,garage_model,garage_submodel,garage_engine);
			}
		});
	
		$(garage_engine).change(function(){
			/*if(this.value != ''){
			$(form).submit(); 
			}*/
		})
	
	
	if(estimate_cost_form.length > 0){
		enableDisableElement(estimate_cost_year,true,'Year');
		enableDisableElement(estimate_cost_make,true,'Make');
		enableDisableElement(estimate_cost_model,true,'Model');
		enableDisableElement(estimate_cost_submodel,true,'Submodel');
		enableDisableElement(estimate_cost_engine,true,'Engine');		
		$(estimate_cost_year).change(function(){
			if(this.value == ''){				
				enableDisableElement(estimate_cost_make,true,'Make');				
			}else{
				$(estimate_cost_make).html("<option value=''>Loading Makes...</option>");
				load_makes(estimate_cost_year,estimate_cost_make);
			}
			$(estimate_cost_submodel).hide(); 
			$(estimate_cost_engine).hide(); 
			$("#search-jobs").attr("disabled","disabled");
			enableDisableElement(estimate_cost_model,true,'Model');
			enableDisableElement(estimate_cost_submodel,true,'Submodel');
			enableDisableElement(estimate_cost_engine,true,'Engine');
		});

		/* load models */
		$(estimate_cost_make).change(function(){
			if(this.value == ''){			
				enableDisableElement(estimate_cost_model,true,'Model');				
				$("#search-jobs").attr("disabled","disabled");
			}else{
				$(estimate_cost_model).html("<option value=''>Loading Models...</option>");							
				load_models(estimate_cost_year,estimate_cost_make,estimate_cost_model,estimate_cost_submodel,estimate_cost_engine);				
			}
			$(estimate_cost_submodel).hide(); 
			$(estimate_cost_engine).hide(); 			
			enableDisableElement(estimate_cost_submodel,true,'Submodel');
			enableDisableElement(estimate_cost_engine,true,'Engine');
		});
		/* load submodel */
		$(estimate_cost_model).change(function(){
			if(this.value == ''){				
				enableDisableElement(estimate_cost_submodel,true,'Submodel');
				enableDisableElement(estimate_cost_engine,true,'Engine');
				$(estimate_cost_submodel).hide(); 				
				$("#search-jobs").attr("disabled","disabled");				
			}else{				
				$(estimate_cost_submodel).show(); 
				$(estimate_cost_submodel).html("<option value=''>Loading Submodels...</option>");
				load_submodels(estimate_cost_year,estimate_cost_make,estimate_cost_model,estimate_cost_submodel,estimate_cost_engine);
				$("#search-jobs").removeAttr("disabled");  				
			}			
			$(estimate_cost_engine).hide(); 
			enableDisableElement(estimate_cost_engine,true,'Engine');
		});
		/* load engine */ 
		$(estimate_cost_submodel).change(function(){
			if(this.value == ''){
				$(estimate_cost_engine).hide(); 
				enableDisableElement(estimate_cost_engine,true,'Engine');
			}else{ 
				$(estimate_cost_engine).show();
				$(estimate_cost_engine).html("<option value=''>Loading Sub Engines...</option>");
				load_engines(estimate_cost_year,estimate_cost_make,estimate_cost_model,estimate_cost_submodel,estimate_cost_engine);
			}
		});

		$(estimate_cost_engine).change(function(){
			/*if(this.value != ''){
			$(form).submit();
			}*/
		})

	}
	if(how_to_form.length > 0){
		enableDisableElement(how_to_year,true,'Year');
		enableDisableElement(how_to_make,true,'Make');
		enableDisableElement(how_to_model,true,'Model');
		enableDisableElement(how_to_submodel,true,'Submodel');
		enableDisableElement(how_to_engine,true,'Engine'); 		
		$(how_to_year).change(function(){
			if(this.value == ''){				
				enableDisableElement(how_to_make,true,'Make');
			}else{
				$(how_to_make).html("<option value=''>Loading Makes...</option>");
				load_makes(how_to_year,how_to_make);
			}
			$(how_to_submodel).hide(); 
			$(how_to_engine).hide(); 
			enableDisableElement(how_to_model,true,'Model');
			enableDisableElement(how_to_submodel,true,'Submodel');
			enableDisableElement(how_to_engine,true,'Engine');

			var scevar      = [];
	        scevar['eVar19'] = s.pageName;
			var scevent     = [];
			sclinkCode(s_account, 'Vehicle Widget', scevent, scevar);
		});

		/* load models */
		$(how_to_make).change(function(){
			if(this.value == ''){				
				enableDisableElement(how_to_model,true,'Model');
			}else{
				$(how_to_model).html("<option value=''>Loading Models...</option>");
				load_models(how_to_year,how_to_make,how_to_model,how_to_submodel,how_to_engine);
			}
			$(how_to_submodel).hide(); 
			$(how_to_engine).hide();  
			enableDisableElement(how_to_submodel,true,'Submodel');
			enableDisableElement(how_to_engine,true,'Engine');
		});
		/* load submodel */
		$(how_to_model).change(function(){
			if(this.value == ''){								
				$(how_to_submodel).hide();
				enableDisableElement(how_to_submodel,true,'Submodel');
				enableDisableElement(how_to_engine,true,'Engine');
			}else{				
				$(how_to_submodel).show();
				$(how_to_submodel).html("<option value=''>Loading Submodels...</option>");
				load_submodels(how_to_year,how_to_make,how_to_model,how_to_submodel,how_to_engine);
			} 
			$(how_to_engine).hide(); 
			enableDisableElement(how_to_engine,true,'Engine');
		});
		/* load engine */
		$(how_to_submodel).change(function(){
			if(this.value == ''){				
				enableDisableElement(how_to_engine,true,'Engine');
				$(how_to_engine).hide(); 
			}else{
				$(how_to_engine).show(); 
				$(how_to_engine).html("<option value=''>Loading Sub Engines...</option>");
				load_engines(how_to_year,how_to_make,how_to_model,how_to_submodel,how_to_engine);
			}
			
		});

		$(how_to_engine).change(function(){
			/*if(this.value != ''){
			$(form).submit();
			}*/
		}) 
	} 
	
	
	if(widget_form.length > 0){
		enableDisableElement(widget_year,true,'Year');
		enableDisableElement(widget_make,true,'Make');
		enableDisableElement(widget_model,true,'Model');
		enableDisableElement(widget_submodel,true,'Submodel');
		enableDisableElement(widget_engine,true,'Engine'); 		
		$(widget_year).change(function(){
			if(this.value == ''){				
				enableDisableElement(widget_make,true,'Make');
			}else{
				$(widget_make).html("<option value=''>Loading Makes...</option>");
				load_makes(widget_year,widget_make);
			}
			$(widget_submodel).hide(); 
			$(widget_engine).hide(); 
			enableDisableElement(widget_model,true,'Model');
			enableDisableElement(widget_submodel,true,'Submodel');
			enableDisableElement(widget_engine,true,'Engine');
		});

		/* load models */
		$(widget_make).change(function(){
			if(this.value == ''){				
				enableDisableElement(widget_model,true,'Model');
			}else{
				$(widget_model).html("<option value=''>Loading Models...</option>");
				load_models(widget_year,widget_make,widget_model,widget_submodel,widget_engine);
			}
			$(widget_submodel).hide(); 
			$(widget_engine).hide();  
			enableDisableElement(widget_submodel,true,'Submodel');
			enableDisableElement(widget_engine,true,'Engine');
		});
		/* load submodel */
		$(widget_model).change(function(){
			if(this.value == ''){								
				$(widget_submodel).hide();
				enableDisableElement(widget_submodel,true,'Submodel');
				enableDisableElement(widget_engine,true,'Engine');
			}else{				
				$(widget_submodel).show();
				$(widget_submodel).html("<option value=''>Loading Submodels...</option>");
				load_submodels(widget_year,widget_make,widget_model,widget_submodel,widget_engine);
			} 
			$(widget_engine).hide(); 
			enableDisableElement(widget_engine,true,'Engine');
		});
		/* load engine */
		$(widget_submodel).change(function(){
			if(this.value == ''){				
				enableDisableElement(widget_engine,true,'Engine');
				$(widget_engine).hide(); 
			}else{
				$(widget_engine).show(); 
				$(widget_engine).html("<option value=''>Loading Sub Engines...</option>");
				load_engines(widget_year,widget_make,widget_model,widget_submodel,widget_engine);
			}
			
		});

		$(widget_engine).change(function(){
			/*if(this.value != ''){
			$(form).submit();
			}*/
		}) 
	} 
	
	
	
	
	
	var load_makes = function(year,make,preload)
	{	
		$.getJSON("/carpicker/get_makes",{year_id:$(year).val()},function(data){
			if (!data.error){
				enableDisableElement(make,false);			
				var options = [], i = 0, o = null;	
				make.options[0].text = "Select Make"; 
				for (i=0; i<data.make_years.length; i++) {
					make.options[i+1] = new Option(data.make_years[i].make_name,data.make_years[i].make_id);					
					if(preload && data.make_years[i].make_id == current_make){
						make.options[i+1].selected = true; 										
					}
				}
				if(preload){  
					load_models(estimate_cost_year,estimate_cost_make,estimate_cost_model,estimate_cost_submodel,estimate_cost_engine,true);
				}	 			 	 
			}else{
				make.options[0].text = "No Makes"; 
			}			
			
		});
	} 

	var load_models = function(year,make,model,submodel,engine,preload)
	{
		$.getJSON("/carpicker/get_models",{year_id:$(year).val(),make_id:$(make).val()},function(data){														
			enableDisableElement(model,false);			
			var options = [], i = 0, o = null;	
			model.options[0].text = "Select Model"; 			
			if (!data.error){
				$("#search-jobs").attr("disabled","disabled");	 	
				for (i=0; i<data.models.length; i++) {
					model.options[i+1] = new Option(data.models[i].model_name,data.models[i].model_id);
					model.options[i+1].selected = (data.models.length == 1 || (preload && data.models[i].model_id == current_model)) ? true : false; 
				}
				if(data.models.length == 1){
					$(submodel).show(); 					
					$("#search-jobs").removeAttr("disabled");
					load_submodels (year,make,model,submodel,engine); 
				}
				if(preload){
					$("#search-jobs").removeAttr("disabled");
					$(submodel).show(); 
					load_submodels(year,make,model,submodel,engine,true);
				}			
			}
			 
			
		});

	}
	var load_submodels = function(year,make,model,submodel,engine,preload)
	{
		$.getJSON("/carpicker/get_submodels",{year_id:$(year).val(),make_id:$(make).val(),model_id:$(model).val()},function(data){							
			var options = [], i = 0, o = null;	
			submodel.options[0].text = "Select Submodel"; 						
			if (!data.error){
				enableDisableElement(submodel,false);
				$(submodel).show(); 
				for (i=0; i<data.submodels.length; i++) {
					submodel.options[i+1] = new Option(data.submodels[i].submodel_name,data.submodels[i].submodel_id);
					submodel.options[i+1].selected = (data.submodels.length == 1 || (preload && data.submodels[i].submodel_id == current_submodel)) ? true : false; 
				}
				if(data.submodels.length == 1){
					$(engine).show();  
					load_engines (year,make,model,submodel,engine); 
				}
				if(preload && current_submodel){
					$(engine).show();
					load_engines(year,make,model,submodel,engine,true);
				}
			}else{				
			 	$(submodel).hide(); 
				$(engine).hide(); 
			}											  
		}); 
	}

	var load_engines = function(year,make,model,submodel,engine,preload)
	{
		$.getJSON("/carpicker/get_engines",{year_id:$(year).val(),make_id:$(make).val(),model_id:$(model).val(),submodel_id:$(submodel).val()},function(data){						
			var options = [], i = 0, o = null;		
			engine.options[0].text = "Select Engine"; 								
			if (!data.error){					
				enableDisableElement(engine,false);
				$(engine).show(); 
				for (i=0; i<data.engines.length; i++) {
					engine.options[i+1] = new Option(data.engines[i].engine_name,data.engines[i].engine_id);
					engine.options[i+1].selected = (data.engines.length == 1 || (preload && data.engines[i].engine_id == current_engine)) ? true : false; 
				}; 
				
			}else{
				$(submodel).hide(); 
				$(engine).hide(); 
			}		
		}); 
	}
	
	var load_years = function()
	{
		$.getJSON("/carpicker/get_years", function(data) {
			if(!data.error){
				if (data.years.length){
					var options = [], i = 0,j=0; o = null;
					enableDisableElement(garage_year,false);
					enableDisableElement(widget_year,false);
					enableDisableElement(estimate_cost_year,false);
					enableDisableElement(how_to_year,false);
					for (i=0; i<data.years.length; i++) { 
//					   if(data.years[i].year < 2010 && data.years[i].year >= 1942){
					   	   j++; 	
	                       if($(how_to_form).length > 0){ 
								how_to_year.options[j] =  new Option(data.years[i].year,data.years[i].year_id); 								
						   }	
						   //alert(current_year)
	                       if($(estimate_cost_form).length > 0){                       	
	                       	 estimate_cost_year.options[j] = new Option(data.years[i].year,data.years[i].year_id);                         	 	                       	
	                       	 estimate_cost_year.options[j].selected = (data.years[i].year_id == current_year) ? true : false; 	                       	 							 
						   }
						   if($(widget_form).length > 0){ 
						   	  widget_year.options[j] = new Option(data.years[i].year,data.years[i].year_id);
						   }
						   garage_year.options[j] = new Option(data.years[i].year,data.years[i].year_id);   
					   //}
						
					}
					if($(estimate_cost_form).length > 0){
						if(current_year > 0){
							load_makes(estimate_cost_year,estimate_cost_make,true);						
							$("#search-jobs").removeAttr("disabled");
						}
					} 			
				} 
			}	
		});  
	}
	load_years();
}); 