1 
  2 //
  3 // EidoxPaginator
  4 //
  5 
  6 // History:
  7 // - Thomas Brattli
  8 // - David G. Miles
  9 // - Carlos Castillo
 10 // - Edgard Pineda
 11 
 12 //
 13 // This will hold the main eidoxPaginator object
 14 //
 15 var ep = null;
 16 
 17 // Class constructor
 18 
 19 function EidoxPaginator(contentDiv,pagesDiv,firstButton,prevButton,nextButton,lastButton,firstEnabled,lastEnabled,prevEnabled,nextEnabled,prevDisabled,nextDisabled,firstDisabled,lastDisabled,pageHeight,pageText,pagesDiv_top,firstButton_top,prevButton_top,nextButton_top,lastButton_top,pageText_top) {
 20     this.contentDiv		= document.getElementById(contentDiv); // Internal div
 21 	this.pagesDiv		= document.getElementById(pagesDiv);   // Page nums
 22 	this.fullHeight		= this.contentDiv.offsetHeight;        // Total height
 23 	this.pageHeight		= +pageHeight;                         // Force int
 24 	this.totalPages		= Math.ceil( this.fullHeight / this.pageHeight );
 25 	this.firstButton	= firstButton;
 26 	this.prevButton		= prevButton;
 27 	this.nextButton		= nextButton;
 28 	this.lastButton		= lastButton;
 29 	this.pageText		= pageText;
 30 
 31 	this.pagesDiv_top		= document.getElementById(pagesDiv_top);   // Page nums
 32 	this.firstButton_top	= firstButton_top;
 33 	this.prevButton_top		= prevButton_top;
 34 	this.nextButton_top		= nextButton_top;
 35 	this.lastButton_top		= lastButton_top;
 36 	this.pageText_top		= pageText_top;
 37 
 38 	// Methods
 39     this.scroll			= EidoxPaginatorScroll;
 40 	this.first			= EidoxPaginatorFirst;
 41 	this.prev			= EidoxPaginatorPrev;
 42 	this.next			= EidoxPaginatorNext;
 43 	this.last			= EidoxPaginatorLast;
 44 	this.buttons		= EidoxPaginatorButtons;
 45 	this.go				= EidoxPaginatorGo;
 46 
 47 	// Images
 48 	this.firstEnabled	= firstEnabled;
 49 	this.lastEnabled	= lastEnabled;
 50 	this.prevEnabled	= prevEnabled;
 51 	this.nextEnabled	= nextEnabled;
 52 	this.prevDisabled	= prevDisabled;
 53 	this.nextDisabled	= nextDisabled;
 54 	this.firstDisabled	= firstDisabled;
 55 	this.lastDisabled	= lastDisabled;
 56 
 57 	// Init
 58 	this.scroll(0);
 59     return this 
 60 } 
 61 
 62 function EidoxPaginatorGo(n) {
 63 	var y = 0;
 64 	var p = 1;
 65 	while( p < n && p < this.totalPages && this.fullHeight - this.pageHeight > -y ) {
 66 		y -= this.pageHeight;
 67 		p += 1;
 68 	}
 69 	this.scroll(y);
 70 	this.buttons();
 71 }
 72 
 73 // Scrolling function
 74 
 75 function EidoxPaginatorScroll(y){ 
 76     this.y=y 
 77     this.contentDiv.style.top=this.y + 'px';           // 'px' important for mozilla
 78 
 79 	var currentPage = 1;
 80 	if( this.y != 0 ) {
 81 		currentPage = Math.round( (-this.y) / this.pageHeight ) + 1;
 82 	}
 83 	var msg = this.pageText;
 84 	if( msg.length ) {
 85 		msg = msg.replace( /\$page/g, currentPage );
 86 		msg = msg.replace( /\$total/g, this.totalPages );
 87 	} else {
 88 		msg = currentPage + "/" + this.totalPages;
 89 	}
 90 	this.pagesDiv.innerHTML = "<span>" + msg + "</span>";
 91 	if(this.pagesDiv_top && this.pageText_top) {
 92 		msg = this.pageText_top;
 93 		if( msg.length ) {
 94 			msg = msg.replace( /\$page/g, currentPage );
 95 			msg = msg.replace( /\$total/g, this.totalPages );
 96 		} else {
 97 			msg = currentPage + "/" + this.totalPages;
 98 		}
 99 		this.pagesDiv_top.innerHTML = "<span>" + msg + "</span>";
100 	}
101 } 
102  
103 // First/last page function
104 function EidoxPaginatorFirst() {
105 	this.scroll(0);
106 	this.buttons();
107 }
108 
109 function EidoxPaginatorLast() {
110 	var y = this.y;
111 	while( this.fullHeight - this.pageHeight > -y ) {
112 		y -= this.pageHeight;
113 	}
114 	this.scroll(y);
115 	this.buttons();
116 }
117 
118 // Previous/next page function
119 
120 function EidoxPaginatorPrev(delta,objtimeout,timeout) {
121 	if( this.y < 0 ) {
122 		if(delta) {
123 			this.scroll(this.y+delta);
124 		} else {
125 			this.scroll(this.y+this.pageHeight);
126 		}
127 		if(objtimeout && timeout) {
128 			this.scrollPrevTimer = setTimeout(objtimeout + '.prev(' + delta + ',"' + objtimeout + '",' + timeout + ')',timeout);
129 		}
130 	}
131 	this.buttons();
132 }
133 
134 function EidoxPaginatorNext(delta,objtimeout,timeout) {
135 	if( this.fullHeight - this.pageHeight > -this.y ) {
136 		if(delta) {
137 			this.scroll(this.y-delta);
138 		} else {
139 			this.scroll(this.y-this.pageHeight);
140 		}
141 		if(objtimeout && timeout) {
142 			this.scrollNextTimer = setTimeout(objtimeout + '.next(' + delta + ',"' + objtimeout + '",' + timeout + ')',timeout);
143 		}
144 	}
145 	this.buttons();
146 }
147 
148 // Update buttons to enable/disable state
149 
150 function EidoxPaginatorButtons() {
151 	if( this.y < 0 ) {
152 		if( this.prevButton ) {
153 			if( this.prevButton.src != this.prevEnabled.src ) {
154 				this.prevButton.src = this.prevEnabled.src;
155 				if( this.firstButton ) this.firstButton.src = this.firstEnabled.src;
156 			}
157 		}
158 		if( this.prevButton_top ) {
159 			if( this.prevButton_top.src != this.prevEnabled.src ) {
160 				this.prevButton_top.src = this.prevEnabled.src;
161 				if( this.firstButton_top ) this.firstButton_top.src = this.firstEnabled.src;
162 			}
163 		}
164 	} else {
165 		if( this.prevButton ) {
166 			if( this.prevButton.src != this.prevDisabled.src ) {
167 				this.prevButton.src = this.prevDisabled.src;
168 				if( this.firstButton ) this.firstButton.src = this.firstDisabled.src;
169 			}
170 		}
171 		if( this.prevButton_top ) {
172 			if( this.prevButton_top.src != this.prevDisabled.src ) {
173 				this.prevButton_top.src = this.prevDisabled.src;
174 				if( this.firstButton_top ) this.firstButton_top.src = this.firstDisabled.src;
175 			}
176 		}
177 	}
178 	if( this.fullHeight - this.pageHeight > -this.y ) {
179 		if( this.nextButton ) {
180 			if( this.nextButton.src != this.nextEnabled.src ) {
181 				this.nextButton.src = this.nextEnabled.src;
182 				if( this.lastButton ) this.lastButton.src = this.lastEnabled.src;
183 			}
184 		}
185 		if( this.nextButton_top ) {
186 			if( this.nextButton_top.src != this.nextEnabled.src ) {
187 				this.nextButton_top.src = this.nextEnabled.src;
188 				if( this.lastButton_top ) this.lastButton_top.src = this.lastEnabled.src;
189 			}
190 		}
191 	} else {
192 		if( this.nextButton ) {
193 			if( this.nextButton.src != this.nextDisabled.src ) {
194 				this.nextButton.src = this.nextDisabled.src;
195 				if( this.lastButton ) this.lastButton.src = this.lastDisabled.src;
196 			}
197 		}
198 		if( this.nextButton_top ) {
199 			if( this.nextButton_top.src != this.nextDisabled.src ) {
200 				this.nextButton_top.src = this.nextDisabled.src;
201 				if( this.lastButton_top ) this.lastButton_top.src = this.lastDisabled.src;
202 			}
203 		}
204 	}
205 }
206