//takes a list and makes an animated paginated list with optional easing, and autoplay.  You can also specify the number of items in each page
(function($){
	var settings;
	var at_page={};
	var num_pages={};
	var total_items={};
	var item_widths={};
	var item_heights={};
	var container_widths={};
	var container_heights={};
	var slideDirections = {};
	var intervals={};
	var in_hover={};
	var buildCompleteCallbacks = {};
	var animationCompleteCallbacks = {};
	var clickCallbacks = {};
	var triggeredAnimationComplateCallback = {};
	$.fn.inlinePager = function(options){
		settings = $.extend({}, $.fn.inlinePager.defaults, options);
		$(this).each(function(){
			$(this).css({display:'block'});
			var self=this;
			if($('.'+settings.dataClass+' > li',this).length!==0 && $('.'+settings.pagerClass,this).length!==0){
				if($('.'+settings.dataClass,this).is('ul')){
					if(settings.buildCompleteCallback && typeof settings.buildCompleteCallback === 'function'){
						buildCompleteCallbacks[$.data(this)] = settings.buildCompleteCallback;
					}
					if(settings.animationCompleteCallback && typeof settings.animationCompleteCallback === 'function'){
						animationCompleteCallbacks[$.data(this)] = settings.animationCompleteCallback;
					}

					total_items[$.data(this)]=$('.'+settings.dataClass+' > li',this).length;
					if(total_items[$.data(this)] && settings.clickCallback && typeof settings.clickCallback === 'function'){
						clickCallbacks[$.data(this)] = settings.clickCallback;
						$('.'+settings.dataClass+' > li').bind('click',function(e){clickCallbacks[$.data(self)](self,e)});
						
					}
					num_pages[$.data(this)]=Math.ceil(total_items[$.data(this)]/settings.numItems);
					slideDirections[$.data(this)] = settings.slideDirection;
					at_page[$.data(this)]=1;
					var item_width=$.fn.inlinePager.getItemWidth(this);
					var item_height=$.fn.inlinePager.getItemHeight(this);
					item_widths[$.data(this)]=item_width;
					item_heights[$.data(this)]=item_height;
					var curr_width=$(this).width();
					var curr_height=$(this).height();
					container_widths[$.data(this)]=curr_width;
					container_heights[$.data(this)]=curr_height;
					$('.'+settings.dataClass,this).wrap('<div class="thumb_slide"></div>');
					$('.thumb_slide',this).css({position:'relative',overflow:'hidden'});
					$('.'+settings.dataClass,this).css({position:'absolute',left:0,top:0});
					if(slideDirections[$.data(this)]==='H'){
						$('.thumb_slide',this).css({width:curr_width,height:item_height});
						var shim=Math.floor((curr_width-(item_width*settings.numItems))/settings.numItems);
						var r=parseInt($('.'+settings.dataClass+'> li',this).css('margin-right').replace(/\D/g,''));
						$('.'+settings.dataClass+' > li',this).css('margin-right',r+shim);
						$('.'+settings.dataClass,this).css({width:total_items[$.data(this)]*(item_width+shim)});
					}
					else{
						$('.thumb_slide',this).css({width:item_width,height:curr_height});
						var shim=Math.floor((curr_height-(item_height*settings.numItems))/settings.numItems);
						var r=parseInt($('.'+settings.dataClass+' > li',this).css('margin-bottom').replace(/\D/g,''));
						$('.'+settings.dataClass+' > li',this).css('margin-bottom',r+shim);
						$('.'+settings.dataClass,this).css({height:total_items[$.data(this)]*(item_height+shim)});
					}
					
				}
				
				$.fn.inlinePager.generatePager(this);
				$.fn.inlinePager.bindPagerEvents(this);
				if(settings.autoPage!==false){
					intervals[$.data(this)]=setInterval(function(){
						$.fn.inlinePager.autoPage(self);
					},settings.autoPage);
					$(this).hover(function(e){$.fn.inlinePager.hoverFunc('OVER',this)},function(e){$.fn.inlinePager.hoverFunc('OUT',this)})
				}
			}
			if(buildCompleteCallbacks[$.data(this)]!==undefined){
				buildCompleteCallbacks[$.data(this)](this);
			}
		});
	};
	$.fn.inlinePager.hoverFunc = function(status,curr){
		//this sets the status of the widget to indicate whether the cursor is in it
		if(status==='OVER'){
			in_hover[$.data(curr)]=true;
		}
		else{
			in_hover[$.data(curr)]=false;
		}
	}
	$.fn.inlinePager.autoPage = function(curr){
		//function to page through the list automatically
		if(in_hover[$.data(curr)]){
			return;
		}
		var at=at_page[$.data(curr)];
		var np=num_pages[$.data(curr)];
		at=((at+1)%(np+1));
		if(at===0){
			at=1;
		}
		$.fn.inlinePager.goPage(at,curr);
	}
	$.fn.inlinePager.goPage = function(at,curr){
		//goes to the correct page, and animates it
		//kill the auto pager if they click
		if(arguments.length===3 && settings.autoPage && settings.autoPageStopOnClick && intervals[$.data(curr)]!==undefined){
			clearInterval(intervals[$.data(curr)]);
			intervals[$.data(curr)]=undefined;
		}
		//unbind the events so that clicks do not happen during the animation
		$('ul.'+settings.pagerClass+' > li span',curr).unbind('click');
		var previous=at_page[$.data(curr)];
		at_page[$.data(curr)]=at;
		if(slideDirections[$.data(curr)]==='H'){ //vertical or horizontal slide ?
			var w=container_widths[$.data(curr)];
			var l=$('.thumb_slide > ul',curr).css('left');
			var go=-((at-1)*w);
			$('.thumb_slide > ul',curr).animate({'left':go},settings.transitionSpeed,settings.easing, function(){$.fn.inlinePager.bindPagerEvents(curr)});
		}
		else{
			var h=container_heights[$.data(curr)];
			var t=$('.thumb_slide > ul',curr).css('top');
			var go=-((at-1)*h);
			$('.thumb_slide > ul',curr).animate({'top':go},settings.transitionSpeed,settings.easing,function(){$.fn.inlinePager.bindPagerEvents(curr)});
		}
	};
	$.fn.inlinePager.bindPagerEvents = function(curr){
		//sets the events on the pager arrows and list to turn on and off based on the list position
		//this also sets and removes the classes disabled, and current
		
		var at=at_page[$.data(curr)];
		var np=num_pages[$.data(curr)];
		$('ul.'+settings.pagerClass+'> li span',curr).unbind('click');
		$('ul.'+settings.pagerClass+'> li',curr).removeClass('disabled');
		$('ul.'+settings.pagerClass+'> li',curr).removeClass('current');
		if(at===1){
			$('ul.'+settings.pagerClass+' > li.prev',curr).addClass('current disabled');
		}
		else{
			$('ul.'+settings.pagerClass+' > li.prev span',curr).bind('click',function(e){$.fn.inlinePager.goPage(at-1,curr,e);});
		}
		if(at===np){
			$('ul.'+settings.pagerClass+' > li.next',curr).addClass('current disabled');
		}
		else{
			$('ul.'+settings.pagerClass+' > li.next span',curr).bind('click',function(e){$.fn.inlinePager.goPage(at+1,curr,e);});
		}
		var i;
		for(i=1;i<=np;i++){
			if(i!==at){
				
				$('ul.'+settings.pagerClass+' li:eq('+i+') span',curr).bind('click',(function(ii){return function(e){$.fn.inlinePager.goPage(ii,curr,e);return false};})(i));
			}
			else{
				$('ul.'+settings.pagerClass+' li:eq('+i+')',curr).addClass('current disabled');
			}
		}
		if(triggeredAnimationComplateCallback[$.data(curr)] && animationCompleteCallbacks[$.data(curr)] !== undefined){
			animationCompleteCallbacks[$.data(curr)](curr);
		}
		else{
			triggeredAnimationComplateCallback[$.data(curr)] = true;
		}
	};
	$.fn.inlinePager.generatePager = function(curr){
		//generates the pager as a UL
		if(num_pages[$.data(curr)]>1){
			var pager='';
			var i;
			for(i=1;i<=num_pages[$.data(curr)];i++){
				pager+='<li><span class="pagination_page_num">'+i+'</span></li>';
			}
			pager='<li class="prev"><span class="prev_span">'+settings.previousText+'</span></li>'+pager+'<li class="next"><span class="next_span">'+settings.nextText+'</span></li>';
			$('ul.'+settings.pagerClass,curr).html(pager);
			$('ul.'+settings.pagerClass,curr).css('display','block')
		}
		
	};
	$.fn.inlinePager.getItemWidth = function(curr){
		//if we are a horizontal pager, we only care about the width of the first because they should all be consistent
		// if it is vertical, get the maximum width
		if(slideDirections[$.data(curr)]==='H'){
			return $('.'+settings.dataClass+' > li:eq(0)',curr).outerWidth();
		}
		else{
			var max=0;
			$('.'+settings.dataClass+' > li',curr).each(function(){
				if($(this).outerWidth()>max){
					max=$(this).outerWidth();
				}
			});
			return max;
		}
	};
	$.fn.inlinePager.getItemHeight = function(curr){
		//SAA but, reversed
		if(slideDirections[$.data(curr)]==='V'){
			return $('.'+settings.dataClass+' > li:eq(0)',curr).outerHeight();
		}
		else{
			var max=0;
			$('.'+settings.dataClass+' > li',curr).each(function(){
				if($(this).outerHeight()>max){
					max=$(this).outerHeight();
				}
			});
			return max;
		}
	};
	$.fn.inlinePager.defaults = {
		dataClass: 'data',  //class of the UL that has the items to page
		numItems: 5,   //this is the number of items to display on each page
		pagerClass: 'pagination', //class of the UL that has  the pager
		slideDirection: 'H', //can be H or V
		previousText:'Previous', //Text to be put in the previous button
		nextText:'Next', //text to be put in the next button
		transitionSpeed:'slow', //speed of the transition, slow, fast, or number in ms
		easing: 'linear', //if you want to use anything else, install jquery.easing
		autoPage: false, //if this is a number then it will turn on the auto page, and will set the speed to that number in ms
		autoPageStopOnClick:true,  //if this is false, it will not stop the auto pager when you click
		buildCompleteCallback: false, //set this to a functiont to run when when the pagination is built
		animationCompleteCallback:false, //set this to a function to run when animation is complete
		clickCallback:false //set this to a function to run instead of the normal click event (i.e. href)
	};
})(jQuery);


//Fix the divider lines into the "Upcoming Releases" box
$(document).ready(function(){						   
			$('#movies_upcoming_rel ul.data li').each(function(intIndex){
				if((intIndex%2)!=0)
					$(this).css('background','none');
			});  
			$('#television_upcoming_rel ul.data li').each(function(intIndex){
				if((intIndex%2)!=0)
					$(this).css('background','none');
			});  
});

