$(function(){
	$('ul.auto-paginate').each(function(){
		var $this = $(this);
		var $children = $this.children();
		var size = $this.attr('data-size') || 3;
		var anim = $this.attr('data-anim') || 300;
		var totalCount = $children.length || 1;
		
		var $pager = $('ul.pager[rel='+$this.attr('id')+']');
		var pagesCount = Math.ceil(totalCount/size);
		if(pagesCount > 1){
			for (var i = 0; i < pagesCount; i++) {
				$pager.append($('<li />', {
					html: i+1 
					}));
			}
			$pager.children().bind('click', function(){
				showPage($(this).index());
			});
		}
		var currentIndex;
		var showPage = function(index){
			if(currentIndex == index)
				return;
				
			$pager.children().removeClass('selected');
			$pager.children(':eq('+index+')').addClass('selected');
			
			currentIndex = index;
			$children.stop().hide(anim);
			if(index == 0){
				$children.filter(':lt(' + size + ')').show(anim);
			}
			else
			{
				$children.filter(':gt(' + ((index * size) - 1) + '):lt(' + size + ')').show(anim);
			}
		}
		$.fx.off = true;
		showPage(0);
		$.fx.off = false;
	});
});
