/*
	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();
		
		// 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"); 
		$("input:hidden").hide(); // FF shows them sometimes?
	});
}

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 < 2; 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).mouseout(function()
			{
				$(this).attr("src", eval("b" + imageName + "0.src"));
			});

		});
	}
}

$(function() {
	$("#search-keywords, #login-username, #newsletter-name, #newsletter-email").defaultvalue("Product Search", "Username", "Name", "Email");
	
	// hide password box and show textbox
	if ($("#login-password").length > 0) {
		$("#login-password").hide();
		$('<input type="text" value="Password" id="login-password-text" />').insertAfter("#login-password");
		$("#login-password-text").attr("class", $("#login-password").attr("class"));
		$("#login-password-text").attr("style", $("#login-password").attr("style")).show();
		$("#login-password").blur(pass1);
		$("#login-password-text").focus(pass2);
	}

});


function pass1() {
	if ($("#login-password").val() == "") {
		$("#login-password").next().show();
		$("#login-password").hide();
	}
}

function pass2() {
	$("#login-password").show().focus();
	$("#login-password-text").hide();
}

//Displays Javascript functionalities
$(function() {	
	$("a.showoptions-title, a.showall-title").css("display", "block");		   
});


//Product Tabs - display/hide product content
$(function() {						
		   
	$(".product-content").addClass("hide"); //Hides product content (specifications, reviews, image gallery)
	$(".product-content#content_tab-description").removeClass("hide");
	
	
	$(".product-detail_tabs .category-tabs li a").click(
		function () { 
			$(".lnk-addacc").hide(); // hide the acc msg
			var $tabid = this.id; //gets the id of the clicked Tab
			
			var $contenttabid = "content_"+$tabid; //associates the tab with the content
			
			$(".product-content").addClass("hide"); //hides all content
			$(".product-content#"+$contenttabid).removeClass("hide"); //shows desired content
			
			//sets tab to active
			$(".product-detail_tabs .category-tabs li a").parent().removeClass("active2"); 
			$("#"+$tabid).parent().addClass("active2");
		}
	);
	
	$("#tab-related").click(function() {
		$(".lnk-addacc").show();
	});
});


//Show All/Collapse Shopping Cart Dock Product items.
$(function() {	
	var $dockproducts = $(".cart-dock").children(".dock").size();
	
	for(i=6; i <= $dockproducts; i++){ //Hides all product items other then first 5.
		 $(".dock:nth-child("+i+")").css("display", "none");
	};							
	
	$("a.showall-title").toggle(
		function () { 
			$(".dock").css("display", "block");
			$("a.showall-title").addClass("minus").text("Collapse items");
		},
		function () { 
			for(i=6; i <= $dockproducts; i++){
				 $(".dock:nth-child("+i+")").css("display", "none");
			};	
			$("a.showall-title").removeClass("minus").text("Show all items");
			
		}
	);
});


//Show All/Collapse Refine Search.
$(function() {						
		   
	$(".advanced").addClass("hide");
	
	$("a.showoptions-title").toggle(
		function () { 
			$(".advanced").removeClass("hide");
			$("a.showoptions-title").addClass("minus").text("Hide advanced options");
		},
		function () { 
			$(".advanced").addClass("hide");
			$("a.showoptions-title").removeClass("minus").text("Show advanced options");
		}
	);
});

//Show/Hide Display Fields (Shopping cart - Check out).
$(function() {						
	
	$(".displayfields").css("display", "none"); //Hides display fields.
	$("select#delivery-pickup").attr("disabled", "disabled"); //Disable delivery pickup select
	
	var $ischecked2 = $("input[type='radio']:checked").val();
	var $getfieldid2 = $("input[type='radio']:checked").attr("id"); //gets checked radiobutton's ID on page load

	//Enable/Disable Delivery pick up on load (Step 1) 
	if($getfieldid2 == "BCPickup") {
		$("select#delivery-pickup").removeAttr("disabled");
	} else {
		$("select#delivery-pickup").attr("disabled", "disabled");
	}

	if($ischecked2 && $ischecked2.length > 0) {
		//shows/hides display fields associated with the radiobutton on page load (Steps 2 & 3)
		$(".displayfields").hide();
		$(".displayfields#"+$getfieldid2+"Fields").show();
	}
	else {
		$(".displayfields").hide();
	}

	//Show hide/fields on radiobutton change.
	$("input[type='radio']").not(".nojs").change(function(){

		var $ischecked = $(this).val();
		var $getfieldid = $(this).attr("id"); //gets checked radiobutton's ID

		//Enable/Disable Delivery pick up (Step 1)
		if($getfieldid == "BCPickup") {
			$("select#delivery-pickup").removeAttr("disabled");
		} else {
			$("select#delivery-pickup").attr("disabled", "disabled");
		}

		//shows/hides display fields associated with the radiobutton (Steps 2 & 3)
		if($ischecked.length > 0) {
			$(".displayfields").hide();
			$(".displayfields#"+$getfieldid+"Fields").show();		
		}
		else {
			$(".displayfields").hide();
		}

	});
});


//Show/Hide Credit Card Fields (Step 4 - Shopping cart - Check out).
$(function() {						
	
	var $isccselected = $("select#u_method option:selected").val(); //gets value of option selected
		
	if($isccselected == "C") {
		//shows/hides display fields associated with the select option on load
		$(".displayfields").hide();
		$(".displayfields#CreditCardFields").show();
	}

	if($isccselected == "G") {
		//shows/hides display fields associated with the select option on load
		$(".displayfields").hide();
		$(".displayfields#RentsmartFields").show();
		checkRSfields();	
	}

	if($isccselected == "A") {
		$("#lblPO").addClass("required-label");
	}
	
	$("select#u_method").change(function(){

		var $isccselected = $("select#u_method option:selected").val();
		
		$(".displayfields").hide();
		
		if($isccselected == "C") {
			//shows/hides display fields associated with the select option
			$(".displayfields#CreditCardFields").show();
		}

		if($isccselected == "G") {
			//shows/hides display fields associated with the select option
			$(".displayfields#RentsmartFields").show();
			checkRSfields();
		}
		
		if($isccselected == "A") {
			$("#lblPO").addClass("required-label");
		}
	});
	
	$("select#u_rsoption").change(function() {
			checkRSfields();
	});
});

function checkRSfields() {
	var $rentsmart = $("select#u_rsoption option:selected").val();
	if ($rentsmart == "E") {
		$("div#rsnumber").show();
	} else {
		$("div#rsnumber").hide();
	}
}

// Organisation Type BS selector
$(function() {	
	$("select#registerOrgType").change(function() {

		var $type = $("select#registerOrgType option:selected").val();

		if ($type == "B67" || $type == "B68" || $type == "B69" || $type == "B70") {
			$("#msgBS").show();
		} else {
			$("#msgBS").hide();
		}
	});
});

// State postcode changer
$(function() {
	$("select#registerStateSelect").change(function() {
		$state = $("select#registerStateSelect option:selected").val();
		if($state == "OS")
			$("input#registerPostcodeText").attr("maxlength", "10");
		else
			$("input#registerPostcodeText").attr("maxlength", "4");
	});
});

// Invoice Same checkbox
$(function() {
	$("#InvoiceSame").change(function() {
	  if ($("#InvoiceSame").attr("checked"))
	    $("#InvoiceFields").hide()
	  else
	    $("#InvoiceFields").show()
	});
});

// Cart GST Checkbox
$(function() {

 $("#CartGSTCheckbox").click(function() {
 	if ($("#CartGSTCheckbox").attr("checked")) {
 		$(".incprice").show();
 		$(".exprice").hide();
 	} else {
 		$(".incprice").hide();
 		$(".exprice").show();
 	}
 	
 });

});

// Compare button
$(function() {
	$("input:image.comparebutton").click(function() {
		var $sel = $("input:checkbox.comparecheckbox:checked").length;
		if ($sel <= 1 || $sel > 3) {
			$("#comparemsg_" + $(this).attr("id").substring(8)).show();
		} else {
			var $list = "";
			for (var $cb in $("input:checkbox.comparecheckbox:checked").get()) {
				if (!isNaN($cb))
					$list= $list + "," + $("input:checkbox.comparecheckbox:checked").eq($cb).attr("id").substring(10);
			}
			document.location.href= $("#compare_url").val() + "parts=" + $list.substring(1);
		}
	});
});


// Menu function
$(function() {	
	 $(".subnav ul").hover(
		function(){
			$(this).parent().addClass("active");
		},
		function(){
			$(this).parent().removeClass("active");
		}	
	)
});


// Old functions
function OpenWindow(sTitle, sURL, nWidth, nHeight, sScroll) {
	var nLeft = (screen.width - nWidth) / 2;
	var nTop = (screen.height - nHeight) / 2;
	var sConfigure = 'height=' + nHeight + ',width=' + nWidth + ',top=' + nTop + ',left=' + nLeft + ',resizable=no,scrollbars=' + sScroll + ',toolbar=no,menubar=no';

	window.open(sURL, sTitle, sConfigure);
}

function ParentWindow(sURL, oChild) {
	
	if (window.opener) {
		opener.window.location = sURL;
		oChild.blur();
	}
	else {
		document.location = sURL;
	}
}

function ConfirmDelete(sURL, sMsg) {
		if(confirm(sMsg)) {
				document.location = sURL;
		}
}
