function Pagination(options)
{
	this.target = $(options.target);
	this.prev = this.target.find('.pag-prev');
	this.next = this.target.find('.pag-next');
	this.counter = this.target.find('.pag-count');
	this.selector = this.target.find('.pag-select-page');	
	this.disable = (options.disable == null || options.disable == true) ? true : false;
	
	this.total = options.total || 1;
	this.showing = options.show || 10;	
	this.selectedIndex = 1;	
	this.pages = 0;	
	this.opts = options;
	
	this.init();
	
}

Pagination.prototype.init = function()
{
		var _self = this;
		
		// Left control
		this.next.bind('click', function()
		{
			if(_self.selectedIndex < _self.getPages() && _self.disable)
			{
				_self.selectedIndex ++;
				_self.manageControls();
				_self.updateCounter();
				_self.setSelector();
							
				// dispatch event
				_self._dispatchEvent();	
			}	
			//alert("outside")		
			return false;	
		})
		
		// Right control
		this.prev.bind('click', function()
		{
			if(_self.selectedIndex > 1 && _self.disable)
			{
				_self.selectedIndex --;				
				_self.manageControls();
				_self.updateCounter();
				_self.setSelector()	;
				
				// dispatch event
				_self._dispatchEvent();				
			}		
			return false;	
		})
		
			// Select Control
		this.selector.bind('change', function()
		{			
			_self.selectedIndex = $(this).val()
			_self.updateCounter();
			_self.manageControls()
			
			// dispatch event
			_self._dispatchEvent();				
		})		
	
		_self._redraw();
}

// PRIVATE METHODS
Pagination.prototype._redraw = function()
{
	  this.manageControls();
	  this.updateCounter();
	  this.updateSelector();
}

Pagination.prototype._dispatchEvent = function()
{
	   if(this.opts.onChange)
	   {
			if(typeof this.opts.onChange == 'function')
			{
				this.opts.onChange.call(this, {start: Number((this.selectedIndex -1) * this.showing), offset: this.showing})
			}
	   }
	
	   var ev = jQuery.Event("change");
	   ev.index = this.selectedIndex;
	   ev.start = (this.selectedIndex -1) * this.showing;
	   ev.offset = this.showing;
	   $(this).trigger(ev);		 
}

// PUBLIC METHODS SETTERS
Pagination.prototype.setIndex = function(value)
{
	this.selectedIndex = value;		
}

Pagination.prototype.setShow = function(value)
{
	this.showing = value;
	this._redraw();
	//this._dispatchEvent();	
}

Pagination.prototype.setTotal = function(value)
{
	this.total = value;
	this._redraw();	
	//this._dispatchEvent();
}

Pagination.prototype.reset = function()
{
	this.selectedIndex = 1;
	this._redraw();	
}

Pagination.prototype.setSelector = function()
{
	this.selector.val(this.selectedIndex);	
}

// PUBLIC METHODS GETTERS
Pagination.prototype.getSelectedIndex = function()
{
	return this.selectedIndex;
}

Pagination.prototype.getStart = function()
{
	return (this.selectedIndex -1) * this.showing;	
}

Pagination.prototype.getOffset = function()
{  
	 return this.showing;
}

Pagination.prototype.getPages =  function()
{
	return 	Math.ceil(this.total/this.showing);
}

Pagination.prototype.manageControls = function()
{
	if(this.selectedIndex <= 1)
	{
		this.prev.stop().animate({'opacity':0.3}, 200, 'swing');
	} else {
		this.prev.stop().animate({'opacity':1}, 200, 'swing');	
	}
	
	//
	if(this.selectedIndex >= this.getPages())
	{
		this.next.stop().animate({'opacity':0.3}, 200, 'swing');
		
	} else {
		
		this.next.stop().animate({'opacity':1}, 200, 'swing');
	}
	
	if(this.getPages() == 1)
	{
		this.selector.stop().animate({'opacity':0.3}, 200, 'swing');
	} else {
		this.selector.stop().animate({'opacity':1}, 200, 'swing');
	}
}

Pagination.prototype.updateCounter = function()
{
	if(this.counter)
	{
		this.counter.text(this.selectedIndex+"/"+ (this.getPages()));
	}
}


Pagination.prototype.updateSelector = function()
{
	if(this.selector)
	{		
		this.selector.empty();	
		for(var i=0; i< Number(this.getPages());i++)
		{
			this.selector.append("<option value='" + (i+1) + "'>" + (i+1) + "</option>")	
		}
		this.selector.val(this.selectedIndex);	
	}		
}
