(function($){
	$.fn.itempager = function(){
		//var currentDiv = 'special_products';
		//var currentPage = 0;
		//var itemsPerPage = 3;
		//var numPages;
		//var numItems;
		
		var element = this;
		var defaults = {  
			currentDiv: '',
			itemSelector: ' div .centerBoxWrapper',
			nextButton: '#next_page_button',
			prevButton: '#prev_page_button',
			currentPage: 0,
			itemsPerPage: 3,
			numPages: '',
			numItems: ''
		};  
		var options = $.extend(defaults, options);  

		$(options.nextButton, element).click( function() {
			options.currentPage++;
		
			alert(options.currentPage);
			
			var leftboundItemIndex = options.currentPage * options.itemsPerPage;
			var rightboundItemIndex = (options.currentPage * options.itemsPerPage) + options.itemsPerPage;
			
			$('#'+options.currentDiv+options.itemSelector).children().each( function(index) {
				if(index < leftboundItemIndex || index > rightboundItemIndex)
					$(this).css('display','none');
				else
					$(this).css('display','');
			});
			
			enablePrev();
			if(options.currentPage >= options.numPages)
				disableNext();											  
		});
		
		$(options.prevButton, element).click( function() {
			options.currentPage--;
		
			var leftboundItemIndex = options.currentPage * options.itemsPerPage;
			var rightboundItemIndex = (options.currentPage * options.itemsPerPage) + options.itemsPerPage;
			
			$('#'+options.currentDiv+options.itemSelector).children().each( function(index) {
				if(index < leftboundItemIndex || index > rightboundItemIndex)
					$(this).css('display','none');
				else
					$(this).css('display','');
			});
			
			enableNext();
			if(options.currentPage <= 0)
				disablePrev();			
		});
		
		$(element).load(function(){
			switchDiv(element.id);					
			
			function switchDiv(div_id) {			
				options.currentDiv = div_id;
				options.currentPage = 0;
				
				$('#'+options.currentDiv+options.itemSelector+' br').remove();
				$('#'+options.currentDiv+options.itemSelector+' h2').remove();				
			
				numItems = $('#'+options.currentDiv+options.itemSelector).children().length;
				
				options.numPages = Math.ceil(options.numItems / options.itemsPerPage) - 1;
				
				disablePrev();
				disableNext();
				
				if(options.numItems > options.itemsPerPage)
					enableNext();
					
				var leftboundItemIndex = options.currentPage * options.itemsPerPage;
				var rightboundItemIndex = (options.currentPage * options.itemsPerPage) + options.itemsPerPage;					
				$('#'+options.currentDiv+options.itemSelector).children().each( function(index) {
					if(index < leftboundItemIndex || index > rightboundItemIndex)
						$(this).css('display','none');
					else
						$(this).css('display','');
				});													
			};

			function enablePrev() {
				$(options.nextButton).css('display','');
			};		
			function disablePrev() {
				$(options.prevButton).css('display','none');
			};
			function enableNext() {
				$(options.nextButton).css('display','');
			};		
			function disableNext() {
				$(options.prevButton).css('display','none');
			};
		});
	};
})(jQuery);
