/*
	Banner jQuery
	Controls banner functionality and fading effect.
	
*/
	

$(function(){
	
	//Initialize banner
	var timer = 0;
	var delay = 7000;
	bannerInterval();

	//Set interval of fade effect
	function bannerInterval(){
		clearTimeout(timer);
		timer = setTimeout(function(){bannerRotate()}, delay);
		delay = 7000;
   }
	
	//Click function for banner thumbnails
	$('#banner div.banner_thumb_nav img.banner_thumb').click(function(){
		
		var index = $(this).index();
		$('#banner ul li').removeClass("selected");
		$('#banner div.banner_thumb_nav img.banner_thumb').removeClass("selected");
		$(this).addClass("selected");
		$('#banner ul li').eq(index).addClass("selected");
		
		//Stalls rotation for 10 seconds
		delay = 10000;
		bannerInterval();
		
	});
	
	//Click function for right arrow (next)
	$('#banner .banner_thumb_right').click(function(){
		var nextLI = $('#banner ul li.selected').next();
		$('#banner ul li').removeClass("selected");
		$('#banner div.banner_thumb_nav img.banner_thumb').removeClass("selected");
		
		if(nextLI.length == 0) 
		{
			$('#banner ul li:first-child').addClass("selected");
			$('#banner div.banner_thumb_nav img.banner_thumb').eq(0).addClass("selected");
		}
		else 
		{
			nextLI.addClass("selected");
			$('#banner div.banner_thumb_nav img.banner_thumb').eq(nextLI.index()).addClass("selected");
		}

		delay = 10000;
		bannerInterval();
	
	});
	
	//Click function for left arrow (prev)
	$('#banner .banner_thumb_left').click(function(){
		var prevLI = $('#banner ul li.selected').prev();
		$('#banner ul li').removeClass("selected");
		$('#banner div.banner_thumb_nav img.banner_thumb').removeClass("selected");
		
		if(prevLI.length == 0)
		{
			$('#banner ul li:last').addClass("selected");
			$('#banner div.banner_thumb_nav img.banner_thumb').eq($('#banner ul li:last').index()).addClass("selected");

		}
		else 
		{
			prevLI.addClass("selected");
			$('#banner div.banner_thumb_nav img.banner_thumb').eq(prevLI.index()).addClass("selected");

		}

		delay = 10000;
		bannerInterval();
	});


	//Banner rotate function, fades current banner into next banner
	function bannerRotate()
	{
		var $active = $('div#banner ul li.selected');	
		if( $active.length == 0) $active = $('div#banner ul li:last');
		
		if( $active.next().length != 0) var $next = $active.next();
		else var $next = $('div#banner ul li:first');
			

		$next.children('a').children('img.banner_img').css({opacity: 0.0});
		$next.children('div.banner_description').css({opacity: 0.0});
		$active.addClass('last-selected');
		$active.removeClass('selected');
		$next.addClass('selected');
		$next.children('a').children('img.banner_img').animate({opacity: 1.0}, 1000);
		$next.children('div.banner_description').animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('last-selected');
    	});
		$('#banner div.banner_thumb_nav img.banner_thumb').removeClass("selected");
		$('#banner div.banner_thumb_nav img.banner_thumb').eq($next.index()).addClass("selected");
		//Call banner to rotate
		bannerInterval();
	}

});

