/*
	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)
{
	$(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[type="hidden"]').hide(); // FF shows them sometimes?
		$('input').placeholder();
		$("a.showoptions-title, a.showall-title").css("display", "block");		   
	});
}

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"));
			});

		});
	}
}

//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");
			
		}
	);
});

$(function() {
	$(".displayfields").hide(); //Hides display fields.
	$("input[type='radio']:checked").each(function() {
		var $ischecked2 = $(this).val();
		var $getfieldid2 = $(this).attr("id"); //gets checked radiobutton's ID on page load
		if($ischecked2 && $ischecked2.length > 0) {
			//shows/hides display fields associated with the radiobutton on page load (Steps 2 & 3)
			$(".displayfields#"+$getfieldid2+"Fields").show();
		}
	});

	//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

		//shows/hides display fields associated with the radiobutton (Steps 2 & 3)
		if($ischecked.length > 0) {
			$(this).parents(".form-layout01").find(".displayfields").hide();
			$(".displayfields#"+$getfieldid+"Fields").show();
		}
		else {
			$(this).parents(".form-layout01").find(".displayfields").hide();
		}

	});
});

//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");
		}
	);
});

// 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");
		}	
	)
});

 
// Simulates PHP's date function
Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(i-1>=0&&format.charAt(i-1)=="\\"){returnStr+=curChar;}else if(replace[curChar]){returnStr+=replace[curChar].call(this);}else if(curChar!="\\"){returnStr+=curChar;}}return returnStr;};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate();},D:function(){return Date.replaceChars.shortDays[this.getDay()];},j:function(){return this.getDate();},l:function(){return Date.replaceChars.longDays[this.getDay()];},N:function(){return this.getDay()+1;},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')));},w:function(){return this.getDay();},z:function(){var d=new Date(this.getFullYear(),0,1);return Math.ceil((this-d)/86400000);},W:function(){var d=new Date(this.getFullYear(),0,1);return Math.ceil((((this-d)/86400000)+d.getDay()+1)/7);},F:function(){return Date.replaceChars.longMonths[this.getMonth()];},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1);},M:function(){return Date.replaceChars.shortMonths[this.getMonth()];},n:function(){return this.getMonth()+1;},t:function(){var d=new Date();return new Date(d.getFullYear(),d.getMonth(),0).getDate()},L:function(){var year=this.getFullYear();return(year%400==0||(year%100!=0&&year%4==0));},o:function(){var d=new Date(this.valueOf());d.setDate(d.getDate()-((this.getDay()+6)%7)+3);return d.getFullYear();},Y:function(){return this.getFullYear();},y:function(){return(''+this.getFullYear()).substr(2);},a:function(){return this.getHours()<12?'am':'pm';},A:function(){return this.getHours()<12?'AM':'PM';},B:function(){return Math.floor((((this.getUTCHours()+1)%24)+this.getUTCMinutes()/60+this.getUTCSeconds()/3600)*1000/24);},g:function(){return this.getHours()%12||12;},G:function(){return this.getHours();},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12);},H:function(){return(this.getHours()<10?'0':'')+this.getHours();},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes();},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds();},u:function(){var m=this.getMilliseconds();return(m<10?'00':(m<100?'0':''))+m;},e:function(){return"Not Yet Supported";},I:function(){return"Not Yet Supported";},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00';},P:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+':00';},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result;},Z:function(){return-this.getTimezoneOffset()*60;},c:function(){return this.format("Y-m-d\\TH:i:sP");},r:function(){return this.toString();},U:function(){return this.getTime()/1000;}};

// 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;
		}
}

