$(document).ready(function() {
						   
	// init 					   
	var $active;	
	var $active_index;
	var $next;
	var $next_index;
	
	
	function fade(){	
		$active_index 	= $active_index+1;
		$next_index 	= $next_index+1;
		//console.log("active:" + $active_index + " next:" + $next_index);		
		$('#header-banner-slides li:nth-child('+$active_index+')').addClass('last-active');
		
		$('#header-banner-page-links li').removeClass('active');
		$('#header-banner-page-links li:nth-child('+$next_index+')').addClass('active');
		
		$('#header-banner-slides li:nth-child('+$next_index+')').css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$('#header-banner-slides li:nth-child('+$active_index+')').removeClass('active last-active');
			});	
		
	}

	fadeSwitch = function(){		
							play = setInterval(function(){ 
														//set timer - this will repeat itself every 3 seconds
														$active = $('#header-banner-slides li.active');																	
														if ( $active.length == 0 ) $active = $('#header-banner-slides li:last');
																$active_index = $('#header-banner-slides li').index($active);																
																$next =  $active.next().length ? $active.next()	: $('#header-banner-slides li:first');
																$next_index = $('#header-banner-slides li').index($next);
																fade(); //Trigger the paging and slider function
														}, 5000); //Timer speed in milliseconds (7 seconds)
							};	
	
	fadeSwitch();
	
	//page links
	$("#header-banner-page-links a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		fadeSwitch(); //Resume rotation
	}).click(function() {	
		$active = $('#header-banner-slides li.active');	
		$active_index = $('#header-banner-slides li').index($active);
		$next_index = $(this).parent().prevAll().length;
		clearInterval(play); // stop fade rotation
		if($active_index  != $next_index) fade(); // check current & trigger fade if necessary
		return false; //prevent browser jump to link anchor
	});	
	
	//banners				
	$("#header-banner-slides a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		fadeSwitch(); //Resume rotation
	});		

	
});	
