$(document).ready(function(){
	
	if($.browser.msie && $.browser.version<7){
	
		// IE6 - pseudo class :hover for footer links
		$('div#footer h2, div#footer ul.columns h3, div#footer ul.columns a').css({color:'#666'});
		$('div#footer, div#footer ul.columns li').hover(
			function () {
				$(this).addClass('hover');
			}, 
			function () {
				$(this).removeClass('hover');
			}
		);
		
		// background images in IE6 must cache
		try {
			document.execCommand("BackgroundImageCache",false,true);
		} catch(err) {
			// handle errors here - do nothing
		}
		
	} // end IE 6 fixes

	// ensure main content area is always at least as tall as the left nav
	$("#grid").css(($.browser.msie && $.browser.version < 7 ? '' : 'min-') + "height", ($("#nav-left").height() + 10) + "px");
	
	// duplicate A to Z to the bottom of the page
	$("#atoz-second").html($("#atoz-first").html());
	
	// take an un-ordered list with a class of makemenu and make it into a drop down menu
	// written by gmg710 Centrelink
	$('ul.makemenu').each(function(){
	
		$(this).css("display", "none");
		
		var date = new Date();
		var now = date.getTime();
		
		$(this).after('<form id="' + now + '_form" action="" style="display: inline" class="menu"><select id="' + now + '_select"><\/select> <input class="go-btn-small" value="Go" type="submit" /><\/form>');
		
		$(this).find("li").each(function(){
		
			var a = $(this).find("a")

			$('#' + now + '_select').append('<option value="' + a.attr("href") + '">' + a.text() + '<\/option>');
			
		});
		
		$('#' + now + '_form').submit(function(){
			document.location = $('#' + now + '_select').val();
			return false;
		});
		
	}); // end makemenu
	
	// Loop through the links in the body of the page and create a print reference section (only visible for print)
	// written by gmg710 Centrelink
	// create a heading, ordered list at the end of the page and hide it
	$('#docBody').append('<h2 class="printlinks">Link references<\/h2><ol class="printlinks"><\/ol>');
	
	// loop through all of the links in #docBody that are internal or external without the class of noprint
	$('#docBody').find('a[href]').each(function(){
		
		var that = this; // we can still refer to the link later
		
		// create a full absolute link from the href value
		var expandedLink = "";
		if ($.isUrlInternal($(this).attr('href'))){
			expandedLink = window.location.protocol + "//" + window.location.hostname + "/" + $(this).attr('href').replace(/^\//, "");
		} else if ($.isUrlFragment($(this).attr('href')) || $(this).hasClass('noprint')) {
			// we don't want to do anything with an anchor or 'noprint' link so change the CSS (no underline for print) and skip it
			$(this).addClass("anchor");
			return true;
		} else {
			expandedLink = $(this).attr('href');
		}
		
		// if the link exists in the list, append the number to the link in a sup[num]
		var existsAlready = false, refCount = 1;
		
		$('ol.printlinks li').each(function(refnum){
			if (!existsAlready){
				if (expandedLink == $(this).text()){
					var ref = refnum + 1;
					$(that).after('<sup class="printonly">[' + ref + ']<\/sup>');
					existsAlready = true;
				}
			}
			refCount++;
		});
		
		// else append it to the list and append that new number to the link in a sup[num]
		if (!existsAlready){
			$('ol.printlinks').append('<li>' + expandedLink + '<\/li>');
			$(this).after('<sup class="printonly">[' + refCount + ']<\/sup>');
		}
		
	}); // end print links
	
}); // end $(document).ready()


// IE print changes
window.onbeforeprint = function(){ // IE only - Firefox is done through CSS2 and CSS3

	$('div#breadcrumbs ul').find('li').each(function(){
		if (/^You\ are\ here/.test($(this).text())){
			$(this).css("display", "none");
		}
		if(/^Home/.test($(this).text())){
			$(this).text("www.centrelink.gov.au > ");
		}
		
	});

};

// IE print changes - set everything back
window.onafterprint = function(){ // IE only - set everything back
	$('div#breadcrumbs ul').find('li').each(function(){
		if (/^You\ are\ here/.test($(this).text())){
			$(this).css("display", "inline");
		}
		if(/^www/.test($(this).text())){
			$(this).html('<a href="/">Home<\/a> >  ');
		}
		
	});
};

// Old functions used with applicationsfunction openURLinWindow(winName,url)
function openURLinWindow(winName,url)
	{
		var agt=navigator.userAgent.toLowerCase(); 
		var is_gecko = (agt.indexOf('gecko') != -1);
		var is_opera = (agt.indexOf('opera') !==-1);
		var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
		var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
		var vheight = screen.height*0.85;
		var vwidth = screen.width*0.95;
		var features = "scrollbars=yes,menubar=no,locationbar=no,toolbar=no,resizable=yes,dependant=yes,alwaysRaised=yes,hotkeys=no,directories=no,status=yes,height=" + vheight + ",width=" + vwidth;

		if (is_ie)
			{ // Internet Explorer
				features = features + ",left=0,top=0";
				remote = window.open(url,winName,features,"replaceFlag=yes");		 
				if (window.focus) { remote.focus();}		 		 
			}
		else
			{ // Netscape + others
				features = features + ",screenX=0,screenY=0";
				remote = window.open(url,winName,features,"replaceFlag=yes");
				remote.focus();		 		 		 
			}		 		 
	}

function setSecureWindow(url)
	{
		openURLinWindow('',url);
	}

	function registerOnLoad(func){
	if (window.addEventListener) { // w3c
		window.addEventListener('load', func, false); 
	} else if (window.attachEvent) { // IE
		window.attachEvent('onload', func);
	} else {
		oldLoad = window.onload;
		window.onload = function(){
			if (typeof(oldLoad) === "function"){
				func();
				oldLoad();
				return;
			}
		}
	}
}


// Search validation
function funnelbackSearch(form)
	{
				
		var searchQuery = (form) ? form.getElementsByTagName("input")[0].value : document.getElementById("query").value;
		
		if ((searchQuery === "") || (searchQuery === "search keywords") || (searchQuery === "search this website"))
			{
				alert("You have not entered a search term. Please enter a valid search term and try again.");
				return false;
			}
		
		return true;
		
	}

	/*
 * urlInternal - v1.0 - 10/7/2009
 * http://benalman.com/projects/jquery-urlinternal-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
 
(function ($) {
    var g, i = !0,
        r = !1,
        m = window.location,
        h = Array.prototype.slice,
        b = m.href.match(/^((https?:\/\/.*?\/)?[^#]*)#?.*$/),
        u = b[1] + "#",
        t = b[2],
        e, l, f, q, c, j, x = "elemUrlAttr",
        k = "href",
        y = "src",
        p = "urlInternal",
        d = "urlExternal",
        n = "urlFragment",
        a, s = {};

    function w(A) {
        var z = h.call(arguments, 1);
        return function () {
            return A.apply(this, z.concat(h.call(arguments)))
        }
    }
    $.isUrlInternal = q = function (z) {
        if (!z || j(z)) {
            return g
        }
        if (a.test(z)) {
            return i
        }
        if (/^(?:https?:)?\/\//i.test(z)) {
            return r
        }
        if (/^[a-z\d.-]+:/i.test(z)) {
            return g
        }
        return i
    };
    $.isUrlExternal = c = function (z) {
        var A = q(z);
        return typeof A === "boolean" ? !A : A
    };
    $.isUrlFragment = j = function (z) {
        var A = (z || "").match(/^([^#]?)([^#]*#).*$/);
        return !!A && (A[2] === "#" || z.indexOf(u) === 0 || (A[1] === "/" ? t + A[2] === u : !/^https?:\/\//i.test(z) && $('<a href="' + z + '"/>')[0].href.indexOf(u) === 0))
    };

    function v(A, z) {
        return this.filter(":" + A + (z ? "(" + z + ")" : ""))
    }
    $.fn[p] = w(v, p);
    $.fn[d] = w(v, d);
    $.fn[n] = w(v, n);

    function o(D, C, B, A) {
        var z = A[3] || e()[(C.nodeName || "").toLowerCase()] || "";
        return z ? !!D(C.getAttribute(z)) : r
    }
    $.expr[":"][p] = w(o, q);
    $.expr[":"][d] = w(o, c);
    $.expr[":"][n] = w(o, j);
    $[x] || ($[x] = function (z) {
        return $.extend(s, z)
    })({
        a: k,
        base: k,
        iframe: y,
        img: y,
        input: y,
        form: "action",
        link: k,
        script: y
    });
    e = $[x];
    $.urlInternalHost = l = function (B) {
        B = B ? "(?:(?:" + Array.prototype.join.call(arguments, "|") + ")\\.)?" : "";
        var A = new RegExp("^" + B + "(.*)", "i"),
            z = "^(?:" + m.protocol + ")?//" + m.hostname.replace(A, B + "$1").replace(/\\?\./g, "\\.") + (m.port ? ":" + m.port : "") + "/";
        return f(z)
    };
    $.urlInternalRegExp = f = function (z) {
        if (z) {
            a = typeof z === "string" ? new RegExp(z, "i") : z
        }
        return a
    };
    l("www")
})(jQuery);