var onSlide = 1;
var lastSlide = 0;
var isPaused = true;
var inTransition = false;
var interval;
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	def: 'easeInOutCubic',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});
$(document).ready(function() {
	$("#ContentRotator-Title1").show();
	$("#ContentRotator-Image1").show().css("left",0);
	$("#ContentRotator-Details1").show();
	$("#ContentRotator-WatchNow1").show();
	$("#ContentRotator-Pagination1").addClass("Selected");
	$("#Switch a").toggle(
		function(){
			$("#ContentRotator-Slide-ShowsPanel").animate({left:"-879px"},1000,function(){
				$(".AllShows").fadeIn();
				$("#Switch a").html("Close");
			});
			stopRotator();
			return false;
		},
		function(){
				$("#ContentRotator-Slide-ShowsPanel").animate({left:"0px"},500,function(){
				$(".AllShows").hide();
				$("#Switch a").html("All Shows");
			});
			startRotator();
			return false;
		}
	);
	// Stop rotator on user interaction
	$("#ContentRotator-Slide-Pagination").hover(function(){
		stopRotator();
	},function(){
		startRotator();
	});
	$(".ContentRotator-Slide1-WatchNow").hover(function(){
		stopRotator();
	},function(){
		startRotator();
	});
	$(".ContentRotator-Slide1-Episode").hover(function(){
		stopRotator();
	},function(){
		startRotator();
	});
	// Content Rotator Pagination
	$(".ContentRotator-Pagination").click(function(){
		if (onSlide != $(this).find("a").html()) goToRotator($(this).find("a").html());
		return false;
	});
	// remove the focus lines
	$('a').focus( function() { $(this).blur(); } );
	// Start the rotator
	$(window).load(function() {
		startRotator();
	});
});
function startRotator() {
	if (isPaused) {
		isPaused=false;
		window.clearInterval(interval);
		interval = window.setInterval('goRightRotator();',5000);
	}
}
function stopRotator() {
	isPaused=true;
	window.clearInterval(interval);
}
function goRightRotator(newSlide) {
	if (!inTransition) {
		inTransition = true;
		lastSlide = onSlide;
		if (newSlide) {
			onSlide = newSlide;
		}else{
			if (onSlide < maxSlide && onSlide >= 1) {
				onSlide++;
			}else{
				onSlide = 1;
			}
		}
		$("#ContentRotator-Details"+(lastSlide)).fadeOut(500);
		$("#ContentRotator-WatchNow"+(lastSlide)).fadeOut(500);
		$("#ContentRotator-Slide1-PanelTop").animate({top:"-125px"},500,function(){
			$("#ContentRotator-Title"+(lastSlide)).hide();
			$("#ContentRotator-Title"+(onSlide)).show();
		});
		$("#ContentRotator-Slide1-PanelTop").delay(1000).animate({top:"0px"},500);
		$("#ContentRotator-Image"+(lastSlide)).delay(500).animate({left:"-921px"},1000,function(){$(this).hide();});
		$("#ContentRotator-Image"+(onSlide)).delay(500).css("left","921px").show().animate({left:"0px"},1000);
		$("#ContentRotator-Details"+(onSlide)).delay(1500).fadeIn(500);
		$("#ContentRotator-WatchNow"+(onSlide)).delay(1500).fadeIn(500,function(){inTransition = false;});
		$(".ContentRotator-Pagination").removeClass("Selected");
		$("#ContentRotator-Pagination"+(onSlide)).addClass("Selected");
	}
}
function goLeftRotator(newSlide) {
	if (!inTransition) {
		inTransition = true;
		lastSlide = onSlide;
		if (newSlide) {
			onSlide = newSlide;
		}else{
			if (onSlide <= maxSlide && onSlide > 1) {
				onSlide--;
			}else{
				onSlide = maxSlide;
			}
		}
		$("#ContentRotator-Details"+(lastSlide)).fadeOut(500);
		$("#ContentRotator-WatchNow"+(lastSlide)).fadeOut(500);
		$("#ContentRotator-Slide1-PanelTop").animate({top:"-125px"},500,function(){
			$("#ContentRotator-Title"+(lastSlide)).hide();
			$("#ContentRotator-Title"+(onSlide)).show();
		});
		$("#ContentRotator-Slide1-PanelTop").delay(1000).animate({top:"0px"},500);
		$("#ContentRotator-Image"+(lastSlide)).delay(500).animate({left:"921px"},1000,function(){$(this).hide();});
		$("#ContentRotator-Image"+(onSlide)).delay(500).css("left","-921px").show().animate({left:"0px"},1000);
		$("#ContentRotator-Details"+(onSlide)).delay(1500).fadeIn(500);
		$("#ContentRotator-WatchNow"+(onSlide)).delay(1500).fadeIn(500,function(){inTransition = false;});
		$(".ContentRotator-Pagination").removeClass("Selected");
		$("#ContentRotator-Pagination"+(onSlide)).addClass("Selected");
	}
}
function goToRotator(newSlide) {
	if (onSlide > newSlide) {
		goLeftRotator(newSlide);
	}else{
		goRightRotator(newSlide);
	}
}
/*************************ALL SHOWS PANEL***********************************/
$(document).ready(function(){ 
   var currentPosition = 0;
   var slideWidth = 875;
   var slides = $('.AllShows');
   var numberOfSlides = slides.length;
   var currentPage = '';
  slides.wrapAll('<div id="ContentRotator-ShowThumbList"></div>').css({'float':'left', 'width': slideWidth});
  $('#ContentRotator-ShowThumbList').css('width', slideWidth * numberOfSlides);
  	selectDirection(currentPosition);
  $('.control').bind('click', function(){
	currentPosition = ($(this).attr('id')=='ContentRotator-Pagination-Next') ? currentPosition+1 : currentPosition-1;												   	selectDirection(currentPosition);
    $('#ContentRotator-Pagination-CurrentPage').html(currentPosition+1);
	$('#ContentRotator-ShowThumbList').animate({'marginLeft' : slideWidth*(-currentPosition)
																		   
    });
  });
  function selectDirection(position){
	if(position==0){ $('#ContentRotator-Pagination-Previous').hide(),
					 $(".ContentRotator-Pagination-Page").css("marginLeft", "15px")
	} else{ $('#ContentRotator-Pagination-Previous').show(),
			$(".ContentRotator-Pagination-Page").css("marginLeft", "0px")
		}
		if(position==numberOfSlides-1){
			$("#ContentRotator-Pagination-Next").hide();
		}else{
			$("#ContentRotator-Pagination-Next").show();
		}
	}
});

