/**
2 * @author egreiner
3 */
var modelObject='';
(function($){
	$.musa_clunkers={
	
		_init:function(){			
			$.musa_clunkers.popContent();
			$.musa_clunkers.closeContent();
			if(isMain)$.musa_clunkers.initSelects();
		},
		 // fadein content
		popContent:function(){		
			$('.popContent').click(function () {	
				
				var topscroll=(document.documentElement.scrollTop);
				
				$('.eligible').css("top", $(this).offset().top/2  - $('.eligible').height()/2 + topscroll );	
				$('.eligible').css("left", 250);
				$('.eligible').fadeIn(400);			
			});
		},
		
		// fadeout content
		closeContent:function(){ 
			$('.closeDiv').click(function () {			
				$(".eligible").fadeOut(400);			
			});
		},
		
		
		popWindow: function(url) {
			window.open (url, "win1","scrollbars=1,resizable=0,width=620,height=370"); 
		},
				
		modelUpdate: function() {
			document.cashForClunkersForm.combinedMPG.value='';
			yearElement=document.cashForClunkersForm.changeYear;
			makeElement=document.cashForClunkersForm.changeMake;
			modelElement=document.cashForClunkersForm.changeModel;
			
			defaultVal="Model/Drive";
			modelElement.length = 0;
			modelElement.options[0] = new Option(defaultVal);
			modelElement.options[0].value = "";
			
			engineElement=document.cashForClunkersForm.changeEngine;			
			engineElement.length = 0;
			engineElement.options[0] = new Option("Engine/Transmission");
			engineElement.options[0].value = "";
			
			var yr= yearElement.value;
			var mk=makeElement.value;
			
			//kfc
			document.cashForClunkersForm.make.value = document.cashForClunkersForm.changeMake.value;
			//end

			$.getJSON("/webservices/mx/carClunkers/" + yr + "/" + mk, function(json){			  
			   modelObject=json;			 
			  $.musa_clunkers.modelSelectFill();			  	
			});			
		
		},
		
		modelSelectFill: function() {
			modelElement=document.cashForClunkersForm.changeModel;
			
			
			for( var i = 0; i < modelObject.resultList.length; i++ ) {
				modelElement.options[i + 1] = new Option(modelObject.resultList[i].model);
				modelElement.options[i + 1].value = modelObject.resultList[i].model;				
			}	

			//alert("done");
			//alert(modelElement.length);

			//alert("Model: " + document.cashForClunkersForm.model.value);
			if (document.cashForClunkersForm.model.value != null && document.cashForClunkersForm.model.value != '') {
				document.cashForClunkersForm.changeModel.value = document.cashForClunkersForm.model.value;
				$.musa_clunkers.engineUpdate();
				
			}
			
			//alert("Engine: " + document.cashForClunkersForm.engine.value);
			if (document.cashForClunkersForm.engine.value != null && document.cashForClunkersForm.engine.value != '') {
				document.cashForClunkersForm.changeEngine.selectedIndex = document.cashForClunkersForm.engine.value;
				$.musa_clunkers.mpgUpdate();
			}
		},
		
		engineUpdate: function() {
			document.cashForClunkersForm.combinedMPG.value='';
			defaultVal="Engine/Transmission";
			
			modelElement=document.cashForClunkersForm.changeModel;
			engineElement=document.cashForClunkersForm.changeEngine;
			
			engineElement.length = 0;
			engineElement.options[0] = new Option(defaultVal);
			engineElement.options[0].value = "";
			
			modsel=modelElement.value;
			modidx=modelElement.selectedIndex-1;
			for( var i = 0; i < modelObject.resultList[modidx].detailList.length; i++ ) {
				engineElement.options[i + 1] = new Option(modelObject.resultList[modidx].detailList[i].engine + ' ' + modelObject.resultList[modidx].detailList[i].transmission);
				engineElement.options[i + 1].value = modelObject.resultList[modidx].detailList[i].combinedMPG;						
			}	
			//kfc
			//alert("model: " + document.cashForClunkersForm.changeModel.value);
			document.cashForClunkersForm.model.value = document.cashForClunkersForm.changeModel.value;
		},
		
		mpgUpdate: function() {
			document.cashForClunkersForm.combinedMPG.value=document.cashForClunkersForm.changeEngine.value;

			//kfc
			//alert("mpg is " + document.cashForClunkersForm.changeEngine.value);
			document.cashForClunkersForm.engine.value = document.cashForClunkersForm.changeEngine.selectedIndex;
		},
		
		makeUpdate: function(elem) {
			document.cashForClunkersForm.combinedMPG.value='';
			var yearElement=document.cashForClunkersForm.changeYear;
			var makeElement=document.cashForClunkersForm.changeMake;
			
			makeElement.length = 0;
			makeElement.options[0]=new Option("Make");
			
			modelElement=document.cashForClunkersForm.changeModel;			
			modelElement.length = 0;
			modelElement.options[0] = new Option("Model/Drive");
			modelElement.options[0].value = "";
			
			engineElement=document.cashForClunkersForm.changeEngine;			
			engineElement.length = 0;
			engineElement.options[0] = new Option("Engine/Transmission");
			engineElement.options[0].value = "";
			
			
			for(i=1; i<yearMakes[yearElement.selectedIndex-1].length; i++) {
				makeElement.options[i] = new Option(yearMakes[yearElement.selectedIndex-1][i].name);
				makeElement.options[i].value = yearMakes[yearElement.selectedIndex-1][i].name;
			}
			
			//kfc: 07/22/09
			document.cashForClunkersForm.year.value = document.cashForClunkersForm.changeYear.value;
		},
		
		
		initSelects: function() {			
			document.cashForClunkersForm.changeYear.length=0;
			document.cashForClunkersForm.combinedMPG.value='';
			document.cashForClunkersForm.changeYear.options[0]=new Option("Year");
			for(i=0;i<yearMakes.length;i++) {
				document.cashForClunkersForm.changeYear.options[i+1]= new Option(yearMakes[i][0]);
				document.cashForClunkersForm.changeYear.options[i+1].value=yearMakes[i][0];
			}						
			
			//alert("Year: " + document.cashForClunkersForm.year.value);
			if (document.cashForClunkersForm.year.value != null && document.cashForClunkersForm.year.value != '') {
				document.cashForClunkersForm.changeYear.value = document.cashForClunkersForm.year.value;
				$.musa_clunkers.makeUpdate();
			}
			
			//alert("Make: " + document.cashForClunkersForm.make.value);
			if (document.cashForClunkersForm.make.value != null && document.cashForClunkersForm.make.value != '') {
				document.cashForClunkersForm.changeMake.value = document.cashForClunkersForm.make.value;
				$.musa_clunkers.modelUpdate();
			}
		}
	}
	$.musa_clunkers._init();	
	
})(jQuery);



function popupDisplay(elemPos) {
	$.musa_clunkers.popContent(elemPos);
}