/*
	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();
		InitRolloverInputs();
		
		// Makes links open in new window when they have rel=external || class="external-link" || href startes with http:
		$("a[@rel=external], a.external-link, a[@href^=http://]").attr("target", "_blank");
		// Reset attribute for absolute internal links http://www.newgencoal.com.au or http://newgencoal.com.au
		$("a[@href^=http://www.newgencoal], a[@href^=http://newgencoal]").removeAttr("target");
		
		// Added class to external links for browsers that don't understand css attribute selectors (ie6/7)
		$("#content p a[@href^=http:]").addClass("offsite");
		$("#content p a[@href^=http://www.newgencoal.com.au]").removeClass("offsite"); // Removes class if internal absolute link
		
		// Form code to change focus and blured states
		$(".formstates01 textarea, .formstates01 input").focus(function(){
			($(this).val() == $(this).attr("title")) ? $(this).val("") : "";
			$(this).parents(".formstates01").removeClass("formstates01 form-blured01").addClass("form-focused01");
		}).blur(function(){
			($(this).val() == "") ? $(this).val($(this).attr("title")) : "";
			$(this).parents(".form-focused01").removeClass("form-focused01").addClass("formstates01 form-blured01");
		});
		
		// Only adding position through javascript as position relative when js is turned off
		// causes ie6 to make the select field sit funny.
		$(".formselect01_inner").selectbox().parent().css({ position: "relative" });
		$(".form-layout01 .field05").css({ position: "relative" }); // fixes issue when js is off ie6
		
		// Form fields clear and re-insert of generic content
		$(".formvalue01").focus(function(event)
		{
			if ($(this).val() == $(this).attr("title"))
			{
				$(this).val("");
			}
		}).blur(function(event)
		{
			if ($(this).val() == "")
			{
				$(this).val($(this).attr("title"))
			}
		});
		
		// Enables hover psuedo class to work in ie6 for non anchor elements
		$(".list03 li, .tags-container01 li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		}).focus(function(){
			$(this).addClass("hover");
		}).blur(function(){
			$(this).removeClass("hover");
		});
		
		
		// Applies tooltip functionality
		tooltip();
		
		
		// Show anything that is only visible with Javascript turned ON
		$(".jsonly").show();
		
		// Hide anything that is only visible with Javascript turned OFF
		$(".nonjsonly").hide();
		
		
		$("a.gobackinhistory").click(function(event)
		{
			event.preventDefault();
			history.go(-1);
		});

	});
}

var _RolloverNavigationLoaded = false;
function InitRolloverNavigation()
{
	if (_jQueryLoaded && !_RolloverNavigationLoaded)
	{
		$("#navigation a, #subnavigation a").each(function()
		{
			$(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"));
			});

		});
	}
}

// Checks to see if left column is longer than content and adjusts content height
function checkColumnHeight()
{
	var leftColumnHeightOffset = $("#subnavigation").height()-100;
	var contentContainerHeightOffset = $(".content01 .content-container").height();
	
	if(leftColumnHeightOffset > contentContainerHeightOffset && $.browser.version == "6.0")
	{
		$(".content01 .content-container").css({
			height: (leftColumnHeightOffset)
		});
	}
	else if(leftColumnHeightOffset > contentContainerHeightOffset)
	{
		$(".content01 .content-container").css({
			minHeight: (leftColumnHeightOffset)
		});
	}
}

// Simple tooltip script without the need for plugins
this.tooltip = function(){	
	/* CONFIG */		
		var xOffset = 150;
		var yOffset = -280;	
		// these 2 values are fallback values if the browser fails to determine the offset		
	/* END CONFIG */		
	
	// Glossary link tooltip
	$("a.popup-link").hover(function(e){											  
		xOffset = $(this).offset().top;
		
		this.t = this.title;
		this.title = "";	
							  
		$("body").append("<div id='tooltip02'><div class='tooltip-top'>&nbsp;</div><div class='tooltip-container'><div class='tooltip-inner'><h4 class='subtitle08'>"+ this.t.split("::::")[0] + "</h4><p>" + this.t.split("::::")[1] + "</p><div class='clear'>&nbsp;</div></div><div class='tooltip-icon'>&nbsp;</div><div class='clear'>&nbsp;</div></div><div class='tooltip-btm'>&nbsp;</div></div>");
		var tooltipHeight = $("#tooltip02").height();
		$("#tooltip02")
			.css({
				top: (xOffset-tooltipHeight) + "px",
				left: (e.pageX + (yOffset+149)) + "px"
			}).show().pngFix();
	},
	function(){
		this.title = this.t;
		$("#tooltip02").remove();
	})
	
	// Image bank tooltip
	$("a.tooltip").hover(function(e){											  
		xOffset = $(this).offset().top;
		yOffset = $(this).offset().left-280;
		
		this.t = this.title;
		this.title = "";	
		$(this).css({ position: "relative" });	
							  
		$(this).append("<span class='tooltip-hover' style='display: none;'>&nbsp;</span>");
		$("body").append("<div id='tooltip'><div class='tooltip-top'>&nbsp;</div><div class='tooltip-inner'><h4 class='subtitle08'>"+ this.t.split("::::")[0] + "</h4><p>" + this.t.split("::::")[7] + "</p><div class='clear'>&nbsp;</div></div></div>");
		var tooltipHeight = $("#tooltip").height();
		$("#tooltip")
			.css({
				top: (xOffset-tooltipHeight-16),
				left: ($(this).width()/2) + yOffset
			}).show().pngFix();	
		$(".tooltip-hover")
			.css({
				display: "block",
				position: "absolute",
				top: 0,
				left: 0,
				width: ($.browser.msie) ? $(this).find("img").width()-4 : $(this).find("img").width()-8,
				height: ($.browser.msie) ? $(this).find("img").height()-4 : $(this).find("img").height()-8,
				border: "4px solid #ffffff"
			});	
	},
	function(){
		this.title = this.t;
		$("#tooltip, .tooltip-hover").remove();
		$(this).css("position","");	
	}).click(function(){
		this.title = this.t;
	});	
};