/*
	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
*/

var ie6 = (($.browser.msie && $.browser.version=="6.0") ? true : false);

// jQuery functions that run on "dom ready"

var _jQueryLoaded = (typeof(jQuery) == "function");

if (_jQueryLoaded)
{
    $(document).ready(function() {
        InitRolloverNavigation();
        InitAllSwaps();
        InitFuncs();

        if (jQuery().resize.delay) {
            $.resize.delay = 25;
        }

        // 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(evt){
				evt.preventDefault();
				function RandomId(){
					var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
					var string_length = 30;
					var randomstring = '';
					for (var i=0; i<string_length; i++) {
						var rnum = Math.floor(Math.random() * chars.length);
						randomstring += chars.substring(rnum,rnum+1);
					}
					return randomstring;
				}
				window.open(this.href,RandomId());
			});

        // 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");
				}
			)
		;

        if (jQuery().tableHover) {
            $(".invperf_table01").not(".invperf_table03, .invperf_table05").tableHover({ rowClass: "rowHover", colClass: "colHover", cellClass: "cellHover" });
        }

        $(".expander2")
			.each(function() {

			    var theobject = $(this).find(".expander2_object");
			    var thehandle = $(this).find(".expander2_handle");

			    theobject
					.hide();

			    thehandle
					.addClass(".expander2_handle_contracted")
					.css("cursor", "pointer")
					.click(function() {
					    theobject.slideToggle();
					    thehandle.toggleClass("expander2_handle_contracted");
					});

			})
		;

        $("#invest2_colset1_autoadjust")
			.addClass("invest2_colset1_autoadjusted")
			.each(function() {

			    var all_insides = $(this).find(".invest2_box_inside2");
			    var col_left = $(this).find(".invest2_col1 .invest2_col_inside");
			    var col_right = $(this).find(".invest2_col2 .invest2_col_inside");
			    var box_left = $(this).find(".invest2_box2 .invest2_box_inside2");
			    var box_right = $(this).find(".invest2_box4 .invest2_box_inside2");

			    function AutoAdjustHeights() {
			        all_insides.height("auto");
			        if (col_left.height() > col_right.height()) {
			            var difference = col_left.height() - col_right.height();
			            var resize_obj = box_right;
			        }
			        if (col_right.height() > col_left.height()) {
			            var difference = col_right.height() - col_left.height();
			            var resize_obj = box_left;
			        }
			        var resize_height = resize_obj.height();
			        resize_obj.height(resize_height + difference);
			    }

			    AutoAdjustHeights();

			    $(window).resize(function() {
			        AutoAdjustHeights();
			    });

			})
		;

        $("ul.link_loaders1 li")
			.each(function() {
			    if ($(this).hasClass("file-not-cached")) {
			        var theLink = $(this).children("a").hide();
			        var cloneLink = theLink.clone().addClass("loading_click").insertAfter(theLink).show();
			        cloneLink.click(function() {
			            theLink.show();
			            cloneLink.hide();
			        });
			    }
			})
		;

        $("a.loading_click")
			.click(function() {
			    the_lc = $(this);
			    function StartLoadSpin() {
			        if (typeof ls_timerID != 'undefined') {
			            clearTimeout(ls_timerID);
			        }
			        ls_timerID = self.setTimeout("the_lc.siblings('.ls_img').remove();", 60000);
			    }
			    if ($(this).siblings(".ls_img").length > 0) {
			        StartLoadSpin();
			    }
			    else {
			        the_lc.after('<span class="ls_img"><img src="assets/loadingspin.gif" width="16" height="16" alt="Loading..." /></span>');
			        StartLoadSpin();
			    }
			    the_ls = $(this).siblings().find("img");
			    self.setTimeout("the_ls.attr('src',the_ls.attr('src'))", 1);
			})
		;

        $("#subcolset_autoadjust")
			.each(function() {

			    var hdgs = $(this).find(".subcolset_col h4");
			    var resize_height = Math.max($(hdgs[0]).height(), $(hdgs[1]).height(), $(hdgs[2]).height());

			    function AutoAdjustHeights() {
			        hdgs.height(resize_height);
			    }

			    AutoAdjustHeights();

			    $(window).resize(function() {
			        AutoAdjustHeights();
			    });

			})
		;

        $("#invest2_pastoptions").tabs({
            collapsible: true,
            selected: -1
        });

        $("dl.expandable").each(function() {
            if ($(this).children().length > 10) {
                $(this).after('<dl class="expandable_sibling listing10"></dl>');
                var sibling = $(this).next('.expandable_sibling').hide();
                $(this).children().slice(10).appendTo(sibling);
                var viewmore_div = $(this).parent().find(".sub_link_btn.js_only").show();
                var viewmore_btn = $(this).parent().find(".sub_link_btn.js_only a");
                viewmore_btn.click(function() {
                    viewmore_btn.fadeOut(300);
                    viewmore_div.slideUp(250);
                    sibling.slideDown(400);
                    return false;
                });
            }
        });

        $(".invest2_colset4_autoadjust")
			.addClass("invest2_colset4_autoadjusted")
			.each(function() {

			    var all_insides = $(this).find(".invest2_colset4_col .invest2_inside_height");
			    var box_left = $(this).find(".invest2_colset4_col1 .invest2_inside_height");
			    var box_right = $(this).find(".invest2_colset4_col2 .invest2_inside_height");

			    function AutoAdjustHeights() {
			        if (ie6) {
			            all_insides.height("auto");
			            box_left.height(box_left.height());
			            box_right.height(box_right.height());

			            if (box_left.height() > box_right.height()) {
			                box_right.height(box_left.height());
			            }
			            if (box_right.height() > box_left.height()) {
			                box_left.height(box_right.height());
			            }
			        }
			        else {
			            all_insides.css("min-height", "0").height("auto");
			            box_left.css("min-height", box_left.height());
			            box_right.css("min-height", box_right.height());

			            if (box_left.height() > box_right.height()) {
			                box_right.css("min-height", box_left.height());
			            }
			            if (box_right.height() > box_left.height()) {
			                box_left.css("min-height", box_right.height());
			            }
			        }
			    }

			    AutoAdjustHeights();

			    $(window).resize(function() {
			        AutoAdjustHeights();
			    });

			    all_insides.resize(function() {
			        AutoAdjustHeights();
			    });

			})
		;

        // if page is an investment option page, 
        if (window.location.href.indexOf("investments_investment-options_") > 0) {
            var ColCont = $("#content .invest2_colset3_option");
            var Col1 = $("#content .invest2_colset3 .invest2_col1");
            var Col2 = $("#content .invest2_colset3 .invest2_col2");

            function AdjustColWidthForFlashGraph() {
                if (ColCont.width() < 1220) {
                    Col1.width(610);
                    Col2.width(ColCont.width() - 610);
                }
                else {
                    Col1.width("49.9%");
                    Col2.width("49.9%");
                }
            }

            $(window).resize(function() {
                AdjustColWidthForFlashGraph();
            });
            AdjustColWidthForFlashGraph();

        }

        $("#home_banner_left2 .banner_item .inside2").addClass("inside2_js");

        if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
            DD_belatedPNG.fix('.banner_item .inside2');
        }

        $("#home_banner_cycle")
			.after('<div id="home_banner_nav" />')
			.cycle({
			    fx: "fade",
			    speed: "500",
			    timeout: 9500,
			    pager: "#home_banner_nav"
			})
		;

        $("#hp_colset2_autoadjust")
            .addClass("hp_colset2_autoadjusted")
            .each(function() {

                var all_insides = $(this).find(".hp_colset2_col .hp_colset2_boxi2");
                var box_left = $(this).find(".hp_colset2_col1 .hp_colset2_boxi2");
                var box_right = $(this).find(".hp_colset2_col2 .hp_colset2_boxi2");

                function AutoAdjustHeights() {
                    all_insides.height("auto");
                    box_left.height(box_left.height());
                    box_right.height(box_right.height());

                    if (box_left.height() > box_right.height()) {
                        box_right.height(box_left.height());
                    }
                    if (box_right.height() > box_left.height()) {
                        box_left.height(box_right.height());
                    }
                }

                AutoAdjustHeights();

                $(window).resize(function() {
                    AutoAdjustHeights();
                });

            })
        ;


    });
}

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';
}
