/**
 * by Ke20
 * wednesday, april 8th 
 */

function PaginationOfTable()
{
	// variables
	var _nbElements = 0;
	var _elements = new Array();
	var _nbElementsByPage = 0;
	var _currentPage = 0;
	var _nbPages = 0;
	var _table = null;
	var _idPages = null;
	
	// methodes
	this.init = function(nbElementsByPage, idTable, idRefForPages) 
	{
		this._elements = new Array();
		this._nbElementsByPage = nbElementsByPage;
		this._table = document.getElementById(idTable);
		this._idPages = idRefForPages;
		this._currentPage = 0;
		if(!this._table) {
			return false;
		}
		
		// récupèration des éléments à affiches
		for(var i = 0; i < this._table.getElementsByTagName("tr").length; i++) 
		{
			if(this._table.getElementsByTagName("tr")[i].parentNode.nodeName.match(/thead/i))
				continue;
			
			if(this._table.getElementsByTagName("tr")[i].parentNode.nodeName.match(/tfoot/i))
				continue;
			
			if(this._table.getElementsByTagName("tr")[i].childNodes[1].nodeName.match(/th/i))
				continue;
			
			this._elements.push(this._table.getElementsByTagName("tr")[i]);
		}
		
		this._nbElements = this._elements.length;
		this._nbPages = this._nbElements / this._nbElementsByPage;
		
		this.displayRows();
	};
	
	this.displayRows = function() 
	{
		this.displayPages();
		
		nbElementToDisplay = this._nbElements % this._nbElementsByPage;
		
		var firstElement = this._nbElementsByPage * this._currentPage;
		var lastElement = firstElement + this._nbElementsByPage;
		
		for(var i = 0; i < this._elements.length; i++) 
		{
			this._elements[i].style.display = 
				(i >= firstElement && i < lastElement) ? "" : "none";
		}
	};
	
	this.displayPages = function()
	{
		if(this._nbPages == 0) 
			return false;
			
		if(document.getElementById(this._table.id + "pages"))
		{
			var div = document.createElement("div");
			div.id = this._table.id + "pages";
			var oldDiv = document.getElementById(this._table.id + "pages");
			oldDiv.parentNode.replaceChild(div, oldDiv);
		}
		else {
			
			var div = document.createElement("div");
			div.id = this._table.id + "pages";
		}
		
		var refNodePage = (!this._idPages) ? this._table : document.getElementById(this._idPages);
		refNodePage.parentNode.insertBefore(div, refNodePage);
		
		
		var ponctuation = "...";
		var marge = 3; 
		
		var previous = document.createElement("a");
		previous.innerHTML = "< prev | ";
		previous.style.cursor = "pointer";
		previous.style.textDecoration = "none";
		previous.onclick = (function(e) { return function() { e.previousPage(); }; })(this);
		div.appendChild(previous);
		
		var isPonct = false;
		for(var i = 0; i < this._nbPages; i++)
		{	
			var a = document.createElement("a");
			a.innerHTML  = (i != 0) ? ", " : "";
			a.innerHTML += (i+1);
			a.style.cursor = "pointer";
			a.style.textDecoration = "none";
			a.style.color = ((i) == this._currentPage) ? "red" : "";
			a.onclick = (function(e, page){ return function() { e.setPage(page); }; })(this, i);
			
			var span = document.createElement("span");
			span.innerHTML = ponctuation;
			
			
			if((i < marge) || (i > (this._nbPages - (marge + 1)))) {
				div.appendChild(a);
			}
			else 
			{	
				if((i == this._currentPage) || (i == this._currentPage - 1) || (i == this._currentPage + 1))
				{
					if(i == this._currentPage - 1) {
						if(this._currentPage > (marge + 1)) {
							div.appendChild(span);
						}
					}
					
					div.appendChild(a);
					
					if(i == this._currentPage + 1) {
						if((this._currentPage + marge) < (this._nbPages - marge)) {
							div.appendChild(span);
						}
					}
				}
				else
				{
					if(!isPonct)
					{
						if(this._currentPage < (this._nbPages)) {
							div.appendChild(span);
							isPonct = true;
						}		
					}
				}
			}
		}
		
		var next = document.createElement("a");
		next.innerHTML = " | next >";
		next.style.cursor = "pointer";
		next.style.textDecoration = "none";
		next.onclick = (function(e) { return function() { e.nextPage(); }; })(this);
		div.appendChild(next);
	};
	
	this.nextPage = function()
	{
		if(this._currentPage < (this._nbPages - 1))
			this._currentPage++;
		this.displayRows();
	};
	
	this.previousPage = function()
	{
		if(this._currentPage > 0)
			this._currentPage--;
		this.displayRows();
	};
	
	this.setPage = function(numPage)
	{
		if(numPage >= 0 && numPage < this._nbPages)
			this._currentPage = numPage;
		this.displayRows();
	}
};
