// Used to track scrolling of carousel on mouse enter/leave of arrows
var carouselTimer = null;

// // This is a shortcut utility function for initializing the vehicle carousel.
// A little extra work is needed to correct some bugs in the jcarousellite plugin. First, the width of the whole carousel has to be set explciitly
// because "740px" was hard-coded in the plugin, specifically for another carousel, I think. Second, there is a bug in how the plugin defines the height
// of each carousel item, where it's probably assuming there all the same height. The height of vehicle items varies, so we need to find and record the
// tallest item height, and set all of the items to that after the plugin screws up.
// Configurable jcarousellite options are visible, scroll and speed (see http://www.gmarwaha.com/jquery/jcarousellite/#doc for details).
function initVehicleCarousel(o) {
	var o = $.extend({ visible: 5, scroll: 1, speed: 400 }, o || {});
	var maxHeight = 0;

	// Loop over items for the carousel and find height of the tallest carousel item
	$("#veh-carousel ul li").each(function () { if ($(this).height() > maxHeight) maxHeight = $(this).height();	});

	// Initialize the carousel, fix carousel width, and fix the items' heights
	$("#veh-carousel").jCarouselLite({
		circular: true,
		visible: o.visible,
		scroll: o.scroll,
		speed: o.speed,
		btnNext: "#veh-next",
		btnPrev: "#veh-prev"
	}).css("width", "930px").find("li").css({height: maxHeight});

	// When you hover over the prev/next arrows, move the carousel on a timer
	// Note that clicking still works, too!
	// Also fix the height of the buttons to match the carousel
	// We don't bind in the case of tablets because mouse-type events are not very well supported
	$("#veh-prev, #veh-next").css({height: $("#veh-carousel").outerHeight()});
	if ( !(typeof window.onorientationchange != "undefined" && 'ontouchstart' in window) ) {
		$("#veh-prev, #veh-next").mouseenter(function() {
			$(this).addClass("hover");
			window.clearInterval(carouselTimer);
			$("#" + $(this).attr("id")).trigger("click");
			if ($(this).attr("id") == "veh-prev") carouselTimer = setInterval(function() { $("#veh-prev").trigger("click"); }, 900);
			else carouselTimer = setInterval(function() { $("#veh-next").trigger("click"); }, 900);
		}).mouseleave(function() {
			$(this).removeClass("hover");
			window.clearInterval(carouselTimer);
		});
	}

}

$(document).ready(function() {

	loadFlash();

	// Show lightbox for PC/CDA links in carousel
	$("div#veh-cda a").click(function () {
		checkJQueryUIDialogloaded();
		return false;
	});

	// pc cookie functionality
	var myPC = getCookie("pc");
	if (typeof myPC == "undefined" || myPC == null || myPC.length == 0 || myPC.toLowerCase() == "null") {
		var thisURL = location.toString();
		if (thisURL.indexOf('french') != -1) $('#pc').val("CODE POSTAL");
		else $('#pc').val("POSTAL CODE");
	} else {
		$('#pc').val(myPC);
	}

	if (( $('#pc').val()) != "POSTAL CODE" && ( $('#pc').val()) != "CODE POSTAL" && ( $('#pc').val()) != "")  {
		/*alert ("we have a cookie!")*/
	} else {
		getVal();
	}

	$("#pc").change(getVal);
	$("#pc").blur(getVal);

	function getVal() {
		if (validatePC($('#pc').val())) {
			var today = new Date();
			today.setTime(today.getTime());
			var one_day = 1000 * 60 * 60 * 24
			var cookieExpiresDate = new Date (today.getTime() + (30 * one_day));
			
			$('#pc').val($('#pc').val().replace(/ /,"").toUpperCase());
			var homeCook = $('#pc').val();
			setCookie("pc", homeCook, cookieExpiresDate, "/");
			$(this).removeClass("error");
		} else {
			$(this).addClass("error");
		}
	}

	function validatePC(el) {
		if (( $('#pc').val()) != "POSTAL CODE" && ( $('#pc').val()) != "CODE POSTAL" && ( $('#pc').val()) != "") {
			var pcRegExp = /(^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$)/;
			if (pcRegExp.test(el)) {
				return true;
			} else {
				// do nothing
				return false;
			}
		}
	}


	$("#pc").click(function() {
		getText = $("#pc").val();
		if ((getText == "POSTAL CODE") || (getText == "CODE POSTAL")) {
			$("#pc").val("");
			$("#pc").attr('maxlength',6);
		}
	});

	// On states for feature tabs (other than highlights, which is taken care of inside carousel)
	$("#feattabs li:not(.sub) a").click(function(){
		//close global nav if open
		if($("#GlobalNavTab").css("top") == "35px") toggleTab();

		$("#feattabs li:not(.sub) a").removeClass("on").parent("li").removeClass("on");
		$(this).addClass("on").parent("li").addClass("on");
		//$("#feattabs li.sub a").animate({opacity:"0.1"},400);
		$("#feattabs li.sub a").addClass("disabled");//.parent().hide();
		$("#feattabs li#first").siblings("li.sub");
		var myIndex = $("#feattabs li:not(.sub)").index($(this).parent("li"));
		$("#featContent > div").hide();
		$("#featContent > div").eq(myIndex).show();
		return false;
	});

	$("#feattabs li.sub a, #feattabs li#first a ").click(function(){
		//close global nav if open
		if($("#GlobalNavTab").css("top") == "35px") toggleTab();

		//$("#feattabs li.sub a").animate({opacity:"1"},100);
		$("#feattabs li:not(.sub)").removeClass("on").children("a").removeClass("on");
		$("#feattabs li.sub a").removeClass("disabled");//.parent().show();
		$("#feattabs li#first a").addClass("on").parent("li").addClass("on");
		$("#featContent > div").hide();
		$("#featContent > div").eq(0).show();
	});

    $('#ft_prev, #ft_next').hover(function() {
       $(this).addClass('over');
    }, function() {
       $(this).removeClass('over');
    });

	$("#footer ul.social a").attr("target", "_blank");

	// Load the "New Vehicle Offers" link in the footer in a pop-up window
	// partial port of openCurrentOffers() from /js/common/global_nav.js
	$("#footer a[href*='/current-offers/']").click(function () {
		var w = 300;
    var h = 434;
    var bw, bh, bl, bt, topPos, leftPos;
		if (document.all) {
			bw = screen.width;
			bh = screen.height;
			bl = 0;
			bt = 0;
		} else {
			bw = window.outerWidth;
			bh = window.outerHeight;
			bl = window.screenX;
			bt = window.screenY;
		}
		leftPos = Math.floor((bw - w) / 2) + bl;
		topPos = Math.floor((bh - h) / 2) + bt;
		var popWindow = window.open($(this).attr("href"), "CurrentOffers", "toolbar=0, status=0, width=" + w + "px, height=" + h + "px, top=" + topPos + "px, left=" + leftPos + "px");
    popWindow.focus();
		return false;
	});

});
