/*
*
*	Stagelight - custom jQuery rotator
*	7th Hat Media Group
*	Version: 	1.0
*	Created: 	2010-05-02 
*
*/
$(document).ready(
	function(){
		var currentIndex = 0;
		var slides = new Array();
		var btns = new Array();
		
		$('.title').children('div').each(
			function(){
				slides.push($(this));
				$(this).css("background","#000000");
				//$(this).css("display","none");
				$(this).css("left","900px");
				/*if($.support.opacity)
					$(this).css("display","none");
				else
					$(this).css("filter","alpha(opacity = 0)");*/
			}
		);
		
		$s = slides[0];
		//$s.customFadeIn(500);
		$s.animate({left:0},500);
		
		$('.links').children('a').each(
			function(){
				$(this).click(
					function(){
						var ind = $(this).attr("num");
						swapBanner( Math.round(ind) );
					}
				);
				btns.push($(this));
			}
		);
		
		$b = btns[0];
		$b.addClass("active");
		
		var timer = setInterval(function(){ swapBanner('false'); }, 6000);
		
		/* functions */
		
		function swapBanner(newIndex){
			$s = slides[currentIndex];
			$s.animate({left:-900},500,function(){ $(this).css("left","900px"); });
			/*$s.customFadeOut(500, 
				function(){ 
					//$s.css("display","none");
					$('.title').children('div').css("display","none");
				}
			);*/
			$b = btns[currentIndex];
			$b.removeClass("active");
			if(newIndex=='false'){
				currentIndex = (currentIndex + 1) < slides.length ? (currentIndex + 1) : 0;
			}else{
				currentIndex = newIndex;
				clearInterval(timer);
				timer = setInterval(function(){ swapBanner('false'); }, 6000);
			}
			showBanner();
		}
		
		function showBanner(){
			$ns = slides[currentIndex];
			//$ns.customFadeIn(1000, setActiveButton);
			$ns.animate({left:0}, 250, setActiveButton);
		}
		
		function setActiveButton(){
			var i = 0;
			$('.links').children('a').each(
				function(){
					if(i == currentIndex){
						$(this).addClass("active");
					}
					i++;
				}
			);
		}
		
		/*function resetButtons(n){
			var i = 0;
			$('.links').children('a').each(
				function(){
					if(i == n){
						$(this).addClass("active");
					}else{
						$(this).removeClass("active");
					}
					i++;
				}
			);
		}*/
	}
);

// Fix for IE not fading?
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);
