/* PUSH  FOR IE5 */
if(typeof Array.prototype.push=="undefined"){
	Array.prototype.push=function(){
		var n=this.length>>>0;
		for(var i=0;i<arguments.length;i++) {
			this[n]=arguments[i];
			n =n+1>>>0;
		}
		this.length=n;
		return n;
	};
}


var pagingControl = Class.create({
	initialize:function(containerDiv, dataArrayLength, labelElement){
		this.containerDiv=containerDiv;
		this.labelElement=$(labelElement);
		this.labelElement.hide();
		containerDiv.pagingControl=this;  //this is so we can external references to the control
		this.pageControls=[];
		this.howMany2Display=5;
		this.totalPages=0;
		this.recordsPerPage=5;
		this.currentPage=1;
		this.previousPage=1;
		this.prevControl=null;
		this.nextControl=null;
		this.firstControl=null;
		this.lastControl=null;
		this.groupBeforeControl=null;
		this.groupAfterControl=null;
	},
	reset:function( dataArrayLength,recordsPerPage){
		this.containerDiv.removeAllChildern();
		this.uiRoot=document.createElement('ul');
		this.uiRoot.className='pagingLayout';
		this.containerDiv.appendChild(Element.extend(this.uiRoot));
		this.arrayLength=dataArrayLength;
		this.recordsPerPage=recordsPerPage;
		this.currentPage=1;
		this.previousPage=1;
		if(dataArrayLength>0){
			this.totalPages=Math.ceil(dataArrayLength/recordsPerPage);
			this.draw(this.uiRoot);		}

		this.updateControlContainer(this);
	},
	updateControlContainer:function(control){ //this will make handling events outside the control easier
		var con=control.containerDiv;
		con.arrayLength=control.arrayLength;
		con.recordsPerPage=control.recordsPerPage;
		con.totalPages=control.totalPages;
		con.currentPage=control.currentPage;
		con.indexStart=(control.currentPage-1)*control.recordsPerPage;
		con.indexStop=control.recordsPerPage+con.indexStart
		if(con.indexStop>control.arrayLength) con.indexStop=control.arrayLength;
	},
	onPrevClick:function(e){
		var el=Event.element(e);
		Event.stop(e);
		if(this.currentPage<=1) return;
		this.previousPage=this.currentPage;
		this.setCurrentPage(this.currentPage-1);
		this.updateControlContainer(this);
		this.containerDiv.fire('pagingControl:onChange');
	},

	onNextClick:function(e){
		var el=Event.element(e);
		Event.stop(e);
		if(this.currentPage>=Math.ceil(this.arrayLength/this.recordsPerPage)) return;
		this.previousPage=this.currentPage;
		this.setCurrentPage(this.currentPage+1);
		this.updateControlContainer(this);
		this.containerDiv.fire('pagingControl:onChange');
	},
	onPageClick:function(e){
		var el=Event.element(e);
		Event.stop(e);
		if(this.currentPage==el.pageNumber) return;
		this.previousPage=this.currentPage;
		this.setCurrentPage(el.pageNumber);
		this.updateControlContainer(this);
		this.containerDiv.fire('pagingControl:onChange');
	},
	onFirstClick:function(e){
		Event.stop(e);
		this.previousPage=this.currentPage;
		this.setCurrentPage(1);
		this.updateControlContainer(this);
		this.containerDiv.fire('pagingControl:onChange');
	},
	onLastClick:function(e){
		Event.stop(e);
		this.previousPage=this.currentPage;
		this.setCurrentPage(this.totalPages);
		this.updateControlContainer(this);
		this.containerDiv.fire('pagingControl:onChange');
	},
	onGroupBeforeClick:function(e){
		Event.stop(e);
		if(this.startDrawingWithPage-this.howMany2Display>0){
			this.previousPage=this.currentPage;
			this.setCurrentPage(this.startDrawingWithPage-1 );
			this.updateControlContainer(this);
			this.containerDiv.fire('pagingControl:onChange');
		}
	},
	onGroupAfterClick:function(e){
		Event.stop(e);
		if(this.startDrawingWithPage+this.howMany2Display<=this.totalPages){
			this.previousPage=this.currentPage;
			this.setCurrentPage(this.startDrawingWithPage+this.howMany2Display);
			this.updateControlContainer(this);
			this.containerDiv.fire('pagingControl:onChange');
		}
	},

	setCurrentPage:function(pagenumber){
		if(this.pageControls.length==0) return;
		var pages,N2Display,startPage;
		pages=this.totalPages;
		this.pageControls[this.currentPage-1].className='pagingPageNormal';
		this.previousPage=this.currentPage;
		this.currentPage=pagenumber;
		this.pageControls[this.currentPage-1].className='pagingPageSelected';
		N2Display=this.howMany2Display;
		//from page# figure out which page to start with.
		this.startDrawingWithPage = ((Math.ceil(pagenumber/N2Display)-1)*N2Display)+1;
		startPage=this.startDrawingWithPage;

		//setup pages to show, based upon how many to show and what grouping is active.
		for (var v=1,len=pages+1;v<len;v++) {
			if(v<startPage || v>=(startPage+N2Display) )
				this.pageControls[v-1].parentNode.hide();
			else
				this.pageControls[v-1].parentNode.show();
		}

		if(pagenumber<=1) {
			this.prevControl.className='pagingDisabled';
			this.firstControl.className='pagingDisabled';
		}
		else {
			this.prevControl.className='pagingEnabled';
			this.firstControl.className='pagingEnabled';
		}
		if(pagenumber>=Math.ceil(this.arrayLength/this.recordsPerPage)) {
			this.nextControl.className='pagingDisabled';
			this.lastControl.className='pagingDisabled';
		}
		else {
			this.nextControl.className='pagingEnabled';
			this.lastControl.className='pagingEnabled';
		}
		if(startPage > N2Display) this.groupBeforeControl.parentNode.show(); else this.groupBeforeControl.parentNode.hide();
		if(pages > N2Display && startPage < pages) this.groupAfterControl.parentNode.show(); else this.groupAfterControl.parentNode.hide();

	},
	createControl:function(root,text,className){
		var c=root.$cE('li').$cE('a',{href:'#'});
		c.$cT(text);
		if(className) c.className=className;
		return c;
	},
	draw:function(root){
		var pages=this.totalPages;
		if(pages==1){this.labelElement.hide(); return; }
		this.labelElement.show();
		this.firstControl=this.createControl(root,'< First ','pagingDisabled');
		Event.observe(this.firstControl, 'click',this.onFirstClick.bindAsEventListener(this), false);

		this.prevControl=this.createControl(root,'< Prev ','pagingDisabled');
		Event.observe(this.prevControl, 'click',this.onPrevClick.bindAsEventListener(this), false);


		this.groupBeforeControl=this.createControl(root,' ... ', 'pagingPageNormal');
		Event.observe(this.groupBeforeControl, 'click',this.onGroupBeforeClick.bindAsEventListener(this), false);

		for (var i=this.pageControls.length; i--;) { delete this.pageControls[i]; } //prevent menory leaks
		this.pageControls=[];
		for (var v=1,len=pages+1;v<len;v++) {
			var a=this.createControl(root,' '+v+' ','pagingPageNormal');
			a.pageNumber=v;
			Event.observe(a, 'click',this.onPageClick.bindAsEventListener(this), false);
			this.pageControls.push(a);
		}

		this.groupAfterControl=this.createControl(root,' ... ', 'pagingPageNormal');
		Event.observe(this.groupAfterControl, 'click',this.onGroupAfterClick.bindAsEventListener(this), false);

		this.nextControl = this.createControl(root, ' Next >', 'pagingEnabled');
		Event.observe(this.nextControl, 'click',this.onNextClick.bindAsEventListener(this), false);

		this.lastControl = this.createControl(root, ' Last >', 'pagingEnabled');
		Event.observe(this.lastControl, 'click',this.onLastClick.bindAsEventListener(this), false);

		this.pageControls[0].className='pagingPageSelected'; //set 1 page selected
		this.setCurrentPage(1);
	}
});
