/*
	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();
		InitAllSwaps();
		InitFuncs();
		
		// Makes links open in new window when they have rel=external
		$("a[@rel='external'], a.external-link, a.new-window, a[@target='_blank']")
			.not(".thickbox")
			.click(function(){
				window.open(this.href);
				return false;
			});
		
		// 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) {}
		}
		
		// Improved MSIE sniffing - see http://www.ericmmartin.com/jquery-browser-issues-revisited/
		if ($.browser.msie)
		{
			jQuery.browser.version = jQuery.browser.msie &&
				parseInt(jQuery.browser.version) >= 6 &&
				typeof window['XMLHttpRequest'] == "object" ?
					"7.0" :
					jQuery.browser.version;
			jQuery.browser.msie6 = jQuery.browser.msie &&
				parseInt(jQuery.browser.version) == 6 &&
				typeof window['XMLHttpRequest'] != "object";
		}
		
		$(".search1_autocomplete").autocomplete(
			"WebServices/keywords.aspx",
			{
				delay:0,
				minChars:0,
				maxItemsToShow:10
			}
		);
		
		// Flowchart table popup - formembers_becomingamember_choosing-a-fund_eligibility.aspx
		$(".flowchart_link")
			.click(function(){
				path = $(this).attr("href");
				if (path) {
					window.open(path, "popup", "width=830, height=550, toolbar=no, status=no, scrollbars=yes, resizable=yes");
					return false;
				}
			});

		// find elements and hide if js is turned on
		$('.jsOff').hide();
			
		// Forms & Publications control/pages - "Complete order" link
		$(".qtycol02")
			.removeClass("qtycol02_nojs")
			.children(".completelink")
			.each(
				function()
				{
					var completelink = $(this);
					var controlfield = completelink.parent("td").prev(".qtycol01").find("input.quantity_txbx");
					(controlfield.val() > 0) ? completelink.css("visibility","visible") : completelink.css("visibility","hidden");
				}
			)
			.parent("td").prev(".qtycol01").find("input.quantity_txbx")
			.click(
				function()
				{
					var controlfield = $(this);
					var completelink = controlfield.parent("div").parent("td").next(".qtycol02").find(".completelink");
					(controlfield.val() > 0) ? completelink.css("visibility","visible") : completelink.css("visibility","hidden");
				}
			)
			.change(
				function()
				{
					var controlfield = $(this);
					var completelink = controlfield.parent("div").parent("td").next(".qtycol02").find(".completelink");
					(controlfield.val() > 0) ? completelink.css("visibility","visible") : completelink.css("visibility","hidden");
				}
			)
			.keyup(
				function()
				{
					var controlfield = $(this);
					var completelink = controlfield.parent("div").parent("td").next(".qtycol02").find(".completelink");
					(controlfield.val() > 0) ? completelink.css("visibility","visible") : completelink.css("visibility","hidden");
				}
			)
		;
		
	});
}

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 InitAllSwaps()
{
	if (_jQueryLoaded)
	{
		$("img[@src*='swap_'], input[@src*='swap_']")
		.not("img[@src*='_2.'], input[@src*='_2.']")
		.not(".no_swap")
		.not("img[@src*='swap_expand_']")
		.not("img[@src*='swap_tabtools_']")
		.not("img[@src*='swap_tabformslinks_']")
		.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'");
					}
				}
			}
			$(this).mouseover(function(){
				$(this).attr("src", eval("b" + imageName + "1.src"));
			});
			$(this).mouseout(function()
			{
				$(this).attr("src", eval("b" + imageName + "0.src"));
			});
			
		});
	}
}
function InitFuncs()
{
	if (_jQueryLoaded)
	{
		$("#funcs #func_explain")
			.show();
			
		$("#funcs #func_icons a")
			.mouseover(function(){
				explain_text = $(this).children("img").attr("alt");
				$("#funcs #func_explain").text(explain_text);
			})
			.mouseout(function(){
				$("#funcs #func_explain").html("&nbsp;");
			});
		
		$("#funcs #func_icons #func_icon_email")
			.click(function(){
				path = $(this).attr("href");
				if (path) {
					window.open(path, "popup", "width=450, height=400, toolbar=no, status=no, scrollbars=yes, resizable=yes");
					return false;
				}
			});
		
		$("#funcs #func_icons #func_icon_print")
			.show()
			.click(function(){
				if(window.print) {
					window.print();
				} else {
					if(agt.indexOf("mac") != -1) {
						alert("To print this page press Command-P.");
					} else {
						alert("To print this page press Control-P.");
					}
				}
				return false;
			});
		
	}
}

// Funtions for Global Tabs hover states
function TabHoverOn(id) {
	document.getElementById(id).className="global-tab-active";
}
function TabHoverOff(id) {
	document.getElementById(id).className="";
}


/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
	
http://adamv.com/dev/javascript/querystring
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}

/* Investment Choice - Investment Options Tab switcher */

function show_1invest() {
    document.getElementById("invch_tabs_1invest").style.display = "block";
    document.getElementById("invch_1invest").style.display = "block";
    document.getElementById("invch_tabs_2fytd").style.display = "none";
    document.getElementById("invch_2fytd").style.display = "none";
    document.getElementById("invch_tabs_3month").style.display = "none";
    document.getElementById("invch_3month").style.display = "none";
}

function show_2fytd() {
    document.getElementById("invch_tabs_1invest").style.display = "none";
    document.getElementById("invch_1invest").style.display = "none";
    document.getElementById("invch_tabs_2fytd").style.display = "block";
    document.getElementById("invch_2fytd").style.display = "block";
    document.getElementById("invch_tabs_3month").style.display = "none";
    document.getElementById("invch_3month").style.display = "none";
}

function show_3month() {
    document.getElementById("invch_tabs_1invest").style.display = "none";
    document.getElementById("invch_1invest").style.display = "none";
    document.getElementById("invch_tabs_2fytd").style.display = "none";
    document.getElementById("invch_2fytd").style.display = "none";
    document.getElementById("invch_tabs_3month").style.display = "block";
    document.getElementById("invch_3month").style.display = "block";
}

function LoadInvChTabs() {
    document.getElementById("invch_tabs").style.display = "block";

    document.getElementById("invch_content").className = "invch_content_applied";

    var qs_i = new Querystring()
    var load_invch_tab = qs_i.get("load_invch_tab", "1")
    if (load_invch_tab == "2") {
        show_2fytd();
    }
    else if (load_invch_tab == "3") {
        show_3month();
    }
    else {
        show_1invest();
    }

    var areaContainer = document.getElementById("invch_content");
    var areaHeadings = areaContainer.getElementsByTagName("h4");
    for (var areaHeadingsIndex = 0; areaHeadingsIndex < areaHeadings.length; areaHeadingsIndex++) {
        areaHeadings[areaHeadingsIndex].style.display = "none";
    }

}



function hideAllFields()
{
    for (i = 1; i < 12; i++)
    {
        document.getElementById('language' + i).style.display = 'none';
        document.getElementById('hide' + i).style.display = 'none';
    }
}

function displayThis(id)
{
    document.getElementById('language' + id).style.display = 'block';
    document.getElementById('show' + id).style.display = 'none';
    document.getElementById('hide' + id).style.display = 'inline';
}

function hideThis(id)
{
    document.getElementById('language' + id).style.display = 'none';
    document.getElementById('show' + id).style.display = 'inline';
    document.getElementById('hide' + id).style.display = 'none';
}