/*
	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"),
	DTD = DTD || {};

if (_jQueryLoaded)
{    
	$(document).ready(function()
	{
        
		ClickableItem();
		InitYouTubeVideo();
		
		// 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"); 
		
		// Stops IE 6 flicker on hover of link with background image
		if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
			try {
				document.execCommand("BackgroundImageCache", false, true);
			} catch(err) {}
		}

		// Cufon custom font replacement
		CufonReplacement();
		
		InitRolloverInputs();
		
		//CustomCheckbox();
		
		// Adds default blur text to all empty inputs with this class according to title="" attribute
		$(".fieldhint").fieldhint();
		
		$(".print_link").show().click(function(){print();return false;});
		
		HighlightErrors();
		ActiveSideNavigation();
				
		// find elements and hide if js is turned on
		$('.jsOff').hide();
	});
}

function CufonReplacement() {
	Cufon.replace('h1', {fontFamily: 'DIN-Medium'});
	Cufon.replace('h2', {fontFamily: 'DIN-Medium'});
	Cufon.replace('.btn2 .btn-txt', {fontFamily: 'DIN-Medium'});
	Cufon.replace('.btn3 .btn-txt', {fontFamily: 'DIN-Medium'});
	Cufon.replace('h4.title03', {fontFamily: 'DIN-Medium'});
	Cufon.replace('h3.title06', {fontFamily: 'DIN-Medium'});
	Cufon.replace('.intro03', {fontFamily: 'DIN-Medium'});
	Cufon.replace('.table03 th', {fontFamily: 'DIN-Medium'});
	Cufon.replace('h4.title04', {fontFamily: 'DIN-Medium'});
	Cufon.replace('.form-layout04 h2.title02', {fontFamily: 'DIN-Medium'});
}

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('.fieldhintform').submit(function(){ // THIS USED TO SAY 'form:first()' / 'form' ...
          if (t.val() == title) {
              t.val('');
              t.removeClass('fieldhint_blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
};

function ClickableItem() {
	$(".clickbox a.mainlink")
		.parents(".clickbox")
			.css("cursor","pointer")
			.click(function(){
				var mainlink = $(this).find("a.mainlink:first");
				var mainhref = mainlink.attr("href");
				if (mainlink.attr("target") == "_blank") {
					window.open(mainhref);
				} else {
					document.location = mainhref;
				}
			})
		.find("*")
			.click(function(){
				$(this).parents(".clickbox").click();
				return false;
			});	
}

function InitYouTubeVideo() {
	
	var videoWidth = 591,
		videoHeight = 350;
	
	$("object").each(function()
	{
		
		if($(this).find("param[value^='http://www.youtube.com/v/']").size() == 1){
		   $(this).height(videoHeight).width(videoWidth).addClass("videoYouTube");
	    }

	});
	$("embed[src^='http://www.youtube.com/v/']").each(function()
	{
		$(this).height(videoHeight).width(videoWidth).addClass("videoYouTube");

	});
}

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 HighlightErrors() 
{
	$("input.submit-button").click(function(){	
											
		$("span.error").each(function (i) {
			
			
			
			var spanSize = $(this).parent().find("span.error").size();
			
			if(spanSize == 1)
			{
				if($(this).is(':visible') == true)
				{
					 $(this).parents('.field01').addClass("input-error");
				} 
				else
				{	
					$(this).parents('.field01').removeClass("input-error");
				}
			} else {
				var firstChild = $(this).parent().find("span.error:nth-child(1)").is(':visible');
				var secondChild = $(this).parent().find("span.error:nth-child(2)").is(':visible');
				if(firstChild == true || secondChild == true)
				{
					
					$(this).parents('.field01').addClass("input-error");
				} else {					
					$(this).parents('.field01').removeClass("input-error");
				}
			}
		});
	
	});

}


function ActiveSideNavigation() 
{
	$("ul#side-navigation ul").each(function (i) {
		
		if($(this).is(':visible') == true){
			$(this).prev('a').addClass("parent-active");
		}
			
	});
}

/* Equal height columns */

function setEqualHeight(columns)  
{  
	var tallestcolumn = 0;  
	columns.each(  
	function()  
{  
currentHeight = $(this).height();  
if(currentHeight > tallestcolumn)  
	{  
		tallestcolumn  = currentHeight;  
		}  
	}  
	);  
		columns.height(tallestcolumn);  
	}  
	$(document).ready(function() {  
	setEqualHeight($(".box07-small  > div"));  
});  


/* Homepage state interactivity */
function imageMapStates() {
	var mapInfo =  $(".map-states ol"),
		mapContainer = $(".intermap"),
		mapStates = $(".map-states ol > li",$("#banner")),
		mapAreas = $("#map area"), state = "#map-vic",
		loop = 1, overlay = "map-overlay-vic",
		overlayItem = $(".map-overlay"),
		cycleOpts = {
			before: function(cur,next){
				overlay = $(next).attr("data-overlay");
				overlayItem.attr("class","map-overlay " + overlay);
			}
		},
		timer;
	
	mapInfo.cycle(cycleOpts);
	
	mapAreas.live("mouseover",function(e){
		mapHighlight(e);
	}).live("click",function(e){
		mapHighlight(e);
	});
	
	function mapHighlight(e) {
		clearTimeout(timer);
		
		var state = $(e.target).attr("href");
		overlay = "map-overlay-vic";
		overlayItem.attr("class","map-overlay map-overlay-" + state.split("-")[1]);
		
		if($(state).size != 0) {
			mapStates.hide();
			mapInfo.cycle('stop');
			$(state).show().animate({opacity: 1});
		}
		
		DTD.mapInfo = mapInfo || $(".map-states ol");
		DTD.mapStates = mapStates || $(".map-states ol > li",$("#banner"));
		
		e.preventDefault();
	}
	
	mapContainer.bind("mouseover mouseout",function(e){
		var type = e.type;
		
		if(type === "mouseover") {
			clearTimeout(timer);
		} else {
			timer = setTimeout(function(){
				DTD.mapStates.each(function(index,elem){
					$(elem).css({opacity: 1}).hide();
				});
				DTD.mapInfo.cycle(cycleOpts);
			},5000);
		}
	});
	
	
}
