
//
// EidoxPaginator
//

// History:
// - Thomas Brattli
// - David G. Miles
// - Carlos Castillo

//
// This will hold the main eidoxPaginator object
//
var ep = null;

// Class constructor

function EidoxPaginator(contentDiv,pagesDiv,prevButton,nextButton,prevEnabled,nextEnabled,prevDisabled,nextDisabled,pageHeight) {
    this.contentDiv		= document.getElementById(contentDiv); // Internal div
	this.pagesDiv		= document.getElementById(pagesDiv);   // Page nums
	this.fullHeight		= this.contentDiv.offsetHeight;        // Total height
	this.pageHeight		= +pageHeight;                         // Force int
	this.totalPages		= Math.ceil( this.fullHeight / this.pageHeight );
	this.prevButton		= prevButton;
	this.nextButton		= nextButton;

	// Methods
    this.scroll			= EidoxPaginatorScroll;
	this.next			= EidoxPaginatorNext;
	this.prev			= EidoxPaginatorPrev;
	this.buttons		= EidoxPaginatorButtons;

	// Images
	this.prevEnabled	= prevEnabled;
	this.nextEnabled	= nextEnabled;
	this.prevDisabled	= prevDisabled;
	this.nextDisabled	= nextDisabled;

	// Init
	this.scroll(0);
    return this 
} 

// Scrolling function

function EidoxPaginatorScroll(y){ 
    this.y=y 
    this.contentDiv.style.top=this.y + 'px';           // 'px' important for mozilla

	var currentPage = 1;
	if( this.y != 0 ) {
		currentPage = Math.round( (-this.y) / this.pageHeight ) + 1;
	}
	var msg = currentPage + "/" + this.totalPages;
	this.pagesDiv.innerHTML = "<span>" + msg + "</span>";
} 
 
// Previous/next page function

function EidoxPaginatorPrev() {
	if( this.y < 0 ) {
		this.scroll(this.y+this.pageHeight);
	}
	this.buttons();
}

function EidoxPaginatorNext() {
	if( this.fullHeight - this.pageHeight > -this.y ) {
		this.scroll(this.y-this.pageHeight);
	}
	this.buttons();
}

// Update buttons to enable/disable state

function EidoxPaginatorButtons() {
	if( this.y < 0 ) {
		if( this.prevButton.src != this.prevEnabled.src ) {
			this.prevButton.src = this.prevEnabled.src;
		}
	} else {
		if( this.prevButton.src != this.prevDisabled.src ) {
			this.prevButton.src = this.prevDisabled.src;
		}
	}
	if( this.fullHeight - this.pageHeight > -this.y ) {
		if( this.nextButton.src != this.nextEnabled.src ) {
			this.nextButton.src = this.nextEnabled.src;
		}
	} else {
		if( this.nextButton.src != this.nextDisabled.src ) {
			this.nextButton.src = this.nextDisabled.src;
		}
	}
}

