/* 
Slider Functions
*/

function slideSwitch() {
    var $active = $('div.slider ul.slides li.active');
	var $activecontrol = $('#slider div.controls ul li.active');

    if ( $active.length == 0 ) $active = $('div.slider ul.slides li:last');
	if ( $activecontrol.length == 0 ) $activecontrol = $('#slider div.controls ul li:last');

    var $next =  $active.next().length ? $active.next()
        : $('div.slider ul.slides li:first');
		
	var $nextcontrol =  $activecontrol.next().length ? $activecontrol.next()
        : $('#slider div.controls ul li:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1250, function() {
            $active.removeClass('active last-active');
        });
		
	$nextcontrol.addClass('active');
	$activecontrol.removeClass('active');
}

function slideSelect(index) {
	
    var $selected = $('div.slider ul.slides li:nth-child(' + (index+1) +')');
	var $active = $('div.slider ul.slides li.active');
	
	var $selectedcontrol = $('#slider div.controls ul li:nth-child(' + (index+1) +')');
	var $activecontrol = $('#slider div.controls ul li.active');
	
    if ($active.length == 0 ) $active = $('div.slider ul.slides li:last')
	if ($activecontrol.length == 0) $activecontrol = $('#slider div.controls ul li:last');

    $active.addClass('last-active');
	
	$selected.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 200, function() {
            $active.removeClass('active last-active');
        });
	
	$selectedcontrol.addClass('active');
    $activecontrol.removeClass('active');
}




$(document).ready(function(){
					   
	// Slider 
	// Add buttons to select slides
	var controls = '';
	var linkText = new Array();
	linkText[0] = 'Get career ideas';
	linkText[1] = 'Where to start';
	linkText[2] = 'Options at the end of year 12';
	linkText[3] = 'Training';
	
	var links = new Array();
	links[0] = 'get-career-ideas.htm';
	links[1] = 'where-to-start.htm';
	links[2] = 'options-at-the-end-of-year-12.htm';
	links[3] = 'training.htm';
	
	$('div.slider ul li').each(function(index) {
		if (index == 0) {
			controls += '<li class="active">';
		} else {
			controls += '<li>';
		}
    	controls += '<a href="' + (links[index]) + '"><span>' +
					(linkText[index]) +
					'</span></a></li>';
  	});
	
	$('#slider div.controls').append('<ul>' + controls + '</ul>');
	
	// Add webtrends querystring bits to slider links
	$('div.slider ul.slides li a').each(function(index) {
		$(this).attr("href", function() {
  			return this.href + '?WT.ac=careers-slider' + parseInt(index+1);
		});
	});
	
	// Start slider
	var playSlideshow =  setInterval( "slideSwitch()", 4000 );

	// Pause on hover over container, resume on mouse out
	$('div.slider').hover(
		function() {
    		clearInterval(playSlideshow);
		}
	,
		function() {
    		playSlideshow =  setInterval( "slideSwitch()", 4000 );
		}
	);
});
