/* 
Slider Functions
*/

function slideSwitch() {
    var $active = $('div.slider ul.slides li.active');
	var $activecontrol = $('#featured div.controls ul li.active');

    if ( $active.length == 0 ) $active = $('div.slider ul.slides li:last');
	if ( $activecontrol.length == 0 ) $activecontrol = $('#featured 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()
        : $('#featured 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 = $('#featured div.controls ul li:nth-child(' + (index+1) +')');
	var $activecontrol = $('#featured div.controls ul li.active');
	
    if ($active.length == 0 ) $active = $('div.slider ul.slides li:last')
	if ($activecontrol.length == 0) $activecontrol = $('#featured 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(){
					   
	// Start News Ticker
	var $newsticker = $('#js-news').ticker({
			speed: 0.06,
			pauseOnItems: 3000,
			htmlFeed: true,
			fadeInSpeed: 1000,
			fadeOutSpeed: 300,
			titleText: '<a href="newsroom.htm">Newsroom:</a>'
	});
	
	
	// Slider 
	// Add buttons to select slides
	var controls = '';
	
	$('div.slider ul li').each(function(index) {
		if (index == 0) {
			controls += '<li class="active">';
		} else {
			controls += '<li>';
		}
    	controls += '<a href="javascript:void(slideSelect(' + index + '))">' +
					(index+1) +
					'</a></li>';
  	});
	
	$('#featured 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=Home-Slider-' + parseInt(index+1);
		});
	});
	
	// Start slider
	var playSlideshow =  setInterval( "slideSwitch()", 5000 );

	// Pause on hover over container, resume on mouse out
	$('div.slider').hover(
		function() {
    		clearInterval(playSlideshow);
		}
	,
		function() {
    		playSlideshow =  setInterval( "slideSwitch()", 5000 );
		}
	);
	
	// Most popular numbered unordered list
	//$("#mostpopular ol li").each(function (i) {
	//	i = i+1;
	//	$(this).prepend('<span class="itemnumber">'+i+'</span>');
	//});
	
});
