var currentMake = -1;
var currentModel = -1;
var timeToShow = 300;

preloadImages = new Array();
preloadImages = ["/images/mainMenu/nav_01_off.png","/images/mainMenu/nav_01_on.png","/images/mainMenu/nav_02_off.png","/images/mainMenu/nav_02_on.png","/images/mainMenu/nav_03_off.png","/images/mainMenu/nav_03_on.png","/images/mainMenu/nav_04_off.png","/images/mainMenu/nav_04_on.png","/images/mainMenu/nav_05_off.png",
                 "/images/mainMenu/nav_05_on.png","/images/mainMenu/collisionSubMenuBG.png","/images/mainMenu/linkAllVehicles.png","/images/mainMenu/linkAllVehiclesOn.png","/images/mainMenu/linkBuick.png","/images/mainMenu/linkBuickOn.png","/images/mainMenu/linkCadillac.png","/images/mainMenu/linkCadillacOn.png",
                 "/images/mainMenu/linkChevrolet.png","/images/mainMenu/linkChevroletOn.png","/images/mainMenu/linkEngineComponents.png","/images/mainMenu/linkEngineComponentsOn.png","/images/mainMenu/linkEngines.png","/images/mainMenu/linkEnginesOn.png","/images/mainMenu/linkGMC.png","/images/mainMenu/linkGMCOn.png",
                 "/images/mainMenu/linkHummer.png","/images/mainMenu/linkHummerOn.png","/images/mainMenu/linkMarketingMaterials.png","/images/mainMenu/linkMarketingMaterialsOn.png","/images/mainMenu/linkOldsmobile.png","/images/mainMenu/linkOldsmobileOn.png","/images/mainMenu/linkPaintShop.png","/images/mainMenu/linkPaintShopOn.png",
                 "/images/mainMenu/linkPontiac.png","/images/mainMenu/linkPontiacOn.png","/images/mainMenu/linkPositionStatements.png","/images/mainMenu/linkPositionStatementsOn.png","/images/mainMenu/linkSaturn.png","/images/mainMenu/linkSaturnOn.png","/images/mainMenu/linkServiceBulletin.png","/images/mainMenu/linkServiceBulletinOn.png",
                 "/images/mainMenu/linkTechnicalDocuments.png","/images/mainMenu/linkTechnicalDocumentsOn.png","/images/mainMenu/linkTransferCases.png","/images/mainMenu/linkTransferCasesOn.png","/images/mainMenu/linkTransmissionComponents.png","/images/mainMenu/linkTransmissionComponentsOn.png","/images/mainMenu/linkTransmissions.png",
                 "/images/mainMenu/linkTransmissionsOn.png","/images/mainMenu/linkVinInfo.png","/images/mainMenu/linkVinInfoOn.png","/images/mainMenu/linkWarrantyInfo.png","/images/mainMenu/linkWarrantyInfoOn.png"];

loadedImages = new Array();

for (loadingImage in preloadImages) {
	loadedImages[loadingImage] = new Image();
	loadedImages[loadingImage].src = preloadImages[loadingImage];
}


$(document).ready(function(){
    $(".option").hover(function(){
    	$("." + this.id + "Btn").removeClass(this.id + "Option").addClass(this.id + "OptionOn");
    	$("#" + this.id + "SubMenu").show();
    }, function() {
    	$("." + this.id + "Btn").removeClass(this.id + "OptionOn").addClass(this.id + "Option");
    	$("#" + this.id + "SubMenu").hide();
    });
    $(".subMenuOption").hover(function(object){
    	$(this).removeClass(this.id).addClass(this.id + "On");
    }, function() {
    	$(this).removeClass(this.id + "On").addClass(this.id);
    });
    $(".subMenuOption").click(function(){
    	$(this).parent().parent().hide();
    });
    if(getCookie("acceptTerms") != "true") {
    	
		showTermsPopUp();
    }
    
    if (globalNavSection != "") {
    	$("#" + globalNavSection + "Option").addClass("selected");
    }
});

function getFile(methodType, url, dataType) {
	var fileData = "";
	$.ajax({
        type: methodType,
        url: url,
        dataType: dataType,
        async: false,
        success: function(data) {
			fileData = data;
		}
	});
	
	return fileData;
}

function getXmlTagDataByAttr(xml, tagName, attrId, attrValue) {
	var tagData = "";
	$(xml).find(tagName).each(function() {
		if ($(this).attr(attrId) == attrValue) {
			tagData = $(this);
		}
	});
	return tagData;
}

function acceptTerms() {
	setCookie("acceptTerms", "true");
	clickTrack("termsPopUpAccept");
	hideTermsPopUp();
	
}


function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)	{
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function showTermsPopUp(){
	$("#termsPopUp").fadeIn(400);
}

function hideTermsPopUp(){
	$("#termsPopUp").fadeOut(400);
}




function nextMake() {
	if (pdfsInfo[currentMake + 1] != null)
		changeMake(currentMake + 1);
}

function prevMake() {
	if (currentMake > 0)
		changeMake(currentMake - 1);
}

function changeMake(idMake) {
	$(".comboRIOption").removeClass("comboRIOptionSelected");
	$("#comboRIMakeText").html(pdfsInfo[idMake].id);
	currentMake = idMake;
	$("#comboRIModelText").html(pdfsInfo[idMake].childs[0].id);
	currentModel = 0;
	loadModels(idMake);

	$.each($("#comboRIMakeOptions > .comboRIOption"), function(i, element) {
		if (i == idMake) {
			$(element).addClass("comboRIOptionSelected");
		}
	});
}

function loadMakes() {
	var htmlCode = "";
	for (pdfInfo in pdfsInfo) {
		htmlCode += "<p class='comboRIOption' onClick='changeMake(" + pdfInfo + ")'>" + pdfsInfo[pdfInfo].id + "</p>";
	}
	$("#comboRIMakeOptions").html(htmlCode);
}

function nextModel() {
	if (pdfsInfo[currentMake].childs[currentModel + 1] != null)
		changeModel(currentModel + 1);
}

function prevModel() {
	if (currentMake > -1 && currentModel > 0)
		changeModel(currentModel - 1);
}

function changeModel(idModel) {
	$("#comboRIModelOptions > .comboRIOption").removeClass("comboRIOptionSelected");
	$("#comboRIModelText").html(pdfsInfo[currentMake].childs[idModel].id);
	currentModel = idModel;
	
	$.each($("#comboRIModelOptions > .comboRIOption"), function(i, element) {
		if (i == idModel) {
			$(element).addClass("comboRIOptionSelected");
		}
	});
}

function loadModels(idMake) {
	var htmlCode = "";
	for (pdfInfo in pdfsInfo[idMake].childs) {
		htmlCode += "<p class='comboRIOption' onClick='changeModel(" + pdfInfo + ")'>" + pdfsInfo[idMake].childs[pdfInfo].id + "</p>";
	}
	$("#comboRIModelOptions").html(htmlCode);
	
	$("#comboRIModelOptions > .comboRIOption").first().addClass("comboRIOptionSelected");
	
	$(".comboRIOption").hover(function() {
		$(this).addClass("comboRIOptionOn");
	}, function() {
		$(this).removeClass("comboRIOptionOn");
	});
}

function loadByAjax(type, url, data, success) {
	$.ajax({
		   type: type,
		   url: url,
		   data: data,
		   success: success
	});
}

function alignElementsVertically(elementsId) {
	$.each($(elementsId), function() {
		var elementHeight = $(this).height();
		var parentHeight = $(this).parent().height();
		var space = parentHeight - elementHeight;
		
		$(this).css("top", Math.round(space/2));
	});
}

function alignElementsHorizontally(elementsId) {
	$.each($(elementsId), function() {
		var elementWidth = $(this).width();
		var parentWidth = $(this).parent().width();
		var space = parentWidth - elementWidth;
		
		$(this).css("left", Math.round(space/2));
	});
}

function showVehiclePdfs() {
	if (currentMake >  -1 && currentModel > -1) {
		document.location.href = "/ShowCollisionVehicle.do?make=" + $("#comboRIMakeText").text() + "&model=" + $("#comboRIModelText").text();
	}
}

function showVehiclePdfsByModel(modelId) {
	currentModel = modelId;
	showVehiclePdfs();
}

function showFlashVideo(url) {
	var flashVideoHTML = "<object width='486' height='412'>" +
		"<param name='movie' value='" + url + "'>" +
		"<embed src='" + url + "' width='550' height='400'>" +
		"</embed></object>";
	
	$("#flashVideoPopUp").fadeIn(0, function(){
		$("#flashVideo").html(flashVideoHTML);
	});
	
}

function closeFlashVideo() {
	$("#flashVideo").html("");
	$("#flashVideoPopUp").fadeOut(0);
}
