/*
	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");
var _CufonLoaded    = (typeof(Cufon) == "function");

if (_jQueryLoaded)
{
	$(document).ready(function()
	{
		CheckAgeVerificationCookie();
	
		InitRolloverNavigation();
		InitRolloverInputs();
		InitLoadCUFON();
		InitInputInnerLabels();
		InitFlashBackground();
		
		InitDownloadTracking();
		ToggleDefaultValue();

		
		// Ensures top menu item is highlighted when sub menu items are focused
		$('#header ul li ul').hover(function() {
			    $(this).prev('a').addClass('hover');
		    }, function() {
			    $(this).prev('a').removeClass('hover');
		    });
			
		// JQuery Select Box
		$('.selectReplace').selectbox();
		// JQuery Select Box
		$('.selectReplaceScroll').selectbox({containerClass: 'selectbox-wrapper-scroll'});
		
		
		// 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");
		
		$("a.disabled, a.Selected").click(function() { return false; });
		
		// IE 6 PNG fix for elements:
		// Elements to "fix" are stored in array for readability, converted back to string to feed into fix.
		var DD_fix =
			[
				".transparent-element",
				"#transparent-element-example",
				".headerLogo img",
		                ".greenTile",
		                ".greyTile",
		                ".yellowTile",
		                ".homeTileContent",
		                "#ribbon",
		                "#ribbon .homeRibbon",
		                ".contentPanelWrapper",
		                ".contentPanel",
		                ".contentPanelInternal",
		                ".galleryPaging",
		                ".newsList .thumbnail",
		                ".selectbox-wrapper ul",
		                ".selectbox-wrapper-scroll ul",
		                ".polaroidWrapper",
		                ".polaroid",
		                ".polaroidContent",
		                ".bottleImg",
		                ".gridLayout .thumb",
		                ".ageCheckPanelWrapper",
		                ".ageCheckPanel",
		                ".ageCheckPanelInternal",
		                ".overLapFlower",
						".imgFrame",
				"#contentTiles.horizontalTiles",
					"#harvestClub.featured",
					"#harvestClub.join",
					"#harvestClub.featured .inner",
					"#harvestClub.join .inner",
					"#harvestClubItems.horizontalTiles",
					"#harvestClubItems.horizontalTiles .inner"
                
			].toString();

		(typeof DD_belatedPNG == "object") ? DD_belatedPNG.fix(DD_fix) : "";
		
		// Initialises controls for 'Our Range' custom popup box
		if ( $('#OurRangeContainer').length > 0 ) {         
            $('#OurRangeClose').click(function() { 
                $('#OurRangeContainer').hide();
            });
            
            $('#PrevButton').click(function() {
                var currProdID = parseInt($('#CurrentProductID').val());
                if ( currProdID > 1 ) { 
                    displayProduct(currProdID-1);
                }
            });
            
            $('#NextButton').click(function() {
                var currProdID = parseInt($('#CurrentProductID').val());
                if ( currProdID < 9 ) { 
                    displayProduct(currProdID+1);
                }
            });
        }  
		
		
		$('ul.newsList li:last').addClass('last');
	});
}


function CheckAgeVerificationCookie()
{
	
	var pageUrl = document.location.href.toLowerCase();
	var pageName = pageUrl.substring(pageUrl.lastIndexOf("/") + 1);
	
	if (pageUrl.indexOf("welcome.aspx") == -1)
	{
		if(pageName == "terms-conditions.aspx"){
			//Don't redirect.
		}
		else {
			if ($.cookie("AgeVerification") == null)
			{
				document.location = "welcome.aspx";
			}
		}
	}
}


//------------------------------------------
// JS for making labels within input boxes
// disapear when clicked
//------------------------------------------
function InitInputInnerLabels() 
{    
    if (_jQueryLoaded) 
    {
	    $('.compact input, form.compact select, form.compact textarea').focus(function(){
			    $('label[for=' + $(this).attr('id') + ']').hide();
		    });
	    $('.compact input, form.compact select, form.compact textarea').each(function(ele){
			    if($(this).val().length > 0)
				    $('label[for=' + $(this).attr('id') + ']').css({ display: "none" });
		    });
	    $('.compact input, form.compact select, form.compact textarea').blur(function(){
			    if($(this).val().length == 0)
				    $('label[for=' + $(this).attr('id') + ']').show();
		    });
	    $('.compact').removeClass('compact');
    }
}

function InitFlashBackground()
{    
    
}

function InitLoadCUFON() 
{ 
    if (_CufonLoaded) 
    {
       Cufon.replace('#contentTiles h2', { fontFamily: 'classic-garamond' });
       Cufon.replace('#contentTiles h3', { fontFamily: 'classic-garamond' });
       Cufon.replace('#headingTagLine h1', { fontFamily: 'classic-garamond' });
       Cufon.replace('.contentPanelInternal h2', { fontFamily: 'classic-garamond' });
       Cufon.replace('.ageCheckPanelInternal h1', { fontFamily: 'classic-garamond' });
       Cufon.replace('.welcomePopupContent h2', { fontFamily: 'adobe-garamond' });
       Cufon.replace('#harvestClub h2', { fontFamily: 'classic-garamond' });
       Cufon.replace('#harvestClub h3', { fontFamily: 'classic-garamond' });
       Cufon.replace('#harvestClubItems h2', { fontFamily: 'classic-garamond' });
       Cufon.replace('#harvestClubItems h3', { fontFamily: 'classic-garamond' });
       Cufon.now();
    }
    
}

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()
			{
			    if (!$(this).is('.disabled'))
				    $(this).attr("src", eval("b" + imageName + "1.src"));
			});
			
			$(this).mouseout(function()
			{
				$(this).attr("src", eval("b" + imageName + "0.src"));
			});
            
		});
	}
}


function InitDownloadTracking()
{
	$("a").each(function()
	{
		var href = $(this).attr("href").toLowerCase();

		if (href.indexOf(".pdf") != -1 || href.indexOf(".zip") != -1 || href.indexOf(".exe") != -1)
		{
		   $(this).click(function()
		   {
				var fileName = $(this).attr("href").split("/").reverse()[0];
				pageTracker._trackPageview("File: " + fileName);
		   });
		}
	});
}
           
            
//------------------------------------------
// JS for dynamically changing products
//------------------------------------------
function displayProduct(productID) {
    
    $("#CurrentProductID").val(productID);
    $("#OurRangeProduct").html('<div class="loading">&nbsp;</div>');
    
    var currProdID = parseInt($('#CurrentProductID').val());
    if ( currProdID > 1 ) 
        $('#PrevButton').removeClass('disabled');
    else
        $('#PrevButton').addClass('disabled');
        
    if ( currProdID < 9 ) 
        $('#NextButton').removeClass('disabled');
    else
        $('#NextButton').addClass('disabled');
    
    $('#OurRangeContainer').show();
    
    $.ajax({
        type: "POST",
        url: "our-range_product-details.aspx",
        data: {'ProductID':productID, 'Ajax':'1'},         
        success: function(msg) {
            $("#OurRangeProduct").html(msg);
            Cufon.replace('h2');
            (typeof DD_belatedPNG == "object") ? DD_belatedPNG.fix('.bottleImg') : "";
            $("a[rel=external]").attr("target", "_blank");
            
            if (pageTracker != null)
            {
				pageTracker._trackPageview("our-range_product-details.aspx?ProductID=" + productID);
            }
        }
    });
}
 
function ToggleDefaultValue() {
 // For forms to clear values on focus and add values on blur
 $("input.input01[type=text]").focus(function() {
  currentVal = $(this).attr("title");
  ($(this).val() == currentVal) ? $(this).val("") : "";
 }).blur(function() {
  ($(this).val() == "") ? $(this).val(currentVal) : "";
 });
}
 

