var killable;
var cur_color_section = "";
var cur_color = "";
var cur_add = "";
var label_holder = "";
var cur_section = "models";
var firstPageTrackComplete = false;
var vidPlayerID = '';

//onload_register('updateModelFormLinks()');

function overColor(color, label){
	if(color != cur_color && color != cur_add){
		label_holder = $(label).innerHTML;
		atState(color, label); 
	}
}
function outColor(color, label){
	if(color != cur_color && color != cur_add){
		$(color+"_img").removeClass('at');
		$(label).setHTML(label_holder);
	}
}
function atState(color, label){
	$(color+"_img").addClass('at');
	$(label).setHTML($(color+"_img").getAttribute('alt'));
}
function setColor(color, label){
	resetChips(included_colors[cur_color_section]["add"]);
	if($(cur_color+"_img")){$(cur_color+"_img").removeClass('at');}
	atState(color, label);
	cur_color = color;
	for(i in included_colors[cur_color_section][color]){
		if(included_colors[cur_color_section][color][i] == true){
			$(i+"_img").setStyle('display', 'block');
		}
	}
}
function setAdd(color, label){
	//resetChips(included_colors[cur_color_section]);
	if($(cur_add+"_img")){$(cur_add+"_img").removeClass('at');}
	atState(color, label);
	cur_add = color;
	for(i in included_colors[cur_color_section]["add"][color]){
		if(included_colors[cur_color_section]["add"][color][i] == true){
			$(i+"_img").setStyle('display', 'block');
		}
	}
}
function resetChips(color_object){
	if(typeof(color_object) != "undefined"){
		for(i in color_object){
			if(typeof(color_object[i]) == 'object' && i != "add"){
				$(i+"_img").setStyle('display', 'none');
			}
		}
	}
}
function swapModelPicture(){
	if(cur_color_section == "exterior"){
		var temp_path = "ext/colors";
		var temp_add = "ext/wheels";
	}else if(cur_color_section == "interior"){
		var temp_path = "int/fabrics";
	}
	curVehicle = curVehicle.toLowerCase();
	var c_path = "/" + curVehicle + "/img/models/" + temp_path + "/" + model_cur_trim + "_" + cur_color +".png";
	window.document["model_image_flash"].SetVariable('color', c_path);
	//check if the additional option is a valid one, if not, use the first option available for that color
	if(typeof(included_colors[cur_color_section][cur_color][cur_add]) == 'undefined'){
		cur_add = "";
		for(i in included_colors[cur_color_section][cur_color]){
			if(typeof(included_colors[cur_color_section][cur_color][i]) == 'boolean'){
				cur_add = i;
				break;
			}
		}
		if(cur_add != ''){setAdd( cur_add, $( $(cur_color_section+"_colors").getLast() ).getFirst().id)};
	}
	if(cur_add != ""){
		var w_path = "/" + curVehicle + "/img/models/" + temp_add + "/" + model_cur_trim + "_" + cur_add +".png";
		window.document["model_image_flash"].SetVariable('wheel', w_path);
	}else{
		window.document["model_image_flash"].SetVariable('wheel', '');
	}
	window.document["model_image_flash"].change();
}
function swapSection(id){
	if($(cur_color_section+"_colors")){
		$(cur_color_section+"_colors").addClass('off');
		$(cur_color_section+"_section_btn").removeClass('section_at');
	}
	$(id+"_section_btn").addClass('section_at');
	$(id+"_colors").removeClass('off');
	cur_color_section = id;
	if(cur_color != ""){$(cur_color+"_img").removeClass('at');}
	if(cur_add != ""){$(cur_add+"_img").removeClass('at');}
	cur_color = "";
	cur_add = "";
	var set1;
	for(i in included_colors[id]){
		if( (typeof(included_colors[id][i]) == 'object' || typeof(included_colors[id][i]) == 'boolean') && i != "add"){
			set1 = i
			break;
		}
	}
	var set1_label = $( $(id+"_colors").getFirst() ).getFirst().id;
	var set2;
	if(typeof(included_colors[id][set1]) == "object"){
		for(i in included_colors[id][set1]){
			if(typeof(included_colors[id][set1][i]) == 'boolean'){
				set2 = i;
				break;
			}
		}	
	}
	var set2_label = $( $(id+"_colors").getLast() ).getFirst().id;
	setColor( set1, set1_label);
	if(typeof(set2) != 'undefined'){setAdd( set2, set2_label);}
	if(!firstPageTrackComplete){
		linkTrack("colorizer_models_default", id);
	}else{
		pageTrack(id + "_colorizer", "/" + id + "_colorizer");
	}
	firstPageTrackComplete = true; // stopping double click tracking.
	swapModelPicture();
}
function setEmbedLoaded(){
	swapSection(default_section);
	if($('color_categories').getChildren().length > 1) $('color_categories').setStyle('visibility', 'visible');
	
	//Special case for the overlay video function
	if(vidPlayerID != ''){
		if(vidPlayerID != 'off'){
			embedLoaded = true;
			setJSON();
		}
	}
}
function setCurrentModel(){
	if($(model_cur_trim+"_link")){
		$(model_cur_trim+"_link").addClass("current_model_at");
	}
}
function showModelColorizer(){
	$('model_image').setHTML("<img src='/" + curVehicle + "/img/models/ext/colors/" + model_cur_trim + "_static.jpg' />");
	assetId = "/" + curVehicle + "/img/models/ext/colors/" + model_cur_trim + "_static.jpg";
	showFlash("/flash/content/colorizer.swf","model_image","")
}
/*
//update lpos value within name attribute in 'get a quote' link on models & colors page
function updateModelFormLinks() {
	//Get A Quote form on models page
	if($('form_model_get_quote')){
		var GAQForm = $("form_model_get_quote");
		var GAQAnchors = GAQForm.getElementsByTagName('a');
		for(var i=0; i<GAQAnchors.length; i++) {
			if(GAQAnchors[i].className == "form_button") {
				GAQAnchors[i].attributes.name.value = "&lpos=content&lid=model_get_quote_form_submit_button";
			}
		}
	}
	
	//Search Inventory form on models page
	if($('form_model_inventory')){
		var InvForm = $("form_model_inventory");
		var InvAnchors = InvForm.getElementsByTagName('a');
		for(var i=0; i<InvAnchors.length; i++) {
			if(InvAnchors[i].className == "form_button") {
				InvAnchors[i].attributes.name.value = "&lpos=content&lid=model_inventory_form_submit_button";
			}
		}
		//var dealer_wrapper = InvForm.getNext().getNext();
		//alert(dealer_wrapper.hasClass('pfdealer_wrapper'));
	}
	
	//add tracking to MPG link on models page
	if($('model_mpg_link')){
		var mpg_div = $("model_mpg_link");
		var MPGAnchors = mpg_div.getElementsByTagName('a');
		
		for(var i=0; i<MPGAnchors.length; i++) {
			MPGAnchors[i].setAttribute('name', '&lpos=content&lid=mpg');
		}
	}
}
*/

