/*
 * jQuery Paginate plugin
 * Version 1.0.0 (04/06/2009)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2009 Michel Meyer
 * 
 */

(function($) {

	$.fn.paginate = function (options){
		
		options = $.extend({
			activeIndex: 0,
			buttonLeft: $('#buttonLeft'),
			buttonRight: $('#buttonRight'),
			currentItem: $('#currentItem'),
			countItem: $('#countItem'),
			onChange: function(from, to, pager){from.hide(); to.show();}
		}, options);
		
		
		this.items = new Array();
		this.nbItems = 0;
		this.onTransition = false;
		
		this.initialize = function()
		{
			var i = 0;
			var paginatedElement = this;
			
			$(this).children('li').each(function()
			{
				var active = i == options.activeIndex ? true : false;			
				paginatedElement.items.push(active);
				i++;
			});
			
			this.nbItems = i;
			this.refreshItems();
		}
		
		this.refreshItems = function(direction)
		{
			
			for(var i = 0 ; i < this.items.length ; i++)
			{
				var active = this.items[i];
				var index  = i;
				
				var item = paginatedElement.children('li')[index];
				switch(direction)
				{
					case 'up':
						if(active)
						{
							options.onChange(paginatedElement.children('li')[index-1], item, paginatedElement);
						}
					break;
					case 'down':
						if(active)
						{
							options.onChange(paginatedElement.children('li')[index+1], item, paginatedElement);
						}
					break;
					default:
						if(active)
						{
							$(item).show();					
						}	
						else 
						{
							$(item).hide();
						}	
				}
				
				
				
				
				if(options.activeIndex >= paginatedElement.nbItems - 1)
				{
					options.buttonRight.css({'visibility' : 'hidden'});
				}
				else
				{
					options.buttonRight.css({'visibility' : 'visible'});
				}
				
				if(options.activeIndex <= 0)
				{
					options.buttonLeft.css({'visibility' : 'hidden'});
				}
				else
				{
					options.buttonLeft.css({'visibility' : 'visible'});
				}
				
				options.currentItem.html(options.activeIndex + 1);
				options.countItem.html(paginatedElement.nbItems);
			}
			
			
		}

		this.moveNext = function()
		{
			if (options.activeIndex < paginatedElement.nbItems) 
			{
				this.items[options.activeIndex] = false;
				options.activeIndex++;
				this.items[options.activeIndex] = true;
			}	
		}
		
		this.movePrevious = function()
		{
			if(options.activeIndex > 0)
			{
				this.items[options.activeIndex] = false;
				options.activeIndex--;
				this.items[options.activeIndex] = true;
			}
		}
		
		this.changeEnded = function()
		{
			this.onTransition = false;	
		}
		
        var paginatedElement = this;

			

		return $(this).each(function(){
			
			paginatedElement.initialize();						
			
			options.buttonLeft.click(function(){
				if(!paginatedElement.onTransition){
					paginatedElement.onTransition = true;
					paginatedElement.movePrevious();
					paginatedElement.refreshItems('down');
				}
				return false;
			});
				
			options.buttonRight.click(function(){
				if (!paginatedElement.onTransition) {
					paginatedElement.onTransition = true;
					paginatedElement.moveNext();
					paginatedElement.refreshItems('up');
				}
				return false;
			});
		});
	}
})(jQuery);