// NEW
if (!window.console || !console.firebug){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
	"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {}
}

var vehiclePrice = {
	pricePickup_f250: "$25,300", //enter the price for each vehicle
	pricePickup_f250xlt: "$28,835",
	pricePickup_f250fx4: "$39,200",
	pricePickup_f250lariat: "$35,150",
	pricePickup_f250king: "$41,090",
	pricePickup_f350: "$26,020",
	pricePickup_f450: "$44,145",
	priceChassisCabs_f350: "$26,450",
	priceChassisCabs_f450: "$29,975",
	priceChassisCabs_f550: "$32,965",
	priceCargoVans_e150: "$25,900",
	priceCargoVans_e250: "$26,935",
	priceCargoVans_e350: "$29,725",
	priceCutawayVans_e250: "$25,280",
	priceCutawayVans_e350: "$26,520",
	priceCutawayVans_e450: "$29,305",
	priceWagons_e150: "$28,165",
	priceWagons_e250: null,
	priceWagons_e350: "$31,365",
	priceWagons_e350el: "$33,755",
	priceLcf_l45: "$36,560",
	priceLcf_l55: "$40,685",
	priceMediumDuty_f650pl: "$57,179",
	priceMediumDuty_f650: "$57,405",
	priceMediumDuty_f750: "$58,685",
	priceTC_van: "$20,780",
	priceTC_wagon: "$21,135",
	priceStrippedChassis_e350: "$23,365",
	priceStrippedChassis_e450: "$25,940",
	priceStrippedChassis_f53: null
};
function updateVehiclePrices() {
	for(var vhcl in vehiclePrice) {
		if (!vehiclePrice[vhcl]) {
			var parent = $("span." + vhcl).parent()
			parent.html("<a href=\"/dealer.html\">Contact Dealer</a>");
			parent.find("a").click(function() {location.href=this.href;return false;});
		} else $("span." + vhcl).html(vehiclePrice[vhcl]);
	}
	/* This way requires a common class and a prefix before the vehicle
	$("span.vhclPrice").each(function(pos, elem) {
		var regex = /($|\s)vhclId_([^\s]+)/;
		if(regex.test(elem.className)) {
			var vhcl = regex.exec(elem.className)[2];
			elem.innerHTML = vehiclePrice[vhcl];
		}
	});
	*/
}

var mainNavConfig = {
pickups: "/includes/harder-working-nav/pickups.html", //enter the file name and path for the matching nav section
chassis: "/includes/harder-working-nav/chassis-cabs.html",
mediumDuty: "/includes/harder-working-nav/medium-duty.html",
lowCabForward: "/includes/harder-working-nav/low-cab-forward.html",
cargoVans: "/includes/harder-working-nav/cargo-vans.html",
cutawayVans: "/includes/harder-working-nav/cutaway-vans.html",
wagons: "/includes/harder-working-nav/wagons.html",
strippedChassis: "/includes/harder-working-nav/stripped-chassis.html",
TransitConnect: "/includes/harder-working-nav/tc.html"
};

function exceptionAlertHandler(req) {
	var msg;
	if (typeof req == "string") msg = req;
	else {
		var resp = req.responseText;
		try {
			var ex = eval("(" + resp + ")");
			if(ex) msg = ex.message;
			else msg = null;
		} catch(e) {
			msg = resp;
		}
	}
	if(!msg) msg = "An error has occured. Please try again later.";
	$(document).ready(function() {
		$("#alertContent").html(msg);
		$("#alertOverlay").jqmShow();
		// Enable buttons on forms if this error occurred during submission
		$(".submitting").removeClass("submitting");
	});
}

function navContentLoaded(){
	$(".selectChecks a.checkOption").toggle(
		function(){
			$(this).addClass("selectCompareChecked");
		},
		function(){
			$(this).removeClass("selectCompareChecked");
		}
	);
}

function setPage(){
	var pageSection = $("body").attr("class");
	console.log(pageSection);
	
	if (pageSection == "sectionHome"){
		$(".navHome").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionPickups"){
		$(".navPickups").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionChassisCabs"){
		$(".navChassisCabs").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionMediumDuty"){
		$(".navMediumDuty").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionLowCabForward"){
		$(".navLowCabForward").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionCargoVans"){
		$(".navCargoVans").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionCutawayVans"){
		$(".navCutawayVans").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionWagons"){
		$(".navWagons").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionStrippedChassis"){
		$(".navStrippedChassis").parent("li").addClass("mainNavCurrent");
	} else if (pageSection == "sectionTransitConnect"){
		$(".navTransitConnect").parent("li").addClass("mainNavCurrent");
	}
}

/* BEGIN OVERLAY CODE */
$(function(){
	updateVehiclePrices();
	
	setPage();

	$("head").append('<link rel="stylesheet" type="text/css" href="/css/mainNavOverlay.css"  media="all" />');
	$("body").append('<div id="fctNavModalBox" class="jqmNavWindow"><a href="javascript:;" class="jqmWindowNavClose">Close</a><div id="fctNavModalContent"></div></div>');
	$("body").append('<div id="alertOverlay" class="jqmAlertOverlay"><p class="alertContent" id="alertContent"></p><p style="text-align:center;"><a href="javascript:;" class="alertOverlayClose">Close</a></p></div>');

	if(document.getElementById("kttDescImages") || document.getElementById("kttDescImagesSingleEnlarge") || document.getElementById("kttPhotos")){
		$("body").append('<div id="imgLrgOverlay" class="jqmImgLrgOverlay"><div style="text-align:right; height: 26px;"><a href="javascript:;" class="imgLrgOverlayClose">Close</a></div><div><img src="" alt="Large Image" class="kttDescImageLrg"/></div></div>');

		$("#imgLrgOverlay").jqm({modal:false,overlayClass:"jqmImgLrgOverlayBkg"});

		$(".jqmImgLrgOverlay").click(function(){
			$("#imgLrgOverlay").jqmHide();
		});

		$(".kttDescImageMed").click(function(){
			
			$("#imgLrgOverlay").jqmShow();
		});
	}

	try {
		$("#fctNavModalBox").jqm({modal:true,overlayClass:"jqmNavOverlay"});
	} catch(e) {
		// Every once in awhile this errors out
		setTimeout(function() {
			$("#fctNavModalBox").jqm({modal:true,overlayClass:"jqmNavOverlay"});
		}, 500);
	}

	$("#alertOverlay").jqm({modal:true,overlayClass:"jqmAlertOverlayBkg"});
	$(".alertOverlayClose").click(function() {
		$("#alertOverlay").jqmHide();
		$("#alertContent").html("");
	});

	$.ajaxSetup({error:exceptionAlertHandler});

	$("#mainnav .navPickups").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.pickups);
			killKttVideo();
		//}
	});
	$("#mainnav .navChassisCabs").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.chassis);
			killKttVideo();
		//}
	});
	$("#mainnav .navMediumDuty").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.mediumDuty);
			killKttVideo();
		//}
	});
	$("#mainnav .navLowCabForward").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.lowCabForward);
			killKttVideo();
		//}
	});
	$("#mainnav .navCargoVans").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.cargoVans);
			killKttVideo();
		//}
	});
	$("#mainnav .navCutawayVans").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.cutawayVans);
			killKttVideo();
		//}
	});
	$("#mainnav .navWagons").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.wagons);
			killKttVideo();
		//}
	});
	$("#mainnav .navStrippedChassis").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.strippedChassis);
			killKttVideo();
		//}
	});
	$("#mainnav .navTransitConnect").click(function(){
		//if( !$(this).parent("li").hasClass("mainNavCurrent") ){
			$("#mainnav li").removeClass("mainNavCurrent");
			$(this).parent("li").addClass("mainNavCurrent");
			$("#fctNavModalBox").jqmShow();
			$("#fctNavModalBox #fctNavModalContent").load(mainNavConfig.TransitConnect);
			killKttVideo();
		//}
	});
	
	$("#zip").click(function(){
		$("#fctNavModalBox").jqmHide();
	});

	$(".jqmWindowNavClose").click(function(){
		$("#fctNavModalBox").jqmHide();
		$("#mainnav li").removeClass("mainNavCurrent");
		setPage();
	});

	$("#mainnav a:not(.navHome)").attr("href","javascript:;");

	if(document.getElementById("vehicles_bottom")){
		setTimeout(resizeVehicleBottom, 250);
	}

	$("input.clearDeafault").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$("#kttPhotos li img").click(function(){
		$(".kttDescImageMed").attr("src",$(this).attr("src").replace(/Small/, "Medium"));
	});

	$("#kttPhotos .kttDescImageMed").click(function(){
		$(".kttDescImageLrg").attr("src",$(this).attr("src").replace(/Medium/, "Large"));
	});

	$("#kttDescImages li img").click(function(){
		$(".kttDescImageLrg").attr("src","/images/spacer.gif");
		$(".kttDescImageMed").attr("src",$(this).attr("src").replace(/Small/, "Medium"));
	});

	$("#kttDescImages .kttDescImageMed").click(function(){
		$(".kttDescImageLrg").attr("src",$(this).attr("src").replace(/Medium/, "Large"));
	});

	$("#kttDescImagesSingleEnlarge .kttDescImageMed").click(function(){
		$(".kttDescImageLrg").attr("src",$(this).attr("src").replace(/Medium/, "Large"));
	});
});
//Others
function resizeVehicleBottom(){
	var prodDescHeight = $("#productdescription").height();
	
	if( prodDescHeight >= "330"){
		$("#vehicles_bottom").css("top", ((prodDescHeight - 330) + 407) + "px");
	} else if(document.getElementById("kttDescImages")){
		$("#vehicles_bottom").css("top", "");
	}
	
	if(document.getElementById("kttDescImages")){
		if(prodDescHeight < $("#kttDescImages").height()){
			$("#vehicles_bottom").css("top", (($("#kttDescImages").height() - 275) + 407) + "px");
		}
	}
}

function validateCompareSelections(){
	url = "/compare.html?";
	var checkBoxes = $(".selectCompareChecked");
	if( checkBoxes.length < 2 ) {
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}else{
		for(i=0; i < checkBoxes.length; i++){
			url += "&modelYearId" + i + "=" + checkBoxes[i].id;
			if( $( checkBoxes[i] ).hasClass("proloader") ){
				url += "&preselect" + i + "=Pro_Loader";
			}
		}
		location.href=url;
	}
}

//LCFs
function validateTrimLevelCompareSelections(){
	url = "/compare.html?";
	var checkBoxes = $(".selectCompareChecked");
	if( checkBoxes.length < 2 ){
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}else{
		for(i=0; i < checkBoxes.length; i++){
			url += "&aCode" + i + "=" + checkBoxes[i].id;
		}
		location.href=url;
	}
}
//Cargo/Wagon
function validatePreselectCompareSelections(bodyStyle){
	url = "/compare.html?";
	if( $(".selectCompareChecked").length < 2 ){
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}else{
		for(i=0; i < $(".selectCompareChecked").length; i++){
			url += "&modelYearId" + i + "=" + $(".selectCompareChecked")[i].id + "&preselect" + i + "=" + bodyStyle;
		}
		location.href=url;
	}
}

function validateAcodePreselectCompareSelections(){
	url = "/compare.html?";
	if( $(".selectCompareChecked").length < 2 ){
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}else{
		for(i=0; i < $(".selectCompareChecked").length; i++){
			url += "&aCode" + i + "=" + $(".selectCompareChecked")[i].id + "&preselect" + i + "=" + ((i == 0)?"Cargo":"Wagon");
		}
		location.href=url;
	}
}
function validateMediumDutyCompareSelections(proloader){
	url = "/compare.html?";
	if( $(".selectCompareChecked").length < 2 ) {
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}else{
		for(i=0; i < $(".selectCompareChecked").length; i++){
			url += "&modelYearId" + i + "=" + $(".selectCompareChecked")[i].id + "&preselect" + i + "=" + proloader;
		}
		location.href=url;
	}
}


//Harder working nav trim-level entires
function trimDetail() {
	aCode = "";
	modelName = "";
	trimName = "";
	price = "";
	image = "";
}
var trimLevelCompareDetails = new Array();

lcfL45 = new trimDetail();
lcfL45.aCode = "USB90FOM401A0";
lcfL45.modelName = "Low Cab Forward";
lcfL45.trimName = "L45";
lcfL45.price = "$36,560";
lcfL45.image = "EVD|USB80FOM401A0101.jpg|E4";
trimLevelCompareDetails["USB90FOM401A0"] = lcfL45;

lcfL55 = new trimDetail();
lcfL55.aCode = "USB90FOM402A0";
lcfL55.modelName = "Low Cab Forward";
lcfL55.trimName = "L55";
lcfL55.price = "$40,685";
lcfL55.image = "EVD|USB80FOM401A0101.jpg|E4";
trimLevelCompareDetails["USB90FOM402A0"] = lcfL55;

tc_cargo = new trimDetail();
tc_cargo.aCode = "USC00FOV321A0";
tc_cargo.modelName = "Transit Connect";
tc_cargo.trimName = "Cargo XL";
tc_cargo.price = "$20,780";
tc_cargo.image = "compare/vehicles/tc_van.png";
trimLevelCompareDetails["USC00FOV321A0"] = tc_cargo;

tc_wagon = new trimDetail();
tc_wagon.aCode = "USC00FOV322A0";
tc_wagon.modelName = "Transit Connect";
tc_wagon.trimName = "Wagon XL";
tc_wagon.price = "$21,135";
tc_wagon.image = "compare/vehicles/tc_wagon.png";
trimLevelCompareDetails["USC00FOV322A0"] = tc_wagon;

f650pl = new trimDetail();
f650pl.acode = "USC00FOM011A0";
f650pl.modelName="F-650";
f650pl.trimName ="Pro Loader";
f650pl.price = "$57,179";
f650pl.image = "/images/compare/vehicles/f650pl.png";
trimLevelCompareDetails["USC00FOM011A0"] = f650pl;

f650 = new trimDetail();
f650.acode = "USC00FOM101A0";
f650.modelName="F-650";
f650.trimName ="";
f650.price = "$57,405";
f650.image = "/images/compare/vehicles/f650.png";
trimLevelCompareDetails["USC00FOM101A0"] = f650;

f750 = new trimDetail();
f750.acode = "USC00FOM201A0";
f750.modelName="F-750";
f750.trimName ="";
f750.price = "$58,685";
f750.image = "/images/compare/vehicles/f750.png";
trimLevelCompareDetails["USC00FOM201A0"] = f750;

e150_wagon = new trimDetail();
e150_wagon.acode = "USB90FOV172A0";
e150_wagon.modelName="E-150";
e150_wagon.trimName ="";
e150_wagon.price = "$28,165";
e150_wagon.image = "/images/compare/vehicles/e150w.png";
trimLevelCompareDetails["USB90FOV172A0"] = e150_wagon;

e350_wagon = new trimDetail();
e350_wagon.acode = "USB90FOV233A0";
e350_wagon.modelName="E-350";
e350_wagon.trimName ="";
e350_wagon.price = "$31,365";
e350_wagon.image = "/images/compare/vehicles/e350w.png";
trimLevelCompareDetails["USB90FOV233A0"] = e350_wagon;

e350el_wagon = new trimDetail();
e350el_wagon.acode = "USB90FOV234A0";
e350el_wagon.modelName="E-350";
e350el_wagon.trimName ="EL";
e350el_wagon.price = "$33,755";
e350el_wagon.image = "/images/compare/vehicles/e350elw.png";
trimLevelCompareDetails["USB90FOV234A0"] = e350el_wagon;
/*
Vehicles pages functions
*/
URL_COMPARE = "/compare.html";

function compareLinkClick(){

	var inputModels = $("input:checked[type='checkbox'][name^='chkVehicleModel_']");
	url = URL_COMPARE + "?";

	if( inputModels.length > 1 ){
		for(var i=0; i < inputModels.length; i++){
			url += "&modelYearId" + i + "=" + inputModels[i].id;
			var attributes = inputModels[i].attributes;
			for(j=0; j<attributes.length; j++)
			{
				if(attributes[j].nodeName == "preselect")
				url += "&preselect"+i+"="+attributes[j].nodeValue;
			}
		}
 		location.href=url;
	}else{
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}
}

function compareTrimLinkClick(){

	var inputModels = $("input:checked[type='checkbox'][name^='chkVehicleModel_']");
	url = URL_COMPARE + "?";

	if( inputModels.length > 1 ){
		for(var i=0; i < inputModels.length; i++){
			url += "&aCode" + i + "=" + inputModels[i].id;
		}
		location.href=url;
	}else{
		$("#alertContent").html("Please select at least two vehicles to compare");
		$("#alertOverlay").jqmShow();
	}
}

function getFullyQualifiedUrl(url) {
	if(/^\//.test(url)) url = location.href.replace(/(^http?:\/\/.*?)\/.*/, "$1" + url);
	else url = location.href.replace(/(^http?:\/\/.*\/).*/, "$1" + url);
	return url;
}

function secure(url) {
	// TODO Remove the following line to enable SSL
	return unsecure(url);
	if(/^https?:/.test(url)) url = url.replace(/^http:/, "https:");
	else url = secure(getFullyQualifiedUrl(url));
	return url;
}

function unsecure(url) {
	if(/^https?:/.test(url)) url = url.replace(/^https:/, "http:");
	else url = unsecure(getFullyQualifiedUrl(url));
	return url;
}

// Ensures links with the "SSL" class get the link converted to use SSL
$(document).ready(function() {
	$("a.ssl").each(function(pos, elem) {elem.href=secure(elem.href);});
	try {
		var url = new Url(location.href);
		if(/^https/.test(url.protocol)) $("a[class!=ssl").each(function(pos, elem) {elem.href=unsecure(elem.href);});
	} catch(e) {}
});

function killKttVideo()
{
	var children = $("#kttVideo > *");
	if(children && children.length > 0)
	{
		$("#kttVideo > *").remove();
	}
}