/*
 * jQuery.inlinePager. pagination plugin
 *
 * Copyright (c) 2010 NBCUDPS
 *
 * Maintained by Cristiana Yambo
 * cristiana.yambo@nbcuni.com
 *
 * Launch  : January 2010
 * Version : 1.1
 * Released: May 24, 2010
 
 1.1: added className on numbered pagination LI's
 */
 //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 items_per_page={};
	var total_items={};
	var item_widths={};
	var item_heights={};
	var container_widths={};
	var container_heights={};
	var slideDirections = {};
	var pageCounterInfo = {};
	var intervals={};
	var in_hover={};
	var buildCompleteCallbacks = {};
	var animationCompleteCallbacks = {};
	var clickCallbacks = {};
	var triggeredAnimationComplateCallback = {};
	var ID=0;
	$.fn.inlinePager = function(options){
		settings = $.extend({}, $.fn.inlinePager.defaults, options);
		$(this).each(function(){
			var curr_id=$(this).data('uniq',ID++);
			if(typeof curr_id === 'object'){
				curr_id=$(this).data('uniq');
			}
			$(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.pageCounterClass,this).length){
						pageCounterInfo[$(this).data('uniq')]={'class':settings.pageCounterClass,'type':settings.pageCounterType,'template':settings.pageCounterTemplate};
					}
					
					if(settings.buildCompleteCallback && typeof settings.buildCompleteCallback === 'function'){
						buildCompleteCallbacks[$(this).data('uniq')] = settings.buildCompleteCallback;
					}
					if(settings.animationCompleteCallback && typeof settings.animationCompleteCallback === 'function'){
						animationCompleteCallbacks[$(this).data('uniq')] = settings.animationCompleteCallback;
					}

					total_items[$(this).data('uniq')]=$('.'+settings.dataClass+' > li',this).length;
					if(total_items[$(this).data('uniq')] && settings.clickCallback && typeof settings.clickCallback === 'function'){
						clickCallbacks[$(this).data('uniq')] = settings.clickCallback;
						$('.'+settings.dataClass+' > li').bind('click',function(e){clickCallbacks[$(self).data('uniq')](self,e)});
						
					}
					items_per_page[$(this).data('uniq')]=settings.numItems;
					num_pages[$(this).data('uniq')]=Math.ceil(total_items[$(this).data('uniq')]/settings.numItems);
					slideDirections[$(this).data('uniq')] = settings.slideDirection;
					at_page[$(this).data('uniq')]=1;
					var item_width=$.fn.inlinePager.getItemWidth(this);
					var item_height=$.fn.inlinePager.getItemHeight(this);
					item_widths[$(this).data('uniq')]=item_width;
					item_heights[$(this).data('uniq')]=item_height;
					var curr_width=$(this).width();
					var curr_height=$(this).height();
					container_widths[$(this).data('uniq')]=curr_width;
					container_heights[$(this).data('uniq')]=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[$(this).data('uniq')]==='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[$(this).data('uniq')]*(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[$(this).data('uniq')]*(item_height+shim)});
					}
					$('.'+settings.dataClass+' > li',this).css('display','block');
					
				}
				$.fn.inlinePager._setPage(this);
				$.fn.inlinePager.generatePager(this);
				$.fn.inlinePager.bindPagerEvents(this);
				if(settings.autoPage!==false){
					intervals[$(this).data('uniq')]=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[$(this).data('uniq')]!==undefined){
				buildCompleteCallbacks[$(this).data('uniq')](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[$(curr).data('uniq')]=true;
		}
		else{
			in_hover[$(curr).data('uniq')]=false;
		}
	}
	$.fn.inlinePager.autoPage = function(curr){
		//function to page through the list automatically
		if(in_hover[$(curr).data('uniq')]){
			return;
		}
		var at=at_page[$(curr).data('uniq')];
		var np=num_pages[$(curr).data('uniq')];
		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[$(curr).data('uniq')]!==undefined){
			clearInterval(intervals[$(curr).data('uniq')]);
			intervals[$(curr).data('uniq')]=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[$(curr).data('uniq')];
		at_page[$(curr).data('uniq')]=at;
		if($())
		if(slideDirections[$(curr).data('uniq')]==='H'){ //vertical or horizontal slide ?
			var w=container_widths[$(curr).data('uniq')];
			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._setPage(curr); $.fn.inlinePager.bindPagerEvents(curr);});
		}
		else{
			var h=container_heights[$(curr).data('uniq')];
			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._setPage(curr); $.fn.inlinePager.bindPagerEvents(curr);});
		}
	};
	$.fn.inlinePager._setPage = function(curr){
		if(pageCounterInfo[$(curr).data('uniq')]!==undefined){
			var p=pageCounterInfo[$(curr).data('uniq')]
			if(p.type=='PAGE'){
				$('.'+p['class'],curr).html($.fn.inlinePager._templateReplace(p.template,{END:num_pages[$(curr).data('uniq')], START:at_page[$(curr).data('uniq')], TOTAL:num_pages[$(curr).data('uniq')]}));
			}
			else{
				var total=total_items[$(curr).data('uniq')];
				var at_item=(items_per_page[$(curr).data('uniq')]*(at_page[$(curr).data('uniq')]-1))+1;
				var of=at_item+items_per_page[$(curr).data('uniq')]-1;
				if(of>total){
					of=total;
				}
				$('.'+p['class'],curr).html($.fn.inlinePager._templateReplace(p.template,{END:of, START:at_item, TOTAL:total}));
			}
		}
	}
	$.fn.inlinePager._templateReplace = function(template,replacements){
		if(template){
			for(var r in replacements){
				var reg= new RegExp('\\[\\$\\$'+r+'\\]','g');
				template=template.replace(reg,replacements[r])
			}
		}
		return template;
	}
	
	$.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[$(curr).data('uniq')];
		var np=num_pages[$(curr).data('uniq')];
		$('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:nth-child('+(i+1)+') span',curr).bind('click',(function(ii){return function(e){$.fn.inlinePager.goPage(ii,curr,e);return false};})(i));
			}
			else{
				$('ul.'+settings.pagerClass+' li:nth-child('+(i+1)+')',curr).addClass('current disabled');
			}
		}
		if(triggeredAnimationComplateCallback[$(curr).data('uniq')] && animationCompleteCallbacks[$(curr).data('uniq')] !== undefined){
			animationCompleteCallbacks[$(curr).data('uniq')](curr);
		}
		else{
			triggeredAnimationComplateCallback[$(curr).data('uniq')] = true;
		}
	};
	$.fn.inlinePager.generatePager = function(curr){
		//generates the pager as a UL
		if(num_pages[$(curr).data('uniq')]>1){
			var pager='';
			var i;
			for(i=1;i<=num_pages[$(curr).data('uniq')];i++){
				pager+='<li class="pager_num_'+i+'"><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[$(curr).data('uniq')]==='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[$(curr).data('uniq')]==='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 block that is to display the counter
		pageCounterClass: 'counter', //displays either page 1 of X or showing 1-10 of 24
		pageCounterType: 'ITEMS', //can be either PAGE or ITEMS (corresponds to above)
		pageCounterTemplate: 'Displaying [$$START]-[$$END] of [$$TOTAL]',  //replacement variables are START, END, TOTAL
		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: null, //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);// JavaScript Document
