// Tooltip "help" function
$(function() {
	$('.bubbleInfo').each(function() {
		var time = 250;
		var hideDelay = 500;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var popup = $('.help-popup', this).css('opacity', 0);

		$([trigger.get(0), popup.get(0)]).mouseover(function() {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				popup.css({
					top: -11,
					left: -11,
					display: 'block'
				})
        .animate({ opacity: 1 }, time, 'swing', function() {
        	beingShown = false;
        	shown = true;
        });
			}
		}).mouseout(function() {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				popup.animate({ opacity: 0 }, time, 'swing', function() {
					shown = false;
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
});

function SetCurrentlyActiveTopNavItem() {

	$("#main-nav").find("li").each(function() {
		var menuPath = $(this).attr("title");

		// remove the title
		$(this).attr("title", "");

		var curPath = location.pathname.toLowerCase()

		if (curPath.indexOf("/" + menuPath) >= 0) {
			$("body").attr("id", $(this).attr("class"));
			return false;
		}
	});
}

function changeHomePageBG(imgToShow) {
	var horizPositions = ['-148px', '-622px', '-1098px', '-1573']

	var cssObj = {
		'position': 'relative',
		'text-indent': '0'
	}

	$("#homepage-image").css(
		{
			'background-position': "-164px " + horizPositions[imgToShow]
		}
	);

	$("#homepage-image").css("display", "block");	

	//$("#content-wrapper-2col-functional").css("display", "block");

	jQuery.each(jQuery.browser, function(i, val) {
		if (i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.8")
		{
			$("#homepage-image").css("z-index", "0");
			$(".numbers-line").css("position", "relative");
			$(".row").css("position", "relative");
			$(".welcome").css("position", "relative");
			$("homepage-highlight-container").css("position", "relative");
		}
	});

	$("#nav-home .numbers-line li").removeClass("active");
	$("#nav-home .numbers-line a.image-position" + (imgToShow + 1)).parent("#nav-home .numbers-line li").addClass("active");

	$("h1.text1").css("display", "none");
	$("h1.text2").css("display", "none");
	$("h1.text3").css("display", "none");
	$("h1.text4").css("display", "none");

	$("h1.text" + (imgToShow + 1)).show().css(cssObj);

	sIFR.replace({
		src: '/resources/flash/archer-light.swf'
	}, {
		selector: 'h1.text' + (imgToShow + 1),
		wmode: 'transparent',
		css: { '.sIFR-root': { 'color': '#ffffff', 'text-transform': 'uppercase', 'leading': '-8'} },
		filters: {
			DropShadow: {
				distance: 1,
				color: '#000000',
				strength: 2,
				alpha: .15
			}
		}
	});
}

$(document).ready(function() {

	SetCurrentlyActiveTopNavItem();

	// DGH randomise image load on page request
	//var horizPositions = ['0', '-475px', '-950px']
	var rndNum = Math.ceil(Math.random() * 3);

	changeHomePageBG(rndNum - 1);

	$(".trigger").live("mouseover", function() {
		var time = 2500;
		var hideDelay = 500;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;


		//var popup = $('.colours-box-wrapper', this).css('opacity', 0);
		var popup = $(this).next().css('opacity', 0);
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		if (beingShown || shown) {
			return;
		}
		else {
			beingShown = true;

			popup.css({
				top: 9,
				right: -7,
				display: 'block'
			})

            .animate({ opacity: 1 }, time, 'swing', function() {
            	beingShown = false;
            	shown = true;
            });
		}


	});

	$(".trigger").live("mouseout", function() {

		var time = 0;
		var hideDelay = 250;
		var hideDelayTimer = null;

		var shown = true;
		var popup = $(this).next().css('opacity', 1);

		if (hideDelayTimer) clearTimeout(hideDelayTimer);

		hideDelayTimer = setTimeout(
          function() {
          	hideDelayTimer = null;
          	popup.animate({ opacity: 0 }, time, 'easing', function() {
          		shown = false;
          		popup.css('display', 'none');
          	});

          }, hideDelay);
	});


	// Tooltip on thumbnails in Shadowbox
	$(".iconbar li a").hover(
		function() {

			var iconName = $(this).find("img").attr("src");
			$(this).next("span").attr({
				"style": 'display:inline'
			});
			$(this).next("span").stop().show();
		},
		function() {
			var iconName = $(this).find("img").attr("src");
			$(this).next("span").attr({
				"style": 'display:none'
			});
			$(this).next("span").stop().hide();
		});

	$(".iconbar li span").hover(
		function() {
			$(this).attr({
				"style": 'display:block'
			});
			$(this).show();
		},
		function() {
			$(this).attr({
				"style": 'display:none'
			});
			$(this).hide();
		});

	$(".iconbar li span span").hover(
		function() {
			$(this).attr({
				"style": 'display:block'
			});
			$(this).show();
		},
		function() {
			$(this).animate({ opacity: 1 }, { queue: false, duration: 0 }, "linear",
				function() {
					$(this).attr({ "style": 'display:block' });
				});
		});



	// Tooltip on images in product details 

	$(".image-display-img-wrapper img").hover(
		function() {
			var iconName = $(this).find("img").attr("src");
			$(this).next("span").attr({
				"style": 'display:block'
			});
			$(this).next("span").stop().show();
		},
		function() {
			var iconName = $(this).find("img").attr("src");
			$(this).next("span").attr({
				"style": 'display:none'
			});
			$(this).next("span").stop().hide();
			
			//$(this).next("span").animate({ opacity: 0 }, { queue: false, duration: 0 }, "linear",
			//	function() {
			//		$(this).next("span").attr({ "style": 'display:none' });
			//	});
		});

	$(".image-display-img-wrapper span.image-tooltip").hover(
		function() {
			$(this).attr({
				"style": 'display:block'
			});
			$(this).show();
			
		},
		function() {
			$(this).animate({ opacity: 1 }, { queue: false, duration: 0 }, "linear",
				function() {
					$(this).attr({ "style": 'display:none' });
				});
		});


	$(".image-display-img-wrapper span span").hover(
		function() {
			$(this).attr({
				"style": 'display:block'
			});
			$(this).show();
		},
		function() {
			$(this).animate({ opacity: 1 }, { queue: false, duration: 0 }, "linear",
				function() {
					$(this).attr({ "style": 'display:block' });
				});
		});



	// Subnav styling related to parents of current item 

	$("#subnav a.current").parent("li").addClass("current-parent");
	$("#subnav li.current-parent").parent().parent().addClass("current-parent");
	$("#subnav ul li.current-parent").parent().parent().addClass("current-parent");

	$("#subnav a.current").each(function() {
		if ($(this).siblings().length < 1)
			$(this).parent("li").removeClass("current-parent")
	});


	// Green highlight box fadeout and disappear	

	$(".highlight a").click(function() {
		$(this).parents(".highlight").animate({ opacity: "hide" }, "slow");
	});

	// Red error message box fadeout and disappear	

	$(".error-msg a").click(function() {
		$(this).parents(".error-msg").animate({ opacity: "hide" }, "slow");
	});

	// Show/hide functionality for Detailed Technical Specifications table	

	$(".specifications-detailed table").hide();
	$(".btn-slide-hide").hide();

	$(".btn-slide-show").click(function() {
		$(".specifications-detailed table").slideDown("slow");
		$(this).hide();
		$(".btn-slide-hide").show();

	});

	$(".btn-slide-hide").click(function() {
		$(".specifications-detailed table").slideUp("fast");
		$(this).hide();
		$(".btn-slide-show").show();

	});

	// NZ map image map	

	$('.nz-map-blank area').mouseover(function() {
		var hoverAlt = $(this).attr("alt");
		$("#nz-map-overlay").removeClass();
		$("#nz-map-overlay").addClass(hoverAlt);
	});

	// Australia map image map	

	$('.aus-map-blank area').mouseover(function() {
		var hoverAlt = $(this).attr("alt");
		$("#aus-map-overlay").removeClass();
		$("#aus-map-overlay").addClass(hoverAlt);
	});


	// Homepage background image switcher	
	//changeHomePageBG

	$("#nav-home .numbers-line a.image-position1").click(function() { changeHomePageBG(0) });
	$("#nav-home .numbers-line a.image-position2").click(function() { changeHomePageBG(1) });
	$("#nav-home .numbers-line a.image-position3").click(function() { changeHomePageBG(2) });
	$("#nav-home .numbers-line a.image-position4").click(function() { changeHomePageBG(3) });

	//BBJ - clear search box on click
	$('.search-input').click(function() {
		$('.search-input').val('');
	});

	$('#jumpMenuGo').click(function() {

		var s = $('#jumpMenu').val();

		if (s != "")
			window.location = "/tools/search-products/product-details.aspx?code=" + s;
	});
});

function BindCartDropDown() {

	$(".cartbutton").hide();
	$(".thumbnail-block").css("display", "none");
	$("#imgSampletype").css("padding-left","50");
	$("#imgSampletype").hide();

	$(".CartDropDown").bind("change", function(e) {

	    var val = $(".CartDropDown").find('option').filter(':selected').attr("value");
	    

	    switch (val) {
	        case "1":
	            $(".cartbutton").show();
	            $(".CartMessage").css("display", "none");
	            $(".thumbnail-block").css("display", "none");
	            //Product_Card_Sample_Order.jpg
	            $("#imgSampletype").attr("src", "/resources/img/Product_Card_Sample_Order.jpg")
	            $("#imgSampletype").show();
	            break;

	        case "2":
	            $(".cartbutton").show();
	            $(".CartMessage").css("display", "none");
	            $(".thumbnail-block").css("display", "block");
	            //Colour_Sample.jpg
	            $("#imgSampletype").attr("src", "/resources/img/Colour_Sample.jpg")
	            $("#imgSampletype").show();
	            break;

	        default:
	            $(".cartbutton").hide();
	            $("#imgSampletype").hide();
	            break;
	    }

	});
}
	
	
