// ==ClosureCompiler==
// @output_file_name modelgrades.min.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler=

var chosenGrade = getHttpGetParameters("grade");
var chosenBadge = getHttpGetParameters("badge");

var currentId = "";
var currentModelName = "";

var dataSet = [];

var flashIsLoaded = false;
var flashTimeout = null;

var flashvars = {

};

var flashParams = {
	menu: 					"false",
	wmode: 					"transparent",    
	allowscriptaccess:		"always",
	salign: 				"mt",
	quality: 				"best",
	scale: 					"noscale"
};
var attributes =  {
	id: 					"modelSelectorFlash",
	name: 					"modelSelectorFlash"
};

$(document).ready(function () {
    if (jQuery.browser.msie && $.browser.version <= 6) {

        $(".miniGallery").hide(1, function () {
            $(".miniGallery").show(1);
        });
    }
});

function setBadge(id) {
    if (flashIsLoaded) {
        FL('modelSelectorFlash').setBadge(id);
    }
}

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) {

    swfobject.embedSWF("/cars/_assets/flash/range-overview.swf", "modelSelector", "315", (dataSet.length * 60).toString(), "9.0.0", "/_assets/flash/expressInstall.swf", flashvars, flashParams, attributes);
	$('#featuresHolder').html(response);

	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);
	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});

	// Get caption padding
	var $caption = $('table.master caption:first');
	var captionPadTop = $caption.css("padding-top") + '';
	var captionPadBot = $caption.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 $this = $(this);
	    var $transmission = $this.parent(".engineTransmissionInfo");
	    var corporate = $transmission.find("table.master")
	    var corpHeight = corporate.height();
	    var caption = $transmission.find(".master caption");
	    var capHeight = caption.height();
	    if (i > 0) {
	        $transmission.css({ visibility: "hidden", display: "block" });
	        corpHeight = corporate.height();
	        capHeight = caption.height();
	        $transmission.css({ visibility: "visible", display: "none" });
	    }
	    var captionHeight = (captionPadTop + capHeight + captionPadBot);
	    corpHeight = (corpHeight + captionHeight);
	    $this.css({ height: corpHeight });
	    $this.find(".links").css({ marginTop: captionHeight });
	});
	
	$(".additional").css({width:extraWidth+"px"}); // Needed -> ,height:slideHeight+"px"
	
	$(".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').live("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();
	panelPlay();
}

function checkFlash() {
	if (flashTimeout) clearTimeout(flashTimeout);
	if (flashIsLoaded) {
	    
        var flash = FL('modelSelectorFlash');
	    
        flash.setGrade(chosenGrade);
	    flash.setBadge(chosenBadge);
	    flash.receiveData(dataSet);	
	}
	else {
		flashTimeout = setTimeout("checkFlash()",1000);	
	}
}

function setTo(id) {
	$('.engineTransmissionInfo').hide();
	$('.featuresList').hide();
	$('.featuresTitle').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();
}

