var chosenGrade = getHttpGetParameters("grade");
var chosenBadge = getHttpGetParameters("badge");

function setBadge(id){
	if (flashIsLoaded) {
		FL('modelSelectorFlash').setBadge(id);
	}
}


var currentId = "";
var currentModelName = "";

var dataSet = new Array();

var flashIsLoaded = false;
var flashTimeout = null;


var flashvars = {
	//won'twork here
};
var flashParams = {
	menu: 					"false",
	wmode: 					"transparent",    
	allowscriptaccess:		"always",
	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);
	
//only now that the first call has been fully processed should we make the second call
	transform("LOCAL","/cars/_assets/transforms/features-table.xsl","/cars/"+currentModelName+"/_assets/data/features.xml","",processFeatures);
	
}

function processFeatures(response) {
	
	$('#featuresHolder').html(response);
	$("#modelSelectorFlash").attr("height", dataSet.length*50);
	
	/* To do...
	
	1. Add individual heights to .additional for each GRADE. Need to add some stuff to XML and traverse the DOM.
	2. If you click .moreDetails too quick (ie. it's still fading in), it will remain after the slide animation 
		because it's still becoming a block after it's opacity change.
		
	*/

	var extraWidth = 900 - ($("table.master").width()+1); // Extra 1 for left border
	var links = $(".links table").width()+2; // Extra 2 for borders each side
	var slideSpace = (extraWidth-links);
	//alert(slideSpace);
	var slideHeight = $("table.master").height()+31; // Extra 1 for left border
		
	if ($.browser.msie||$.browser.safari) { slideSpace = slideSpace-2; } // Safari still needs work, but I don't think it's due to this.
	
	$(".extraInfo table").css({width:slideSpace});
	//$(".extraInfo").css({height:$(".extraInfo table").height()});
	
	// Get caption padding
	var captionPadTop = $("table.master caption:first").css("padding-top");
	var captionPadBot = $("table.master caption:first").css("padding-bottom");
	var captionPadTop = parseInt(captionPadTop.split("px")[0]);
	var captionPadBot = parseInt(captionPadBot.split("px")[0]);
	
	
	$(".additional").each(function(i){
		/* To do... Set the top margin to this, and then add it the the slideHeight. */

		var corporate = $(this).parent(".engineTransmissionInfo").find("table.master")
		var corpHeight = corporate.height();
		var caption = $(this).parent(".engineTransmissionInfo").find(".master caption");
		var capHeight = caption.height();
		if (i>0) {
			$(this).parent(".engineTransmissionInfo").css({visibility:"hidden",display:"block"});
			corpHeight = corporate.height();
			capHeight = caption.height();
			$(this).parent(".engineTransmissionInfo").css({visibility:"visible",display:"none"});
		}
		var captionHeight = (captionPadTop+capHeight+captionPadBot);
		corpHeight = (corpHeight+captionHeight);
		$(this).css({height:corpHeight});
		$(this).find(".links").css({marginTop:captionHeight});
		//alert(captionHeight);
	});
	
	$(".additional").css({width:extraWidth+"px"}); // Needed -> ,height:slideHeight+"px"
	//$(".links").css({height:slideHeight+"px"}); Don't think I need this
	
	$(".moreDetails a").click(function () {
		$(".moreDetails").hide();
		$(".miniGallery").fadeOut("fast");
		$(".extraInfo").animate({width:slideSpace+"px"}, 500, function(){
			$("a.lessDetails").fadeIn("slow");
			$(".corporateDetails").fadeIn("slow");
		});
		return false;
	});
	
	$("a.lessDetails").click(function () {
		$(".extraInfo").animate({width:0}, 400, function(){
			$(this).hide();
			$(".corporateDetails").fadeOut("fast", function(){
				$(".miniGallery").fadeIn("normal");
				$(".moreDetails").fadeIn("slow");
				$(".extraInfo caption a").hide().css({display:"inline"}); // inline is a IE fix
			});
		});
		return false;
	});
	
	
	//get the current spec combo id ready for future on change requests
	//choose correct selects with selectFullSpec
	$('.engineTransmissionTbl .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];

			loadPanel("detail", $(this).attr("href"));				

			return false;
	
		});						   
			
			
	checkFlash();
	
	//	$('.featuresList').columnize({width: 225}); // Harry, this needs to be fixed
	//	$('.featuresList').columnize({columns: 3}); // Harry, this needs to be fixed

	//everything is loaded so now safe to continue panel behaviour
		
		
	panelPlay();

}

$(document).ready(
	function() {
		if (jQuery.browser.msie && $.browser.version <= 6) {
			
			$(".miniGallery").hide(1, function () {
				$(".miniGallery").show(1);
			});
			
		}
	}
);

function checkFlash() {
	if (flashTimeout) clearTimeout(flashTimeout);
	if (flashIsLoaded) {
		FL('modelSelectorFlash').setGrade(chosenGrade);
		FL('modelSelectorFlash').setBadge(chosenBadge);
		FL('modelSelectorFlash').receiveData(dataSet);	
	}
	else {
		flashTimeout = setTimeout("checkFlash()",1000);	
	}
}


function setTo(id) {
	//alert(id +" currentId="+currentId);
	
	$('.engineTransmissionInfo').hide();
	$('.featuresList').hide();
	$('.featuresTitle').hide();
	
	/*$('#eT'+currentId).hide();
	$('#featuresTitle'+currentId).hide();
	$('#featuresList'+currentId).hide();*/
	$('#eT'+id).show();
	$('#featuresTitle'+id).show();
	$('#featuresList'+id).show();
	currentId = id;
}

 
function flashIsReady() {		//flash found - flash starts loader anim now....
	flashIsLoaded = true;
	
}


function loadFeatures(model) {		//page loaded - start first ajax call
	currentModelName = model;
	panelPause();
	transform("CARDATA","/cars/_assets/transforms/engine-transmission-table.xsl","GetBadgeDetails",modelParams,processCorporate);
}

function panelCallback(status) {
	if(status == "ready") {
		checkFlash();
	}
}


