PaginationHeader =
{	
	//**************************//
	//customisable variables
	//**************************//
	_custom:
	{
		containerTop: "",	
		containerBottom: "",
		
		slider: "",
		
		window: 5,
		index: 0,
		limit: 0
	},
	
	//**************************//
	//not customisable variables
	//**************************//	
	_elements:
	{
		top:
		{
			wrapper: null,
			main: null,
			
			first: null,
			previous: null,
			pagesCollection: null,
			next: null,
			last: null
		},
		bottom:
		{
			wrapper: null,
			main: null,
			
			first: null,
			previous: null,
			pagesCollection: null,
			next: null,
			last: null
		}
	},
	
	initialize: function(options)
	{
		for(var option in options)
		{
		
			if (this._custom[option] != undefined)
			{
				this._custom[option] = options[option];
			}
		}
		
		this._setHeader();

	},
	
	_setHeader: function()
	{		
		
		var positions = ["top", "bottom"];	
		for (var i = 0; i < 2; i++)
		{

			var wrapperElement = $(document.createElement("div"));
			var classwrap='pag_box_cat';
			if (i>0){ classwrap=classwrap+"_b"; }
			wrapperElement.addClass(classwrap);

			var borderElement = $(document.createElement("div"));
			borderElement.appendTo(wrapperElement)
			
			var containerElement = $(document.createElement("div"));
			containerElement.addClass("pag_box_cat_container");
			containerElement.appendTo(borderElement);
			
			var rootElement = $(document.createElement("div"));
			rootElement.addClass("paging");
			rootElement.appendTo(containerElement);
			
			var labelElement = $(document.createElement("span"));
			labelElement.text("Pages:");
			labelElement.appendTo(rootElement);
		
			this._elements[positions[i]].first = $(document.createElement("a"));
			this._elements[positions[i]].first.addClass("first");
			this._elements[positions[i]].first.text("First");
			this._elements[positions[i]].first.appendTo(rootElement);

			this._elements[positions[i]].previous = $(document.createElement("a"));
			this._elements[positions[i]].previous.addClass("previous");
			this._elements[positions[i]].previous.text("Previous");
			this._elements[positions[i]].previous.appendTo(rootElement);
			
			this._elements[positions[i]].pagesCollection = $(document.createElement("span"));
			this._elements[positions[i]].pagesCollection.addClass("pages");
			this._elements[positions[i]].pagesCollection.appendTo(rootElement);
		
			this._elements[positions[i]].next = $(document.createElement("a"));
			this._elements[positions[i]].next.addClass("next");
			this._elements[positions[i]].next.text("Next");
			this._elements[positions[i]].next.appendTo(rootElement);
			
			this._elements[positions[i]].last = $(document.createElement("a"));
			this._elements[positions[i]].last.addClass("last");
			this._elements[positions[i]].last.text("Last");
			this._elements[positions[i]].last.appendTo(rootElement);
			
			this._elements[positions[i]].wrapper = wrapperElement;
			this._elements[positions[i]].main = rootElement;
			//console.log(wrapperElement);
			
		}
			
	},
	
	_addEvents: function()
	{
		var self = this;

		var positions = ["top", "bottom"];	
		for (var i = 0; i < 2; i++)
		{
			this._elements[positions[i]].main.find("a").each(function()
			{
				$(this).unbind("click");
			});
			
			this._elements[positions[i]].first.bind("click", function(event)
			{
				event.preventDefault();
				
				if (self._isEnabled(event.target))
				{
					self.updatePages(0);
					
					self._fireAction();
				}
			});
			
			this._elements[positions[i]].previous.bind("click", function(event)
			{
				event.preventDefault();
				
				if (self._isEnabled(event.target))
				{
					self.updatePages(self._custom.index - 1);
					
					self._fireAction();
				}
			});
			
			this._elements[positions[i]].next.bind("click", function(event)
			{
				event.preventDefault();
				
				if (self._isEnabled(event.target))
				{
					self.updatePages(self._custom.index + 1);
					
					self._fireAction();
				}
			});
			
			this._elements[positions[i]].last.bind("click", function(event)
			{
				event.preventDefault();
				
				if (self._isEnabled(event.target))
				{
					self.updatePages(self._custom.limit - 1);
					
					self._fireAction();
				}
			});	
			
			this._elements[positions[i]].main.find("span.pages a").each(function()
			{
				var element = this;
				$(this).bind("click", function(event)
				{
					event.preventDefault();
					
					self.updatePages(Number(element.rel));
					
					self._fireAction();
				});
			});
		}
	},
	
	updatePages: function(newIndex)
	{
		
		if ((newIndex != null) && (!isNaN(newIndex)) && (Number(newIndex) >= 0))
		{
			this._custom.index = newIndex;
		}

		var positions = ["top", "bottom"];	
		for (var i = 0; i < 2; i++)
		{		
			if (this._custom.index == 0)
			{
				this._elements[positions[i]].first.addClass("disabledPag");
				this._elements[positions[i]].previous.addClass("disabledPag");
			}
			else
			{
				this._elements[positions[i]].first.removeClass("disabledPag");
				this._elements[positions[i]].previous.removeClass("disabledPag");
			}

			if (this._custom.index == (this._custom.limit - 1))
			{
				this._elements[positions[i]].next.addClass("disabledPag");
				this._elements[positions[i]].last.addClass("disabledPag");
			}
			else
			{
				this._elements[positions[i]].next.removeClass("disabledPag");
				this._elements[positions[i]].last.removeClass("disabledPag");
			}	
			
			this._elements[positions[i]].pagesCollection.html("");
			var index = this._getBuildIndex();
			var windowWidth = index + this._custom.window;
			for(var j = index; (j < windowWidth) && (j < this._custom.limit); j++)
			{
				this._createPage(j).appendTo(this._elements[positions[i]].pagesCollection);
			}
			
			
			this._display();
		}
	},
	
	_fireAction: function()
	{
		if (this._custom.slider != "")
		{
			this._custom.slider.goToStage(this._custom.index, true);
		}
	},
	
	_display: function()
	{
		this._custom.containerTop.html(this._elements.top.wrapper);
		this._custom.containerBottom.html(this._elements.bottom.wrapper);
		
		this._addEvents();
	},
	
	_createPage: function(pageNumber)
	{
		var linkElement = $(document.createElement("a"));
		linkElement[0].rel = pageNumber;
		linkElement.text(Number(pageNumber) + 1);
		
		if (pageNumber == this._custom.index)
		{
			linkElement.addClass("selected");
		}
		return linkElement;
	},
	
	_getBuildIndex: function()
	{
		var buildIndex = 0;
		if (this._custom.index <= ((this._custom.window - 1) / 2))
		{
			buildIndex = 0;
		}
		else if (this._custom.index >= ((this._custom.limit - 1) - ((this._custom.window - 1) / 2)))
		{
			buildIndex = ((this._custom.limit - 1) - (this._custom.window - 1));
		}
		else 
		{
			buildIndex = this._custom.index - ((this._custom.window - 1) / 2);
		}
		
		return buildIndex;
	},
	
	_isEnabled: function(target)
	{
		return !$(target).hasClass("disabledPag");
	},
	
	setLimit: function(value)
	{
		this._custom.limit = value + 1;
	}
}
