
var chosenGrade = getHttpGetParameters("grade");


var flashIsLoaded = false;
var flashTimeout = null;

jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}


var flashvars = {
		};
		var flashvars = {
			//configurationxml:		"data/form_configuration.xml",
			//batchxml: 				"data/lov_batch.xml"
		};
		var flashParams = {
			menu: 					"false",
			allowscriptaccess:		"always",
			wmode: 					"transparent",
			salign: 				"mt",
			quality: 				"best",
			scale: 					"noscale"
		};
		var attributes =  {
			id: 					"modelSelectorFlash",
			name: 					"modelSelectorFlash"
		};
		swfobject.embedSWF("/cars/_assets/flash/range-overview.swf", "modelSelector", "315", "200", "9.0.0", "/_assets/flash/expressInstall.swf", flashvars, flashParams, attributes);

function processCorporate(response) {

	$('#engineTransmissionHolder').html(response);
	$('.currency').prepend("&pound;");
	checkFlash();

}

function processFeatures(response) {

	$('#featuresHolder').html(response);
	
	$('.getCurrentSpec').livequery("click", 
		function() {
			// get the current selected
			var idArray = $(this).attr("id").split("_");
			memory.gradeID 			= idArray[0];
			memory.engineID 		= idArray[1];
			memory.transmissionID 	= idArray[2];			
			changeDropDownsFromMemory();
			return false;
	});		
	showFeatures() 
}




function checkFlash() {
	if (flashTimeout) { clearTimeout(flashTimeout); }
	if (flashIsLoaded) {
		FL('modelSelectorFlash').setGrade(chosenGrade);
		FL('modelSelectorFlash').receiveData(dataSet);
		
		
		// MM Calculate new height of divs once the number of grades is known.
		
		$("#modelSelectorFlash").attr("height", dataSet.length*52);
		var newHeightWithGrades=$("#gradeSelector").height();
		var getheroheight =$("#gradeSelector").height()+45; 
		$("#transparentBox").height(newHeightWithGrades);
				
		//set height for CRV hero image
		if (model == "CR-V"){
		$("#rangeOverview").css("padding-top",getheroheight);
		}
		else
		{
		$("#rangeOverview").css("padding-top",newHeightWithGrades);
		}
		
		
		
	}
	else {
		flashTimeout = setTimeout("checkFlash()",1000);	
	}
}


function flashIsReady() {		//flash found - flash starts loader anim now....
	flashIsLoaded = true;
	
}


function setTo(id) {

	$('.engineTransmissionTbl,.engine').hide();
	$('#eT'+id).show();

	$('select#engine'+id).show();

	memory.gradeID 			= id;
	memory.engineID 		= null;
	memory.transmissionID 	= null;
	changeDropDownsFromMemory();

	showFeatures();
}

function showFeatures() {
	$('.featuresTitle').hide();
	$('.featuresList').hide();
	$('#featuresTitle'+memory.gradeID).show();
	$('#featuresList'+memory.gradeID).show();
}

function initFleetRange(car) {
	fleetRangePage = true;
	initFullSpec();
	transform("CARDATA","/cars/_assets/transforms/engine-transmission-corporate-table.xsl","GetBadgeDetails",modelParams,processCorporate);
	transform("LOCAL","/cars/_assets/transforms/features-table-2-columns.xsl","/cars/"+car+"/_assets/data/features.xml","",processFeatures);
	$('a#saveAsPdf, a#saveAsPdf2').livequery('click', function(e) { 
		
		e.preventDefault();
		
		// find out what car it is
		var gradeID = memory.gradeID;
		var engineID = memory.engineID;
		var transmissionID = memory.transmissionID;
		var date = new Date();
		var currentDate = date.getDate() + "/" + (date.getMonth()+1) + "/" + date.getFullYear();
		
		// construct the webservices we're going to use	
		$.ajax({"url": "/_assets/behaviour/getglobalvar.asp?var=CARDATA", success: function(response) {
			
			var peper = response.toString();
			
			var gradesWS = peper+"GetBadgeDetails?genpoCode=006&"+modelParams+gradeID;
			var optionsWS = peper+"GetFullSpecificationByModelBody?genpoCode=006&"+modelParams+gradeID;
			
			$.ajax({"url": "/_assets/behaviour/getglobalvar.asp?var=LOCAL", success: function(response) {
			
				var local = response.toString();
				var featuresWS = local+"/cars/"+car+"/_assets/data/features.xml";

				$('#pdf-form-urls').val(gradesWS+"|"+optionsWS+"|"+featuresWS);
				$('#pdf-form-name').val(car + "-full-specification.pdf");
				$('#pdf-form-template').val("fullspec"); 
				$('#pdf-form-parameters').val("engineID:"+engineID+",transmissionID:"+transmissionID+",car:"+car+",model:"+model+",body:"+body+",gradeID:"+gradeID+",currentDate:"+currentDate);
						
				$('#pdfForm').submit();
			}});
		}});
	});			
}
