/**
* Paginador de arrays de elementos
* @version 1.0
*/

com = Class.create();

com.lp = Class.create();

com.lp.Paginador = Class.create();
com.lp.Paginador.prototype =
{
	cantidadDePaginas: 0, //@readonly cantidad de páginas de 1 a n
	posPagina: 0, 		  // página actual de 1 a n
	cantXpage: 6,
	desde: -1, // privado
	posItem: -1, // privado
	hasta: -2, // privado
	cantRegs: 0, // privado
	datos: [], // array con items a mostrar
	divNavegador: undefined,
	objName: "",
	initialize:  function(objName){
		this.objName = objName;
	},	
	
	/**
	 * Setea el array de datos a mostrar
	 * @public
	 */
	setArray: function(array){
		if(array != undefined && array.length != undefined)	{
			this.datos = array;	
			this.cantidadDePaginas = Math.ceil(this.datos.length/this.cantXpage);
			this.firstPage(true);
		}else{
			alert("Array no valido");
		}
	},

	/**
	 * Setea la cantidad de items por página a mostrar
	 * @public
	 */	
	setCantXpage: function(cant){
		if(!isNaN(cant) && cant > 0){
			this.cantXpage = Number(cant);
			this.cantidadDePaginas = Math.ceil(this.datos.length/this.cantXpage);			
			this.firstPage(true);
		}
	},
		
	/**
	 * Establece el div donde mostrar el navegador
	 */
	setContentNavegator: function(divNav){
		this.divNavegador = document.getElementById(divNav);
	},	
	
	nextPage: function(){
		var ultimo = this.datos.length - this.datos.length%this.cantXpage;
		if(ultimo >= this.datos.length) ultimo -= this.cantXpage;
		if(this.desde < ultimo){
			this.desde += this.cantXpage;
			if(this.desde > ultimo) this.desde = ultimo;
			this.refreshNavegator();
		}
	},
	
	prevPage: function(){
		if(this.desde > 0){
			this.desde -= this.cantXpage;
			if(this.desde < 0 ) this.desde = 0;
			this.refreshNavegator();				
		}
	},
	
	firstPage: function(force){
		if(this.desde != 0 || (force != undefined && force)){
			this.desde = 0;
			this.refreshNavegator();		
		}
	},
	
	lastPage: function(){
		var ultimo = this.datos.length - this.datos.length%this.cantXpage;
		if(ultimo >= this.datos.length) ultimo -= this.cantXpage;
		if(this.desde < ultimo){
			this.desde = ultimo;
			this.refreshNavegator();
		}
	},
	
	
	toPage: function(pageNum){
		if(!isNaN(pageNum) && pageNum >= 1 && pageNum <= this.cantidadDePaginas){
			this.desde = (pageNum-1)*this.cantXpage;
			this.refreshNavegator();
		}		
	},
	
	

	/**
	 * @return (true|false) Hay mas items a mostrar
	 * @protected
	 */
	moreItem: function(){
		return (this.posItem <= this.hasta && this.posItem >= this.desde  );
	},
	/**
	 * @return Retorna el item apuntado actualmente
	 * @protected
	 */
	currentItem: function(){
		if(this.moreItem()){
			return this.datos[this.posItem];
		}else{
			return undefined;
		}
	},
	
	/**
	 * Avanza una posición el puntero a los items
	 * @protected
	 */
	moveNext: function(){
		this.posItem++;
	},
		
	
	/**
	 * Setea el rango de items a mostrar
	 * @private
	 */
	calcRango: function(){
		this.posItem = this.desde;
		this.hasta = this.desde + this.cantXpage-1;
		if(this.hasta >= this.datos.length) this.hasta = this.datos.length-1;
	},
	
	/**
	 * Actualiza el navegador
	 * @private
	 */
	refreshNavegator: function(){
		this.calcRango();
		this.refresh();		
		this.posPagina = Math.floor(this.desde/this.cantXpage)+1;
		if(this.cantidadDePaginas==0) this.posPagina = 0;

		if(this.divNavegador != undefined){
			var pages = [];
			for(var i=1;i<=this.cantidadDePaginas;i++){
				if(i == this.posPagina){
					pages.push(i);
				}else{
					pages.push("<a href='javascript:"+this.objName+".toPage("+i+");'>"+i+"</a>");
				}
			}
			var info = pages.join(" | ");
			this.divNavegador.innerHTML = info;			
		}
	},

	/**
	* Metodo abstracto
	* Es el encargado de mostrar los items en pantalla
	* Para recorrer los items a mostrar se debe utilizar los metodos:
	* 	moreItem()
	*   currentItem()
	* 	moveNext()
	*/
	refresh: function(){}	
}

