var carouselAutoPlay, carouselContent, current, newIndex, touts;

function carouselPlayNext(){	
	var index = (current) ? $(touts).index(current) + 1 : 0;
	index = (index == $(touts).size()) ? 0 : index;
	current = $(touts).eq(index).activateCarousel()[0];
	activateTout(current);	
};
function activateTout(ele){
	$('div.carousel-tout').removeClass('active');
	//$('.carousel-tout img').fadeTo('fast',  0.5);
	$(ele).addClass('active');	
};
function posPage(){
	var w = $(window).width();
	if(w < 1200 && w > 980)	$("body").css("margin-left",(w - 1200)/2 + "px");
	else if (w < 980) $("body").css("margin-left","-110px");
	else $("body").css("margin-left","0px");
}
$(window).resize(function(){ posPage(); });

$(document).ready(function() {

	// hook up SFI for interactive clicks
	$(".carousel-content-container a").click( function() { 
		// take name, trigger
		mb.metrics.trackInteraction( {link:$(this).text(),linkSource:"SFI"} );		
	});

	posPage();

	//Slider Links
	$('#thumb-img-tout-cont').hoverIntent(
		function(){
			$('.hd-tout-txt',$(this)).addClass('hov');
			//$('.tout-link').animate({ top:'-=45' }, "fast");
			$('.tout-tn-1',$(this)).fadeIn(100);
			$('.tout-tn-2',$(this)).delay(50).fadeIn(100);
			$('.tout-tn-3',$(this)).delay(100).fadeIn(100);
		},
		function(){
			$('.hd-tout-txt',$(this)).removeClass('hov');	
			//$('.tout-link').animate({ top:'+=45' }, "fast");
			$('.tout-thumbnail',$(this)).fadeOut(100);
		}
	);
	
    $('.carousel').each(function(){
        $('<div id="blackout"/>').appendTo(this).hide().css('opacity', 0);
        carouselContent = $('.carousel-content', this);
		touts = $('.carousel-tout', this); 
		
	    $(touts).hover(function(){
	
			 $('.tout-img-lnk img', this).addClass('tmpImgActive');
			 $('a.hd-tout-txt', this).addClass('tmpTxtActive');
			 
	    	$(touts).click(function() {
	    		$(touts).stop(true,true);
	    		activateTout(this);	    	
	    		$(this).activateCarousel();	
	    		return false;
	    	});
	    	$(touts).stop(true,true);
	    	clearInterval(carouselAutoPlay);
	    	
	    	if ($.browser.msie && $.browser.version <= 6 ) {
	    		$('a.hd-tout-txt').css('visibility','hidden');
	    		$('a.tmpTxtActive', this).css('visibility','visible');
	    	};	 
	    },
	    function(){
	        carouselAutoPlay = setInterval("carouselPlayNext()", 98000);
	        $('.tout-img-lnk img').removeClass('tmpImgActive');
	        $('a.hd-tout-txt').removeClass('tmpTxtActive');
	        $('a.tmpTxtInActive').removeClass('tmpTxtInActive');
	        $(touts).stop(true,true);
	        
	        if ($.browser.msie && $.browser.version <= 6 ) {
	        	$('a.hd-tout-txt').css('visibility','visible');
	    	};
	        	 
	    });
    });
    
    current = $(touts).eq(0).addClass('active')[0];   
    $(carouselContent).eq(0).show();
    carouselAutoPlay = setInterval("carouselPlayNext()", 98000);
	
	
	
	
	$(function(){
		$('.link-sets').each(function(){
			var $carousel = this;
			var $ul = $('ul:first', $carousel);
			var $items = $('li', $ul);
			var $prevButton, $nextButton, iCurrentIndex, iColumns, iColumnWidth;
			// events
			$prevButton = $("a.carousel-move-left", $carousel).click(movePrev);
			$nextButton = $("a.carousel-move-right", $carousel).click(moveNext);
			$('a',$items).each(function(){
				$(this).mousedown(function(){
					$(this).addClass('down');
				}).mouseup(function(){
					$(this).removeClass('down');
				});
			});
			// logic
			$prevButton.addClass('button-disabled');
			iColumns = $items.length;
			if(iColumns <= 5) {
				// disable the buttons and return, since there 
				// aren't enough columns for a carousel
				$prevButton.addClass('button-disabled');
				$nextButton.addClass('button-disabled');
				return; 
			}
			iColumnWidth = 920; // $($items[0]).outerWidth()
			var iCurrentIndex = 0;
			var iMinIndex = 0;
			var iMaxIndex = iColumns - 5; // There are 5 visible columns
			
			// internal functions
			function moveNext() {
				if((iCurrentIndex + 1) <= iMaxIndex) {
					iCurrentIndex++;
					updateView();
					updateButtons(iCurrentIndex);
				}
			}		
			function movePrev() {
				if((iCurrentIndex - 1) >= iMinIndex) {
					iCurrentIndex--;
					updateView();
					updateButtons(iCurrentIndex);
				}
			}
			function getCurrentItem() {
				return $('li.focus', $ul);
			}		
			function moveToNextItem($current) {
				if($($current).next().is('li')) {
					$($current).removeClass('focus')
						.attr('tabindex', '-1')
							.find('a').blur()
						.end().next()
						.attr('tabindex', '0')
						.addClass('focus')
						.find('a').focus();
				}
			}
			function moveToPrevItem($current) {
				if($($current).prev().is('li')) {
					$($current).removeClass('focus')
						.attr('tabindex', '-1')
						.prev()
						.attr('tabindex', '0')
						.addClass('focus')
						.find('a').focus();
					}
			}
			function updateView() {
				$($ul).stop();
				$($ul).animate({ 
						left: (iColumnWidth * -iCurrentIndex) + "px"
					}, 500);	
			}	
			function updateButtons(iCurrentIndex){
				if(iCurrentIndex == iMinIndex) {
					$prevButton.addClass('button-disabled');
					$nextButton.removeClass('button-disabled');
					return;
				}
				if((iCurrentIndex) == iMaxIndex) {
					$nextButton.addClass('button-disabled');
					$prevButton.removeClass('button-disabled');
					return;
				}
				$prevButton.removeClass('button-disabled');
				$nextButton.removeClass('button-disabled');
				return;
			}
		});
	});
	
	$('div.link-sets ul li').hoverIntent(function(){
		var _this = $(this);
		$('span.carousel-wrapper', _this).addClass('open');
		$('.greyout', _this).show().animate({height:'172px'}, 150, function(){
		    $('h5',_this).css('bottom', '147px');
		    $('p',_this).css('bottom','-10px');
		    $('a',_this).css('bottom', '70px');
		    $('.lower-links').css("z-index","1001");
		    $(this).fadeOut(250);
		});

	},function(){
		var _this = $(this);
		$('span.carousel-wrapper', _this).removeClass('open');
		$('.greyout', _this).show().animate({height:'112px'}, 150, function(){
		    $('h5',_this).css('bottom', '0px');
		    $('p',_this).css('bottom','-100px');
		    $('a',_this).css('bottom', '25px');
		    $('.lower-links').css("z-index","999");
		    $(this).fadeOut(250);
		});
	}).find('span.carousel-wrapper').append('<div class="greyout"/>');	
	
	
});	




