/**
 *version:0.5
 *lastModified:2008-01-10
 *author:zhangweiit@yahoo.com
 */
function ETPagination(aName, aContainer, aJumpHandler){
	var mName = aName;
	var mContainer = aContainer;
	var mJumpHandler = aJumpHandler;
	var mBarContainer = null;
	var mOffset = 0;
	var mPageSize = 10;
	var mTotal = 0;
	var mPageIndex = 0;
	var mPageCount = 0;
	var mButtonCount = 10;
	
	this.getOffset = function(){return mOffset;};
	this.setOffset = function(aValue){mOffset = parseInt(aValue,10);};

	this.getPageSize = function(){return mPageSize;};
	this.setPageSize = function(aValue){mPageSize = parseInt(aValue,10);};
	
	this.getTotal = function(){return mTotal;};
	this.setTotal = function(aValue){mTotal = parseInt(aValue,10);};

	this.getJumpHandler = function(){return mJumpHandler;};
	this.setJumpHandler = function(aValue){mJumpHandler = aValue;};

	this.getButtonCount = function(){return mButtonCount;};
	this.setButtonCount = function(aValue){mButtonCount = parseInt(aValue,10);};

	this.getPageIndex = function(){return mPageIndex;};
	
	this.getPageCount = function(){return mPageCount;};

	this.refreshCounter = function(){
		mPageCount = (mTotal % mPageSize == 0) ?
			parseInt(mTotal / mPageSize,10) : (parseInt(mTotal / mPageSize,10) + 1);
		mPageIndex = parseInt(mOffset / mPageSize,10) + 1;
	};

	this.getOffsetByIndex = function(aIndex){
		var ret = 0;
		if(aIndex > mPageCount){aIndex = mPageCount;}
		if(aIndex < 1){aIndex = 1;}
		ret = (aIndex - 1) * mPageSize;
		return ret;
	};

	this.getFirstOffset = function(){
		return this.getOffsetByIndex(1);
	}

	this.getLastOffset = function(){
		return this.getOffsetByIndex(mPageCount);
	}

	this.getPrevOffset = function(){
		return this.getOffsetByIndex(mPageIndex - 1);
	}

	this.getNextOffset = function(){
		return this.getOffsetByIndex(mPageIndex + 1);
	}
	
	this.construct = function(){
		var new_ele = document.createElement('div');
		new_ele.style.padding = '0px';
		new_ele.style.border = '0px';
		new_ele.style.margin = '0px';
		new_ele.style.textAlign = 'center';
		mBarContainer = new_ele;
		if(mContainer){
			mContainer.appendChild(new_ele);
		}
	}

	this.createBar = function(){
		this.clearBar();
		var btn = null;
		if(mPageCount <= 0){
			mBarContainer.appendChild(document.createTextNode('暂无数据可以分页显示'));
		}else if(mPageCount <= mButtonCount){
			for(var i = 1; i <= mPageCount; i++){
				btn = this.createButtonByIndex(i,'');
				mBarContainer.appendChild(btn);
				mBarContainer.appendChild(document.createTextNode(' '));
			}
		}else if(mPageIndex <= parseInt(mButtonCount / 2, 10)){
			for(var i = 1; i <= mButtonCount - 2; i++){
				btn = this.createButtonByIndex(i,'');
				mBarContainer.appendChild(btn);
				mBarContainer.appendChild(document.createTextNode(' '));
			}
			btn = this.createButtonByIndex(mPageIndex + 1,'&gt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			btn = this.createButtonByIndex(mPageCount,'&gt;|');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
		}else if(mPageIndex >= mPageCount - parseInt(mButtonCount / 2, 10)){
			btn = this.createButtonByIndex(1,'|&lt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			btn = this.createButtonByIndex(mPageIndex - 1,'&lt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			for(var i = mPageCount - mButtonCount + 3; i <= mPageCount; i++){
				btn = this.createButtonByIndex(i,'');
				mBarContainer.appendChild(btn);
				mBarContainer.appendChild(document.createTextNode(' '));
			}
		}else{
			btn = this.createButtonByIndex(1,'|&lt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			btn = this.createButtonByIndex(mPageIndex - 1,'&lt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			for(var i = mPageIndex - parseInt(mButtonCount / 2, 10) + 2; i <= mPageIndex + parseInt(mButtonCount / 2, 10) - 2; i++){
				btn = this.createButtonByIndex(i,'');
				mBarContainer.appendChild(btn);
				mBarContainer.appendChild(document.createTextNode(' '));
			}
			btn = this.createButtonByIndex(mPageIndex + 1,'&gt;');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
			btn = this.createButtonByIndex(mPageCount,'&gt;|');
			mBarContainer.appendChild(btn);
			mBarContainer.appendChild(document.createTextNode(' '));
		}
	}

	this.clearBar = function(){
		while(mBarContainer.firstChild){
			mBarContainer.removeChild(mBarContainer.firstChild);
		}
	}

	this.rebuild = __rebuild;
	function __rebuild(aOffset,instance){
		instance.setOffset(aOffset);
		instance.refreshCounter();
		instance.createBar();
	}

	this.createButtonByIndex = function(aPageIndex,aText){
		var new_ele = document.createElement('span');
		var offset = this.getOffsetByIndex(aPageIndex);
		if(aText == ''){
			new_ele.innerHTML = '[' + aPageIndex + ']';
		}else{
			new_ele.innerHTML = aText;
		}
		var myInstance = this;
		new_ele.style.paddingLeft = '5px';
		new_ele.style.paddingRight = '5px';
		if(aPageIndex == mPageIndex){
			new_ele.style.fontWeight = 'bold';
			new_ele.fontSize = '14px';
			new_ele.style.backgroundColor = '#FFFFFF';
			new_ele.style.color = '#FF0000';
			new_ele.style.cursor = 'default';
			new_ele.onclick = null;
			new_ele.onmouseover = null;
			new_ele.onmouseover = null;
		}else{
			new_ele.style.cursor = 'pointer';
			new_ele.fontSize = '12px';
			new_ele.style.color = '#000000';
			new_ele.style.backgroundColor = '#FFFFFF';
			new_ele.onclick = function(e){
				var toContinue = true;
				if('function' == typeof mJumpHandler){
					toContinue = mJumpHandler(offset,mPageSize,mName);
				}
				if(toContinue){
					__rebuild(offset,myInstance);
				}
			};
			new_ele.onmouseover = function(){
				this.style.backgroundColor = '#CCCCCC';
			};
			new_ele.onmouseout = function(){
				this.style.backgroundColor = '#FFFFFF';
			};
		}
		
		//new_ele.insertBefore(document.createTextNode(' '),new_ele.firstChild);
		//new_ele.appendChild(document.createTextNode(' '));
		return new_ele;
	}

	this.construct();
}

var et_pagination_js = 1;