﻿// autor Angel Casillas
// pagination.js requires slices.js

function Pagination(container,items,itemsPerPage,ant,sig){

	this.container = getObject(container);
	this.items = items;
	this.itemsPerPage = itemsPerPage;
	this.ant = getObject(ant);
	this.sig = getObject(sig);
	
	this.listaInteractiva = new ObjetoListaInteractiva();
	
	this.initPagination=initPagination;	
	
	this.anterior=anterior;	
	this.siguiente=siguiente;	
	this.setElementosVisiblesPagination=setElementosVisiblesPagination;	
	this.getElementosVisiblesPagination=getElementosVisiblesPagination;	
	this.setSlicePagination=setSlicePagination;	
	this.getSlicePagination=getSlicePagination;	
	this.setModoOnPagination=setModoOnPagination;	
	this.getModoOnPagination=getModoOnPagination;	
	this.setModoOffPagination=setModoOffPagination;	
	this.getModoOffPagination=getModoOffPagination;	
	
}


function initPagination(){
	
	if (this.container) {
			
		var containedItems = this.container.getElementsByTagName(this.items);	
		if (containedItems && containedItems.length > this.itemsPerPage) {
				
			var total = containedItems.length;
			var cont = document.createElement(this.container.tagName);
			cont.id=this.container.tagName+this.container.id+"_0";
			cont.className=this.getModoOffPagination();
			var no=0;
			var cno=1;
			while(no<total){
				cont.appendChild(containedItems[0]);
				if ((no >= this.itemsPerPage-1) && ((no + 1) % this.itemsPerPage == 0)) {
					this.container.parentNode.insertBefore(cont, this.container);
					this.listaInteractiva.anadir(cont.id);
					
					cont = document.createElement(this.container.tagName);
					cont.id=this.container.tagName+this.container.id+"_"+cno;
					cont.className=this.getModoOffPagination();
					cno++;
				}
				no++;
			}
			if (total % this.itemsPerPage != 0) {
				this.container.parentNode.insertBefore(cont, this.container);
				this.listaInteractiva.anadir(cont.id);
			}
			this.container.parentNode.removeChild(this.container);
			
			this.listaInteractiva.init();
						
			if (this.ant)
				this.ant.className = this.getModoOffPagination();		
			if (this.sig && this.listaInteractiva.isLastPosition()) {
				this.sig.className = this.getModoOffPagination();
			}
		}
	}
}

function anterior(){
	this.listaInteractiva.pasas(0,'');
	if (this.ant && this.listaInteractiva.getPosicion()==1) {
		this.ant.className = this.getModoOffPagination();
	}
	if (this.sig && !this.listaInteractiva.isLastPosition()) {
		this.sig.className = this.getModoOnPagination();
	}
}

function siguiente(){
	this.listaInteractiva.pasas(1,'');
	if (this.sig && this.listaInteractiva.isLastPosition()) {
		this.sig.className = this.getModoOffPagination();
	}
	if (this.ant && this.listaInteractiva.getPosicion()>1) {
		this.ant.className = this.getModoOnPagination();
	}
}

function setElementosVisiblesPagination(numero){
	this.listaInteractiva.setElementosVisibles(numero);
}

function getElementosVisiblesPagination(){
	return this.listaInteractiva.getElementosVisibles();
}

	
function setSlicePagination(val){
	this.listaInteractiva.setSlice(val);
}

function getSlicePagination(){
	return this.listaInteractiva.getSlice();
}

function setModoOnPagination(clase){
	this.listaInteractiva.setModoOn(clase);
}

function getModoOnPagination(){
	return this.listaInteractiva.getModoOn();
}

function setModoOffPagination(clase){
	this.listaInteractiva.setModoOff(clase);
}

function getModoOffPagination(){
	return this.listaInteractiva.getModoOff();
}


