(function($) {	
	$.fn.pagination = function(settings) {
		defaults = {
				page:1,
				circular: true,
				auto: false,
				speed: 5000
				};
		settings = jQuery.extend(defaults, settings);
        
		return this.each(function(){
			var _this = $(this);
			var top = $(this).parent().parent();
			var more = $('.more', top);
			var less = $('.less', top); 
			var numTotalPag = Math.ceil(_this.children('li').length / settings.itemView);
			var currentPage = 1;
			
    		_this.children('li').hide().slice(0, settings.itemView).show();
    		
    		if(settings.auto){
    			setInterval(function() {
    				paginate('next');
                }, settings.speed);

			}	
			more.click(function(){
				paginate('next');
			});
			less.click(function(){
				paginate('prev');
			})
			
			function paginate(direction){
				if (direction == 'next'){
					if (currentPage <= numTotalPag) {
						if(settings.circular){
							if ( currentPage == numTotalPag ){
								currentPage = 1;
							}else{
								currentPage++;
							}
						}else  {
							currentPage++;
							if ( currentPage == numTotalPag ){
								more.hide()
							}
						}
					}
					less.show();
					
				}else if (direction == 'prev'){
					if (currentPage >= 1) {
						if(settings.circular){
							if ( currentPage == 1 ){
								currentPage = numTotalPag;
							}else{
								currentPage--;
							}
						} else  {
							if ( currentPage == 1 ){
								less.hide();
							}else{
								currentPage--;
								if(currentPage == 1 ){
									less.hide();
								}
							}
						}
					}
					more.show();	
				}

				var inicio = (currentPage-1) * settings.itemView;
				var fin = inicio + settings.itemView;
				_this.children('li').hide().slice(inicio, fin).show();
			}
		});
	};
})(jQuery);

$(function() {	
	//pagination mesa
	$("#first div ul").pagination({
		itemView:2
	});
	$("#second div ul").pagination({
		itemView:2
	});
	$("#last div ul").pagination({
		itemView:2
	});
	
	//pagination blog
	$("#last-posts ul:first").pagination({
		itemView:4
	});
	$("#last-comments ul:first").pagination({
		itemView:1
	});
	$('#charts1 .box ul').pagination({
		itemView:1,
		auto: true,
		speed: 8000
	});
	$('#charts2 .box ul').pagination({
		itemView:1,
		auto: true,
		speed: 12000
	});
	$('#last-promo .box ul').pagination({
		itemView:1,
		auto: true,
		speed: 7000 
	});
	$('#publicDecisions .box > ul').pagination({
		itemView:1,
		auto: true,
		speed: 12000
	});
	
	
});
