/*
	Copyright DTDigital         :: www.dtdigital.com.au ::
	Unauthorised modification / use is a criminal offence, and
	will be prosecuted to the fullest extent permitted by law.
	All Rights Reserved
*/




// jQuery functions that run on "dom ready"

var _jQueryLoaded = (typeof(jQuery) == "function");

if (_jQueryLoaded)
{
	$(document).ready(function()
	{
		InitRolloverNavigation();
		InitRolloverInputs();
		
		// Price filter found on range & pricing / specs pages
		PriceFilterer();
		
		// Makes links open in new window when they have rel=external
		$("a[@rel=external]").attr("target", "_blank");
		// Use below method if using thickbox, matches class rather than rel
		$("a.external-link").attr("target", "_blank");
		
		// External dealer hiding elements when query string is present
		var hideThese = $(".noshow");
		var showThese = $(".show");
		var changeThese = $(".textchange")
		var queryString = location.search.substring(1, location.search.length);
		if(queryString.indexOf("show=false") != -1)
		{
			$.each(hideThese,
			function()
				{
					$(this).css({ display: "none"});
				}
			);
			$("a").each(
				function()
				{
					var currentLink = $(this).attr("href");
					$(this).attr("href", currentLink + "?show=false");
				}
			);			
			$.each(showThese,
			function()
				{
					$(this).css({ display: "block"});
				}
			);
			$.each(changeThese,
			function()
				{
					$(this).text("Range &amp; Specifications");
				}
			);
						
		}
	});
}

var _RolloverNavigationLoaded = false;
function InitRolloverNavigation()
{
	if (_jQueryLoaded && !_RolloverNavigationLoaded)
	{
		$(".rollover-navigation a").each(function()
		{
			$(this).find("img.navitem_prejs").each(function()
			{
				$(this).attr("class", "navitem_off");
			
				var newImage = $(this).clone();
		
				var newImageSrc = newImage.attr("src").replace("_0.gif", "_1.gif");
				newImage.attr("src", newImageSrc);
		
				newImage.attr("class", "navitem_hover");
		
				newImage.insertAfter(this);
			});
			
			$(this).focus(
				function()
				{
					$(this).toggleClass("hover");
				}
			).blur(
				function()
				{
					$(this).toggleClass("hover");
				}
			);
		});
		
		_RolloverNavigationLoaded = true;
	}
}

function InitRolloverInputs()
{
	if (_jQueryLoaded)
	{
		$(".rollover-input").each(function()
		{
			var imageSrc = $(this).attr("src");
			var imageName = imageSrc.substring(imageSrc.lastIndexOf("swap_") + 5, imageSrc.lastIndexOf("_"));
			
			for (var preloadLoop=0; preloadLoop < 3; preloadLoop++)
			{
				if (eval("typeof(b" + imageName + preloadLoop + ")") != "object")
				{
					eval("b" + imageName + preloadLoop + " = new Image()");
					eval("b" + imageName + preloadLoop + ".src = 'assets/swap_" + imageName + "_" + preloadLoop + ".gif'");
				}
			}
			
			$(this).mouseover(function()
			{
				$(this).attr("src", eval("b" + imageName + "1.src"));
			});
			
			$(this).mousedown(function()
			{
				$(this).attr("src", eval("b" + imageName + "2.src"));
			});
			
			$(this).mouseout(function()
			{
				$(this).attr("src", eval("b" + imageName + "0.src"));
			});

		});
	}
}

function PriceFilterer()
{
	$(".custom-select-container02").click(function(e){
		var tag = e.target;
		
		if(tag.nodeName === "SPAN")
		{
			var StateValue = tag.firstChild.nodeValue;
			
			$("#Pricing .multi-column06:visible").hide();
			
			$("#" + StateValue + "_Prices").toggle();
			if(typeof sIFR === "object"){
				// font replacement won't happen onload to hidden items
				// Do replacement on now visible headings
				sIFR.replace(kievitbold, {
					selector: 'h2.subtitle05a'
					,transparent: true
					,css: ['.sIFR-root { color: #1a5269; }']
				});
			}
		}
	});
}

function popup(mylink, windowname) {
    if (typeof (mylink) == 'string') {
        href = mylink;
    } else { href = mylink.href; }

    params = 'width=' + screen.width;
    params += ', height=' + screen.height;
    params += ', top=0, left=0, toolbar=0,location=0,statusbar=0,menubar=0,resizable=0,scrollbars=0'
    params += ', fullscreen=yes,';

    popupWindow = window.open(href, windowname, params);
    if (window.focus) { popupWindow.focus() }
    return false

}

function isValid() {
	/* Client side validation, highlight fields */
	if(Page_ClientValidate() == false) {
		$.each(Page_Validators,function(index) {
			var currentItem = "#" + $(Page_Validators[index]).attr("id");
			
			var findChildSpan = $(currentItem).parents(".field01").find(".error").size();
			
			if(findChildSpan == 1){
				if(Page_Validators[index].isvalid == false) {
					$(currentItem).parents(".field01").addClass("field-error01");
				} else {
					$(currentItem).parents(".field01").removeClass("field-error01");
				}
			} else {
				var firstErrorSpan = $(this).parent().find("span.error:first").css("display");
				var secondErrorSpan = $(this).parent().find("span.error:last").css("display");
				if(firstErrorSpan == "inline" || secondErrorSpan == "inline"){
					$(currentItem).parents(".field01").addClass("field-error01");
				} else {
					$(currentItem).parents(".field01").removeClass("field-error01");
				}
			}

			
		});
	}
	
}

function numbersOnlyInput() {
	$(".input-bg01 input").keypress(function(e) {
		//if the letter is not digit then display error and don't type anything
		if (e.which != 8 && e.which != 0 && e.which != 13 && (e.which < 48 || e.which > 57)) {
			return false;
		}
	});
}