/*
	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();
		InitHoverSwaps();
		InitViewMore();
		
		// 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");
		
		$(".custom-dropdown").selectbox();
		
		$(".fieldhint").fieldhint();
		
		$(".search1_autocomplete").autocomplete(
			"WebServices/LocationFinder.asmx/LocationAutoSuggest",
			{
				delay:0,
				minChars:3,
				maxItemsToShow:10
			}
		);
		
		if ($.browser.msie==true && $.browser.version==6)
		{
			DD_belatedPNG.fix("ul.dropdown-navigation .subnav .subnav-container");
			DD_belatedPNG.fix("ul.dropdown-navigation .subnav .subnav-container .padding");
			DD_belatedPNG.fix("ul.dropdown-navigation ul");
			DD_belatedPNG.fix(".ac_results");
			DD_belatedPNG.fix(".ac_results div");
			DD_belatedPNG.fix(".ac_results ul");
			DD_belatedPNG.fix(".ac_results li");
		}
		
		trackExternalLinks();
		trackAssetDownloads();
		
		$(".clickable01")
			.css("cursor","pointer")
			.click(
				function()
				{
					var newlinkhref = $(this).find("a").attr("href");
					window.location = newlinkhref;
				}
			)
			.hover(
				function()
				{
					$(this).find("a").addClass("hover1");
				}
				,
				function()
				{
					$(this).find("a").removeClass("hover1");
				}
			)
		;
		
		if ($("#pdf_cycle_content ul").length>1)
		{
			$("#pdf_cycle").append('<div id="pdf_cycle_nav"><span id="pdf_cycle_nav_pages"></span></div>');
			$("#pdf_cycle_nav_pages").before('<a href="#" id="pdf_cycle_prev">Back</a>').after('<a href="#" id="pdf_cycle_next">Next</a>');
			$("#pdf_cycle_content").cycle({
				fx: 'scrollHorz',
				pager: '#pdf_cycle_nav_pages', 
				speed: '300',
				timeout: 0,
				prev: '#pdf_cycle_prev',
				next: '#pdf_cycle_next'
			});
			$("#pdf_cycle_nav").css("padding-left",($("#pdf_cycle").width()/2)-($("#pdf_cycle_nav").width()/2));
		}
	});
}

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 InitHoverSwaps()
{
	if (_jQueryLoaded)
	{
		$(".hoverswap").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()");
					if (imageSrc.indexOf(".gif") > -1) {
						eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".gif'");
					} else if (imageSrc.indexOf(".jpg") > -1) {
						eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".jpg'");
					} else if (imageSrc.indexOf(".png") > -1) {
						eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".png'");
					}
				}
			}
			
			$(this).mouseover(function()
			{
				$(this).attr("src", eval("b" + imageName + "1.src"));
			});
			
			$(this).mouseout(function()
			{
				$(this).attr("src", eval("b" + imageName + "0.src"));
			});

		});
	}
}

function InitViewMore()
{
	if (_jQueryLoaded)
	{
		$(".viewmore_item").each(function(){
			
			/*vm_rows = $(this).find(".viewmore_rows");
			vm_link = $(this).find(".viewmore_link");*/
			
			$(this).find(".viewmore_rows").hide();
			
			$(this).find(".viewmore_link").show().click(function(){
				if ($(this).html().indexOf("more") > -1)
				{
					if ($(this).hasClass("btn-viewmorestockists"))
					{
						$(this).removeClass("btn-viewmorestockists");
						$(this).addClass("btn-viewlessstockists");
					}
				$(this).html("&minus;&nbsp; View less Stockists");
					$(this).parents(".viewmore_item").find(".viewmore_rows").slideDown();
				}
				else if ($(this).html().indexOf("less") > -1)
				{
					if ($(this).hasClass("btn-viewlessstockists"))
					{
						$(this).removeClass("btn-viewlessstockists");
						$(this).addClass("btn-viewmorestockists");
					}
					$(this).html("+&nbsp; View more Stockists");
					$(this).parents(".viewmore_item").find(".viewmore_rows").slideUp();
				}
				return false;
			});

		});
	}
}

// field Hints
jQuery.fn.fieldhint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get it once since it won't change
    var title = t.attr('title'); 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('fieldhint_blur');
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('fieldhint_blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form').submit(function(){ // THIS USED TO SAY form:first()
          if (t.val() == title) {
              t.val('');
              t.removeClass('fieldhint_blur');
          }
      });
	  // do the same when the window is unloaded
	  $(window).unload(function(){ // THIS USED TO SAY form:first()
          if (t.val() == title) {
              t.val('');
              t.removeClass('fieldhint_blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
 }

function trackExternalLinks()
{
	$("a[href^=http://]").click(function(){
		pageTracker._trackPageview('EXTERNAL:' + $(this).attr("href"));
		pageTracker._trackEvent('External Websites', $(this).attr("href"), document.location.pathname + document.location.search);
	});
}

function trackAssetDownloads()
{
	$("a").each(function(){
		myHref = ($(this).attr("href")) ? $(this).attr("href") : "";
		if (myHref.toLowerCase().indexOf(".pdf") != -1 || myHref.toLowerCase().indexOf(".zip") != -1 || myHref.toLowerCase().indexOf(".doc") != -1 || myHref.toLowerCase().indexOf(".xls") != -1)
		{
			$(this).click(function(evt){
				var getFileName = $(this).attr("href").split("/").reverse()[0];
				
				pageTracker._trackPageview( 'File: ' + getFileName );
			});
		}
	});
}