$(document).ready(function() {

	// Remove from screen
	$('.tabbed-content .tab-title').hide();
	$('.additional-info-text').hide();

	// Page Tools
	$('#layout-2col #column1').append('<div class="page-tools container"><h2>Page tools</h2><ul><li class="font-size-large"><a href="#" title="Increase page text size">Increase page text size</a></li><li class="font-size-small"><a href="#" title="Decrease page text size">Decrease page text size</a></li><li class="print"><a href="#" title="Print friendly">Print friendly</a></li><li class="share"><a href="#" title="Email this page">Email this page</a></li></ul></div>')


		// Corners
		$('.container').corner("6px");
		$('.toc').corner("5px");
		$('.toc ul').corner("5px");
		$('.main-content .ruling-info .info-panel').corner("5px");
		$('.latest-news  h2').corner("3px");
		$('.container-promo h2').corner("3px");
		$('.container-list h2').corner("3px");
	
	
		$('.nav-homepage').corner("5px");
		$('.nav-roles .common-links h3').corner("5px");	
		$('.news .date').corner("6px");

	// Navigation Style Fixes
	$('.nav-secondary ul li:first').addClass("no-border");

	$('.nav-secondary ul li .xcc').parent().addClass("selected");
	$('.nav-secondary ul li .xcc').parent().next().addClass("no-border");

	$('.nav-secondary ul li .xcx a').replaceWith('<a href="#" title="Hide / Show more menu options" class="hidden"></a>');
	$('.nav-secondary ul li .xcc a').replaceWith('<span href="#" class="show"></span>');
	$('.nav-secondary ul li .xcx, .nav-secondary ul li .xcc').show();

	$('.nav-secondary ul li .xcx a').click(function(){
		if ($(this).hasClass("hidden")==true) {
			$(this).addClass("show");
			$(this).removeClass("hidden");
			$(this).parent().parent().children("ul").slideDown();
			return false;
		} else {
			$(this).addClass("hidden");
			$(this).removeClass("show");
			$(this).parent().parent().children("ul").slideUp();
			return false;
		}
	});


	if ($('.breadcrumb').width() > 680) {

		charLimit = 20;
		$('.breadcrumb ul li a, .breadcrumb ul li.selected').each(function(i){
			this.title = $(this).html();
			breadcrumbName = $(this).html();
			charCount = breadcrumbName.length;
			if (charCount > charLimit) {
				breadcrumbName = breadcrumbName.substr(0,charLimit) + '...';
				$(this).html(breadcrumbName);
			};
		});

	};

	$('.breadcrumb').show();


	// Promo Margin Fixes
	$('.promos .promo-text:first').addClass("first");
 
	// Main Links
	$('.main-links-2col ul li').addClass("float");
	$('.main-links-2col ul li:odd').after('<li class="clearfix" style="display: block"></li>');

	$('.main-links-3col ul li').addClass("float");
	$('.main-links-3col ul li:eq(2)').after('<li class="clearfix" style="display: block"></li>');





	// Print Links
	$('.page-tools .print').click(function(){
		window.print();
		return false;
	});

	$('.page-tools .font-size-large').click(function(){
		 $("body").css("font-size","75%");
		return false;
	});

	$('.page-tools .font-size-small').click(function(){
		 $("body").css("font-size","68%");
		return false;
	});

	$('.page-tools .share a, .i-want-to .icon-share').attr('href',"mailto:?subject=Thought you might be interested in this ...&body=I've found this on the Queensland Office of State Revenue website and I thought you might be interested:  " + location.href);

	if(window.opera) {
		if ($(".i-want-to .icon-bookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
			$(".i-want-to .icon-bookmark").attr("rel","sidebar");
		}
	}

	$(".i-want-to .icon-bookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = document.location;
		var title = document.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}
	});

	// Inline Tabs
	var lastTab = false;
	var lastTabNumber = 0;

	var totalTabNumbers = $('.tabbed-content .tabs').size();
	if (totalTabNumbers == 9) {
		var TabWidths = 76.77
	} else {
		var TabWidths = 700 / totalTabNumbers - 1;
	};
	
	$('.tabbed-content').prepend('<div id="tabs-nav"><ul></ul></div>')
	$('.tabs').hide();

	$('.tabbed-content .tabs').each(function(i){
		this.id = 'tab'+i;
		next = i + 1;
		nextTab = 'tab'+next;
		previous = i - 1;
		previousTab = 'tab'+previous;
		var texttitle = $(this).children("h2.tab-title").text();
		var title = (texttitle) ? texttitle : this.id;
		var link = '<a href="#' + this.id + '"><span>' + title + '</span></a>';
		$('#tabs-nav ul').append('<li>'+link+'</li>');
		if(i===0) $('.tabs:not(#'+this.id+')').hide();
		if(i>0){
			previousText = '<div class="previous-tab"><a href="#' + previousTab + '" id="' + previous + '" class="icon-previous">Show previous</a></div>';
		} else {
			previousText = '';
		};
		nextText = '<div class="next-tab"><a href="#' + nextTab + '" id="' + next + '" class="icon-next">Show next</a></div>';
		$(this).append('<div class="tab-switcher">' + previousText + '' + nextText + '</div>');
	});

	$('.tabbed-content #tabs-nav li').width(TabWidths);
	$('.tabbed-content #tabs-nav li:last').css('background','none');

	var tabHeight = 0;
	$('#tabs-nav ul li a span').each(function(i){
		if ($(this).height() > tabHeight) {
			tabHeight = $(this).height();
		};
	});

	$("#tabs-nav ul li a span").height(tabHeight);

	// Tab Switch based on QueryString
	var defaultTab = true;

	if ($('.news-tabs').length > 0) {	
		lastTab = true;
		lastTabNumber = totalTabNumbers - 1;
	};

	if ($.query.get('tab').length > 0) {
		tabNumber = $.query.get('tab');
		tabNumber = tabNumber.split('tab')[1];
		defaultTab = false;
	} else {
		if ($.query.get('showtab')) {	
			tabNumber = $.query.get('showtab') - 1;
			defaultTab = false;
		} else {
			if (lastTab == true) {	
				tabNumber = lastTabNumber;
				defaultTab = false;
			} else {
				defaultTab = true;
			};
		};
	};
	
	if (defaultTab == true) {
		$('#tabs-nav ul li:first a').addClass("selected");
		$('.tabs:first').show();
	} else {
		$("#tabs-nav ul li a:eq(" + tabNumber + ")").addClass("selected");
		$(".tabs:eq(" + tabNumber + ")").show();
	};
	

	$('.tabbed-content .tabs:last .tab-switcher .next-tab').hide();

	$('.tabbed-content #tabs-nav a').click(function(){
		$('#tabs-nav ul li a').removeClass();
		$(this).addClass("selected");
		var id = '#'+this.href.split('#')[1];
		$('.tabs').hide();
		$(id).show();
		return false;
	})

	$('.tabbed-content .tab-switcher a').click(function(){
		$('#tabs-nav ul li a').removeClass();
		$('#tabs-nav ul li a:eq('+this.id+')').addClass("selected");
		var id = '#'+this.href.split('#')[1];
		$('.tabs').hide();
		$(id).show();
		return false;
	})



	// Additional Info Panel
	$('.additional-info-link').click(function(){
		$(".additional-info-text").slideToggle("slow");
	})

	// Additional Info Panel
		$(".dropdown-list .list").hide();

		$(".dropdown-list h4").hover(
		  function () {$(".dropdown-list h4").addClass("hover");}, 
		  function () {$(".dropdown-list h4").removeClass("hover");}
		);
	
		$('.dropdown-list .button-link').click(function(){
			$(".dropdown-list").toggleClass("open");
			$(".dropdown-list .list").slideToggle("fast");
		})
	
	


	// Expandable Content
	$('.expandable-content dd, .accordion dd').hide();
	$('.expandable-content dl').after('<div class="expand-all all-link"><span class="link">Expand all</span> <span class="indicator">[<span class="expand">+</span>]</span></div>')
	$('.expandable-content dl').after('<div class="collapse-all all-link"><span class="link">Collapse all</span> <span class="indicator">[<span class="collapse">-</span>]</span></div>')
	$('.expandable-content .collapse-all').hide();

	$('.expandable-content dt, .accordion dt').prepend('<span class="expand-item indicator">[<span class="expand">+</span>]</span>')
	$('.expandable-content dt, .accordion dt').prepend('<span class="collapse-item indicator">[<span class="collapse">-</span>]</span>')
	$('.expandable-content .collapse-item, .accordion .collapse-item').hide();
	$(".expandable-content dd, .accordion dd").wrapInner('<span class="block"></span>');

	$('.expandable-content dt').click(function(){
		if ($(this).hasClass('expanded')==false){
			$(this).next().slideDown();
			$(this).children('.expand-item').hide();
			$(this).children('.collapse-item').show();
			$(this).addClass('expanded');
		} else {
			$(this).next().slideUp();
			$(this).children('.expand-item').show();
			$(this).children('.collapse-item').hide();
			$(this).removeClass('expanded');
		}
	});

	$('.expandable-content .expand-all').click(function(){
		$('.expandable-content dd').slideDown();
		$('.expandable-content .expand-all').hide();
		$('.expandable-content .collapse-all').show();
		$('.expandable-content .expand-item').hide();
		$('.expandable-content .collapse-item').show();
		$('.expandable-content dt').addClass('expanded');
	});

	$('.expandable-content .collapse-all').click(function(){
		$('.expandable-content dd').slideUp();
		$('.expandable-content .expand-all').show();
		$('.expandable-content .collapse-all').hide();
		$('.expandable-content .expand-item').show();
		$('.expandable-content .collapse-item').hide();
		$('.expandable-content dt').removeClass('expanded');
	});

	$('.accordion dt').click(function(){
		if ($(this).hasClass('expanded')==false){
			$('.accordion dt').removeClass('expanded');
			$('.accordion .expand-item').show();
			$('.accordion .collapse-item').hide();
			$('.accordion dt').next().slideUp();
			$(this).next().slideDown();
			$(this).addClass('expanded');
			$(this).children('.expand-item').hide();
			$(this).children('.collapse-item').show();
		} else {
			$('.accordion dt').next().slideUp();
			$(this).removeClass('expanded');
			$('.accordion .expand-item').show();
			$('.accordion .collapse-item').hide();
		}
	});


	// System Requirements
	var SystemRequirementsGood = '<div class="system-check supported">Your browser is fully supported.</div>';
	//var SystemRequirementsUntested = '<div class="system-check semi-supported">Your browser has not been fully tested and may not work properly with our system.</div>';
	var SystemRequirementsBad = '<div class="system-check unsupported">Your browser is not supported.</div>';
	var SystemRequirementsIs = SystemRequirementsBad;
	
	var userAgent = navigator.userAgent;
	
	// Windows XP or Vista only.
	if (userAgent.indexOf("Windows NT 5.1") != -1 || userAgent.indexOf("Windows NT 6.0") != -1) {
		
		// Firefox 2 only.
		if($.browser.mozilla){
			var version = $.browser.version;
			if (isNaN(version)) {
				var parts = version.split('.');
				version = parts.shift() + "." + parts.join('');
			}
			
			if (version >= 1.81 && version < 1.9) {
				SystemRequirementsIs = SystemRequirementsGood;
			}
		}
		
		// Internet Explorer 7 only.
		if($.browser.msie) {
			if($.browser.version >= 7 && $.browser.version < 8){
				SystemRequirementsIs = SystemRequirementsGood;
			}
		}
	}
	
	$('.system-requirements').append(SystemRequirementsIs);




	// Homepage tabs
	$("#primary-nav-roles a, #primary-nav-browse a, #primary-nav-search a").attr("href","javascript:void(0);");
	$(".homepage #nav-primary li").removeClass("selected");




	if ($.query.get('show').length > 0) {	
		$(".nav-homepage div[class*='" + $.query.get('show') + "']").show();
		$(".homepage #nav-primary li[id*='" + $.query.get('show') + "']").addClass("selected");
	} else {

	    // Check Cookie
	    var homepageTabCookie = $.cookie('homepageTabCookie');
		if (homepageTabCookie == null) {	
			$("#primary-nav-roles").addClass("selected");
			$(".nav-roles").show();
		} else {
			$(".nav-homepage div[class*='" + homepageTabCookie + "']").show();
			$(".homepage #nav-primary li[id*='" + homepageTabCookie + "']").addClass("selected");
		};
	};


	$("#primary-nav-roles").click(function(){
		$(".nav-browse, .nav-search").hide();
		$("#nav-primary li:not(#"+this.id+")").removeClass("selected");
		$(this).addClass("selected");
		$(".nav-roles").show();
	    // Set Cookie
		$.cookie('homepageTabCookie', 'nav-roles');
	});
	$("#primary-nav-browse").click(function(){
		$(".nav-roles, .nav-search").hide();
		$("#nav-primary li:not(#"+this.id+")").removeClass("selected");
		$(this).addClass("selected");
		$(".nav-browse").show();
	    // Set Cookie
		$.cookie('homepageTabCookie', 'nav-browse');
	});	
	$("#primary-nav-search").click(function(){
		$(".nav-roles, .nav-browse").hide();
		$("#nav-primary li:not(#"+this.id+")").removeClass("selected");
		$(this).addClass("selected");		
		$(".nav-search").show();
		$("#large-search #large-search-query").focus('');
	    // Set Cookie
		$.cookie('homepageTabCookie', 'nav-search');
	});

	// Role based navigation
	$(".nav-roles .panel h2").hide();

	$('.nav-roles .inner').prepend('<div class="navigation"><ul></ul></div>')
	var numPanels = $('.nav-roles .panel').length;
	$('.nav-roles .panel').each(function(i){
		if(!$(this).hasClass("introduction")){
			this.id = 'tab'+i;
			var texttitle = $(this).find("h2").text();
			var title = (texttitle) ? texttitle : this.id;
			var link;
			
			if(i===1){
				var link = '<li><a href="#' + this.id + '"><span class="bottomright"></span><span class="topleft"></span><span class="bottomleft"></span>' + title + '</a></li>';
			} else if (i===(numPanels-1)) {
				var link = '<li class="last-child"><a href="#' + this.id + '"><span class="topright"></span><span class="topleft"></span><span class="bottomleft"></span>' + title + '</a></li>';
			} else {
				var link = '<li><a href="#' + this.id + '"><span class="topright"></span><span class="bottomright"></span><span class="topleft"></span><span class="bottomleft"></span>' + title + '</a></li>';
			}
			$('.nav-roles .navigation ul').append(link);
			$('.nav-roles .panel').hide();
		}
	});
	
	// Check Cookie
	var homepageBrowseTabCookie = $.cookie('homepageBrowseTabCookie');
	if (homepageBrowseTabCookie == null) {	
		$('.nav-roles .introduction').show();
	} else {
		$(homepageBrowseTabCookie).parent("li").addClass("selected");
		$(".nav-roles .navigation li a[@href^='" + homepageBrowseTabCookie + "']").parent("li").addClass("selected");
		$('.nav-roles .panel').hide();
		$(homepageBrowseTabCookie).show();
	};
	
	$('.nav-roles .navigation a').click(function(){
		$('.nav-roles ul li').removeClass("selected");
		$(this).parent("li").addClass("selected");
		var id = '#'+this.href.split('#')[1];
		$('.nav-roles .panel').hide();
		$(id).show();
		// Set Cookie
		$.cookie('homepageBrowseTabCookie', id);
		return false;
	})

	// Form
    $("form .textbox, form .textarea, form .dropdown").hover(
      function () {
         $(this).addClass("hover");
      }, 
      function () {
         $(this).removeClass("hover");
      }
    );
	$("form .textbox, form .textarea, form .dropdown").focus(function () {
         $(this).addClass("active");
    });
	$("form .textbox, form .textarea, form .dropdown").blur(function () {
         $(this).removeClass("active");
    });



	// Glossary
	$(".glossary h2").hide();
	$('.glossary dl').hide();

	$('.glossary').prepend('<div class="navigation"><ul></ul></div>')
	var numLetters = $('.glossary h2').length;
	$('.glossary h2').each(function(i){
		var title = $(this).text();
		var link;
		if ($('.glossary #'+title).length > 0) {	
			link = '<li class="' + title + '"><a href="#' + title + '"">' + title + '<span class="bottom"></span></a></li>';
		} else {
			link = '<li><span class="noitems">' + title + '</span></li>';
		}			
		$('.glossary .navigation ul').append(link);
	});
	
	$('.glossary .navigation a').click(function(){
		$('.glossary .navigation ul li').removeClass("selected");
		$(this).parent("li").addClass("selected");
		var id = '#'+this.href.split('#')[1];
		$('.glossary dl').hide();
		$(id).show();
		return false;
	});


	if ($.query.get('letter').length > 0) {
		letter = $.query.get('letter');
		letter = letter.toUpperCase();
		
		$(".glossary .navigation li." + letter + "").addClass("selected");
		$(".glossary dl#" + letter + "").show();
	} else {
		$(".glossary dl:first").show();
		$(".glossary .navigation li:first").addClass("selected");
	};





	$(".nav-paging li.selected a").append('<span class="bottom"></span>');
	
	
	// Blur and focus events for search textbox
	$(".nav-search .search-form #large-search-query").attr("value","Type what you're looking for...");	
	$(".nav-search .search-form #large-search-query").focus(function (){
		if ($(this).attr("value") == "Type what you're looking for...") {
			$(this).attr("value","");
		} 
	});
	$(".nav-search .search-form #large-search-query").blur(function (){ 
		if ($(this).attr("value") == "") {
			$(this).attr("value","Type what you're looking for...");
		}
	});		




	// Blur and focus events for filter search textbox
	$(".filter .search #filter-search-query").attr("value","Enter keywords ...");	
	$(".filter .search #filter-search-query").addClass("not-active");

	$(".filter .search #filter-search-query").focus(function (){
		if ($(this).attr("value") == "Enter keywords ...") {
			$(this).toggleClass("not-active");
			$(this).attr("value","");
		} 
	});
	$(".filter .search #filter-search-query").blur(function (){ 
		if ($(this).attr("value") == "") {
			$(this).toggleClass("not-active");
			$(this).attr("value","Enter keywords ...");
		}
	});		



	if ($('table.sortable').length > 0) {
		// Table Sorting
//		$('table.sortable tbody').each(function (){
//			$(this).children('tr:odd').addClass('odd');
//			$(this).children('tr:even').addClass('even');
//		});		
	
		$("table.sortable thead th").hover(
		  function () {$(this).addClass("hover");}, 
		  function () {$(this).removeClass("hover");}
		);
	
		$("table.sortable").tablesorter({widgets: ['zebra'], headers: { 
            // assign the secound column (we start counting zero) 
            0: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }}}); 
	
//		$("table.sortable thead tr th").click(function (){ 
		
//			$(this).parent().parent().parent().children("tbody").children("tr").removeClass();
	
//			$(this).parent().parent().parent().children("tbody").children("tr:odd").addClass('odd');
//			$(this).parent().parent().parent().children("tbody").children("tr:even").addClass('even');
	
//		});		
	}

	// Open all SAP links in a new browser window.
	$("a[@href*='amun.osr.qld.gov.au']").attr("target", "_blank");
	$("a[@href*='troy.osr.qld.gov.au']").attr("target", "_blank");

		var now = new Date();
		var days = new Array(
			'Sunday','Monday','Tuesday',
			'Wednesday','Thursday','Friday','Saturday');
		var months = new Array(
			'January','February','March','April','May',
			'June','July','August','September','October',
			'November','December');
		var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
		function fourdigits(number)	{
			return (number < 1000) ? number + 1900 : number;}
		today = days[now.getDay()] + ", " +
				months[now.getMonth()] + " " +
				date + ", " +
				(fourdigits(now.getYear()));

// Print Header
//	$('#header').after('<div id="print-header" class="clearfix"><div class="titles"><div class="qld-logo"><img src="../../images/qglogo.gif"></div><h3>Queensland Treasury</h3><h2>Office of State Revenue</h2></div><div class="date">This page was printed on <strong>' + today +'</strong></div><div class="website">http://www.osr.qld.gov.au</div></div>');

	$('#header').before('<div id="print-header" class="clearfix"><div class="titles"><img src="/_assets/images/print-header.jpg"/></div><div class="date">This page was printed on <strong>' + today +'</strong></div></div>');


// Contact Us Form

if ($('.AddToEnquiryForm a').length > 0) {
	if ($('#Enquirytype').length > 0) {

		$('.AddToEnquiryForm a').each(function(i){

			thisAddress = $(this).text();
			$("#Enquirytype").append('<option value="' +  thisAddress + '">' + $(this).attr("title") + '</option>');

			$(this).replaceWith('<div class="view-more-link"><span class="text-button"><a href="#EnquiryForm" title="' + $(this).attr("title") + '" class="blue-button"><span>' + $(this).attr("title") + ' enquiry</span></a></span></div>');
		});

		$('.AddToEnquiryForm h4').hide()

	};

	$('.AddToEnquiryForm a').click(function(){
		$("#Enquirytype option:contains('" + $(this).attr("title") + "')").attr('selected', 'selected');
		$(".form form .enquiry").effect("highlight", { color: '#b9f3af' }, 1500);
	});		

};



	if ($('.promos').length > 0) {
		var promo1 = $(".promos .container:first").height();
		var promo2 = $(".promos .container:last").height();
		if (promo1 > promo2) {
			$(".promos .container:last").height(promo1);
		} else {
			$(".promos .container:first").height(promo2);

		};


};

	// Remove the subheaders before the table is sorted.
	$("table.sortable").bind("sortStart", function() {
		$("table.sortable .table-header").remove();
		$("table.sortable").trigger("update");
	});

});

