TablePager = function(){};
TablePager.prototype.initialize = function(items, itemsPerPage, container){
	if(!this._currentPage) this._currentPage=1;
	this._items = items;
	this._itemsPerPage = itemsPerPage;
	this._container = container;
	this._totalPages=Math.ceil(this._items.length/this._itemsPerPage);
	this.draw();
	this.getPage(1);
}
TablePager.prototype.draw = function(){
		while(this._container.firstChild){ this._container.removeChild(this._container.firstChild); }
		if(this._totalPages<=1)return;
		this._buttons= new Array();

		// prev page
		var btn;
		if(this._currentPage>1){
			btn = document.createElement("a");
			btn.href="javascript:void();";
			btn.onclick = this.prevPage.bindAsEventListener(this);
		}else{
			btn = document.createElement("span");
		}
		btn.className="arrow";
		btn.appendChild(document.createTextNode("«"));
		this._buttons.push(btn);
		
		// page numbers
		for(var i=0; i<this._totalPages; i++){
			if(i>0){
				var sep = document.createTextNode("|");
				this._buttons.push(sep);
			}
			if(i+1==this._currentPage){
				var btn = document.createElement("span");
				btn.className="selected";
			}else{
				var btn = document.createElement("a");
				btn.href="javascript:void();";
				btn.onclick = function(){ this.pageOnClick(this.getAttribute("id").split("pagerItem")[1])}
				btn.pageOnClick=this.pageOnClick.bindAsEventListener(this);
			}
			btn.setAttribute("id", "pagerItem"+(i+1));
			btn.appendChild(document.createTextNode(i+1));
			this._buttons.push(btn);
		}

		// next page
		if(this._currentPage<this._totalPages){
			btn = document.createElement("a");
			btn.href="javascript:void();";
			btn.onclick = this.nextPage.bindAsEventListener(this);
		}else{
			btn = document.createElement("span");
		}
		btn.className="arrow";
		btn.appendChild(document.createTextNode("»"));
		this._buttons.push(btn)
		
		var ul = document.createElement("ul");
		for(var i=0; i<this._buttons.length; i++){
			var li = document.createElement("li");
			li.appendChild(this._buttons[i]);
			ul.appendChild(li);
			
		}
		this._container.appendChild(ul);
}
TablePager.prototype.pageOnClick = function(id){
		this.getPage(id);
}
TablePager.prototype.prevPage = function(){
		this.getPage(this._currentPage-1);
}
TablePager.prototype.nextPage = function(){
		this.getPage(this._currentPage+1);
}
TablePager.prototype.removeItem = function(item){
		item.addClassName('paginable');
		this._items = this._items.without(item);
		this.getPage(this._currentPage);
}

TablePager.prototype.getPage = function(pageNum){
	pageNum = parseInt(pageNum);
	this._currentPage=(pageNum>this._totalPages)? this._totalPages : pageNum;
	this.draw();
	var start = (this._currentPage-1) * this._itemsPerPage;
	var end = start + this._itemsPerPage;
	this._items.each(function(item, i){
		if(i>=start && i<end){
			if(item.hasClassName('paginable')) item.removeClassName('paginable');
//			item.style.backgroundImage = "url(../images/linea_04.gif)"
		}else{
			if(!item.hasClassName('paginable')) item.addClassName('paginable');
		}
	})
}