/**********************************************************************************************

  CSS on Sails Framework
  Title: youth.gov.au
  Author: XHTMLized (http://www.xhtmlized.com/)
  Date: June 2010

***********************************************************************************************/

/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */

	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";

		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});

	};

})(jQuery);

$().ready(function() {

	//
	$('a[rel=_blank]').click(function(){
		this.target = "_blank";
	});

	//
	if (($.browser.msie)){

		//
		$('.panel-hottopic .panel-hottopic-nav li.active:last-child a').css({'z-index': '3', 'background-position':'-106px 100%' });
		$('table.default tbody tr:nth-child(even) th, table.default tbody tr:nth-child(even) td').css({'padding': '3px 0 2px 7px', 'background':'#ededed' });

		//
		$(".panel-hottopic .panel-hottopic-nav li:last-child a").hover(
			function () {
				$(this).css({'z-index': '3', 'background-position':'-106px 100%' });
			},
			function () {
				$(this).css({'z-index': '1', 'background-position':'-53px 100%' });
			}
		);

	}

	//
	if (($.browser.msie) && ($.browser.version == "8.0")){

		//
		$('html').addClass('ie8');

	}

	//
	if (($.browser.msie) && ($.browser.version == "7.0")){

		//
		$('html').addClass('ie7');

		//
		$('input[type="text"]').focus(function() {
			$(this).addClass('focus');
		});
		$('input[type="text"]').blur(function() {
			$(this).removeClass('focus');
		});

	}

	if (($.browser.msie) && ($.browser.version == "6.0")){

		//
		$('html').addClass('ie6');

		//
		$('input[type="text"]').focus(function() {
			$(this).addClass('focus');
		});
		$('input[type="text"]').blur(function() {
			$(this).removeClass('focus');
		});

		//
		$("#navigation li").hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);

		//
		$('input[type=text]').addClass("text");
		$('input[type=password]').addClass("text");
		$('input[type=button]').addClass("button");
		$('input[type=reset]').addClass("reset");
		$('input[type=submit]').addClass("submit");
		$('input[type=radio]').addClass("radio");
		$('input[type=checkbox]').addClass("checkbox");
		$('input[type=file]').addClass("file");
		$('input[type=image]').addClass("image");

		$('.h1 h1').addClass("fixPNG");
		$('.panel-hottopic .panel-hottopic-nav li a span').addClass("fixPNG");
		$('.panel-hottopic .panel-hottopic-content img').addClass("fixPNG");
		$('.panel-hottopic .panel-hottopic-content h2').addClass("fixPNG");
		$('.panel-hottopic .panel-hottopic-content').addClass("fixPNG");
		$('.panel-hottopic .panel-hottopic-content-wrapper').addClass("fixPNG");
		$('.panel-hottopic').addClass("fixPNG");
		$('#navigation').addClass("fixPNG");
		$('#header .logos a img').addClass("fixPNG");
		$('#header .tools-top form fieldset input[type="submit"]').addClass("fixPNG");
		$('#header .tools-top form fieldset input[type="text"]').addClass("fixPNG");
		$('#header .tools-top').addClass("fixPNG");
		$('#footer').addClass("fixPNG");
		DD_belatedPNG.fix('.fixPNG');

	}
	
	//$('#navigation').wrap("<div class='navWrap'></div>");
	//$('#navigation').corner("cc:#3B3B3B").parent().css({'padding': '4px 1px','background-color': '#3B3B3B','margin-bottom': '10px'}).corner("cc:#000");

});


/* ---------------------------------------------
elastoNav function extended from the code found on:
http://www.fluidbyte.net/dynamic-padding-horizontal-navigation
usage: elastoNav(selector, width);
where selector is the jquery comatible selector of the ul you need to fit and 
width is the width that you need everything to fit in to as a number.
eg: elastoNav('.navMenu', 900)
------------------------------------------------ */

function elastoNav(dID, w){
	var curwidth = 0;
	var totalelements = 0;
	var targetstring = dID + ' > li a'; // first level decendants only
	$(targetstring).each(function(){
		//remove padding first
		$(this).css('padding-left', '0px').css('padding-right', '0px');
		//then calculate the width
		curwidth = curwidth + $(this).outerWidth();
		totalelements = totalelements+1;
	});
	//test to make sure they are going to fit first.
	if ((w-curwidth)>0){
		//figure out the new width
	 var newWidth = Math.floor((w/totalelements));
	 //apply it
	 $(targetstring).css('width', newWidth+'px');
	
	 // Check for remainder
	 var finalWidth = (totalelements*(newWidth));
	 var remainder = w-finalWidth;
	 
	 // Add padding to last element to close remainder gap 
	 if(finalWidth<w){
	     $(targetstring).eq(totalelements-1).css('padding-right',(remainder)+'px');
	   } 
	}
}

// test border-radius support
jQuery(function() {
	jQuery.support.borderRadius = false;
	jQuery.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function() {
		if(document.body.style[this] !== undefined) jQuery.support.borderRadius = true;
		return (!jQuery.support.borderRadius);
	});
});


function equalHeightNav() {
	// set the li and a elements within #navigation to be of equal height (based on the a with largest height)
	var navHeight = $('#navigation').innerHeight();
	var navVertPadding = parseInt($('#navigation').css('padding-top'),10) + parseInt($('#navigation').css('padding-bottom'), 10);
	var liVertPadding = parseInt($('#navigation > li:first').css('padding-top'), 10) + parseInt($('#navigation > li:first').css('padding-bottom'), 10);
	var aVertPadding = parseInt($('#navigation > li:first > a').css('padding-top'), 10) + parseInt($('#navigation > li:first > a').css('padding-bottom'), 10);
	$('#navigation > li').css('height', (navHeight - navVertPadding - liVertPadding) + 'px');
	$('#navigation > li > a').css('height', (navHeight - navVertPadding - liVertPadding - aVertPadding) + 'px');
}

$().ready(function(){
	//elastoNav('#navigation', 932); //make the menu fit the width of the page
	equalHeightNav();
	if(!$.support.borderRadius) {
		// if no native support for border-radius, use jQuery corner plugin (it uses border-radius when supported, but buggily)
		$('#navigation li a').corner("10px");
	}
	/* a little adjustment for rightmost tertiary nav, if present */
	$('#navigation > li:last ul ul').css({'left': 'auto', 'right': '100%'}); 
});

/*  **********************************
 *  *** Add rounded corners for ie ***
 *  **********************************
 *  Requires: jQuery, jQuery Corners 1.2 feature turned on.
 */
 
 function roundCorners(){
	 $('html.ie6 .niceButton').corner();
 	 $('html.ie7 .niceButton').corner();
 	 $('html.ie8 .niceButton').corner();
 }
 
 $().ready(function(){roundCorners();});


/*  ******************************************
 *  ***jQuery based banner for Youth.gov.au***
 *  ******************************************
 *  Requires: jQuery            - http://jquery.com/
 *            jQuery cycle      - http://jquery.malsup.com/cycle/
 */


// **** BANNER VARS ****
var banner_speed = 400;  //transition speed in ms
var banner_timeout = 6000;  //interval betwen animations in ms

var banner_id = "#slideContainer"; // the container of the slides
var banner_buttons = ".jquerybanner_bar"; // the container of the navigation items
var banner_toggle = "#jquerybanner_stopstart"; // stop/start toggle button
var startingSlideIndex = 0; //defaults to 0, but overwritten with a random value in init();

function init(){
	//add tabindex value to stop/start button
	$(banner_toggle).attr('tabindex', '0');
	//get the random start value
	startingSlideIndex = Math.floor(Math.random()*$('.slide').length);
	
    $(banner_id).cycle({
        speed: banner_speed,
        pager: banner_buttons,
        timeout: banner_timeout,
        startingSlide: startingSlideIndex,
        cleartypeNoBg: true,
        pause: 1,
        pauseOnPagerHover: 1,
	pagerAnchorBuilder: function(index, slide){
		return '<a href="#" title="'+getTitle(index)+'">'+(index+1)+'</a>';
	}	
    });
	//pause the banner by default
    $(banner_id).cycle('pause');
    
    //replicate click functionality on the stop/start div if keyboard is used
    $(banner_toggle).keypress(function(event){
        //if enter is pressed
        if (event.keyCode == '13') {
	    	event.preventDefault();
	    	bannerToggle();    	
	    }
	});

    $(banner_toggle).click(function(){
    	bannerToggle();
    });

	function bannerToggle(){
  	      //toggle the animation
  	      $(banner_id).cycle('toggle');
  	      //now adjust the image and alt text of the stop/start button
  	      if($(banner_toggle +' img').attr('src')=='/Style Library/Images/btn_stop.gif'){
		        $(banner_toggle +' img').attr('src', '/Style Library/Images/btn_play.gif');
	   	        $(banner_toggle +' img').attr('alt', 'Start the animation');
	        } else {
			//immediately load the next slide
			$(banner_id).cycle('next');
	   	        $(banner_toggle +' img').attr('src', '/Style Library/Images/btn_stop.gif');
	   	        $(banner_toggle +' img').attr('alt', 'Stop the animation');
        	}

	}

	function getTitle(index){
		var title = $('.slide h2').eq(index).html();
		return title;
	}
}

$().ready(function(){init();});



