// pageNavigator

var clsPageNavigator = function(oTargetElement, pageSize, sReturnFunc){
	this._targetElement = oTargetElement;				// ÆäÀÌÂ¡ ¿µ¿ª
	this._rtnFunc = sReturnFunc;						// ¸µÅ© Å¬¸¯½Ã ¹ÝÈ¯ÇÔ¼ö..
	this.PageSize = pageSize;							// ÇÑ¹ø¿¡ º¸¿©Áú ÆäÀÌÁöÀÇ °¹¼ö
	this.StartImageButton = null;
	this.EndImageButton = null;
	this.PreviousImageButton = null;
	this.NextImageButton = null;
	this.StartButtonEnable = true;
	this.EndButtonEnable = true;
	this.PreviousButtonEnable = true;
	this.NextButtonEnable = true;
	this.RecordCount = null;
	this.PageButtonCount = 10;
	this.CurrentPage = null;
	this.SelectedItemColor = "#FF0000";
	
	this.page_count = -1;
	this.startPage = -1;
	this.endPage = -1;
	
	this.startImageSrc = null;
	this.endImageSrc = null;
	this.previousImageSrc = null;
	this.nextImageSrc = null;
	
	this.isInit = false;
};			
			
clsPageNavigator.prototype.Init = function(){
	if (this.startImageSrc != null)
		this.StartImageButton = this.getImage(this.startImageSrc);
	else
		this.StartImageButton = document.createTextNode("[Ã³À½]");
	
	if (this.endImageSrc != null)
		this.EndImageButton = this.getImage(this.endImageSrc);
	else
		this.EndImageButton = document.createTextNode("[¸¶Áö¸·]");
	
	if (this.previousImageSrc != null)
		this.PreviousImageButton = this.getImage(this.previousImageSrc);
	else
		this.PreviousImageButton = document.createTextNode("[ÀÌÀü" + this.PageButtonCount + "°³]");
	
	if (this.nextImageSrc != null)
		this.NextImageButton = this.getImage(this.nextImageSrc);
	else
		this.NextImageButton = document.createTextNode("[´ÙÀ½" + this.PageButtonCount + "°³]");
	
	this.isInit = true;
};


clsPageNavigator.prototype.getImage = function(sSrc){
	var imgNode = new Image();
				
	imgNode.src = sSrc;
	imgNode.align = "absmiddle";
	imgNode.border = 0;
				
	return imgNode;
};
			

clsPageNavigator.prototype.Clear = function(){
	if(this._targetElement){
		var obj = this._targetElement;

		while(obj.childNodes[0])
			obj.removeChild(obj.childNodes[0]);
	}
};


clsPageNavigator.prototype.Render = function(iCurrentPage, iRecordCount){
	if (!this.isInit){
		this.Init();
	}
	this.CurrentPage = iCurrentPage;
	this.RecordCount = iRecordCount;
	
	this.Clear();
	if (this.PageSize <= 0){
		alert("ÆäÀÌÁö Å©±â°¡ ÁöÁ¤µÇÁö ¾Ê¾Ò½À´Ï´Ù.")
		return;
	}
		
	this.GeneratePager();
	this.isInit = false;
};

clsPageNavigator.prototype.GeneratePager = function(){
	
	this.page_count = Math.floor( this.RecordCount / this.PageSize );
	if ((this.RecordCount % this.PageSize) > 0)
		this.page_count++;
	
	this.startPage = Math.floor(this.CurrentPage / this.PageButtonCount) * this.PageButtonCount;
	this.endPage = this.startPage + (this.PageButtonCount - 1);
	
	if (this.endPage > this.page_count - 1)
		this.endPage = this.page_count - 1;
	
	var _startNode, _endNode, _prebiousNode, _nextNode, _pageNode;
	var _textNode, _fontNode;
	
	if (this.RecordCount > 0 ){
		if (this.StartButtonEnable){
			if (this.CurrentPage != 0){
				_startNode = document.createElement("a");
				_startNode.setAttribute("href", ("javascript:" + this._rtnFunc + "(0);"));
				_startNode.appendChild(this.StartImageButton);
			} else
				_startNode = this.StartImageButton;
			
			this._targetElement.appendChild(_startNode);
			this._targetElement.appendChild( document.createTextNode("  "));
		}
		
		if (this.PreviousButtonEnable) {
			if (this.startPage >= this.PageButtonCount)	{
				_prebiousNode = document.createElement("a");
				_prebiousNode.setAttribute("href", ("javascript:" + this._rtnFunc + "(" + (this.startPage - this.PageButtonCount) + ");"));
				_prebiousNode.appendChild(this.PreviousImageButton);
			} else
				_prebiousNode = this.PreviousImageButton;
				
			this._targetElement.appendChild(_prebiousNode);
			this._targetElement.appendChild( document.createTextNode("  "));
		}
		
		for (i=this.startPage;i<this.endPage+1;i++) {
			_textNode = document.createTextNode(i+1);
			
			if (i == this.CurrentPage){
				_pageNode = document.createElement("font");
				_pageNode.setAttribute("color",this.SelectedItemColor);
				_fontNode = document.createElement("b");
				_fontNode.appendChild(_textNode);
				_pageNode.appendChild(_fontNode);
			}else{
				_pageNode = document.createElement("a");
				_pageNode.setAttribute("href",("javascript:" + this._rtnFunc + "(" + i + ");"));
				_pageNode.appendChild(_textNode);
			}
			
			this._targetElement.appendChild(_pageNode);
			this._targetElement.appendChild( document.createTextNode("  "));
		}
		
		if (this.NextButtonEnable){
			if (this.endPage < (this.page_count - 1)){
				_nextNode = document.createElement("a");
				_nextNode.setAttribute("href", ("javascript:" + this._rtnFunc + "(" + (this.startPage + this.PageButtonCount) + ");"));
				_nextNode.appendChild(this.NextImageButton);
			} else
				_nextNode = this.NextImageButton;
				
			this._targetElement.appendChild(_nextNode);
			this._targetElement.appendChild( document.createTextNode("  "));
		}

		if (this.EndButtonEnable) {
			if (this.CurrentPage != parseInt(this.RecordCount / this.PageSize)){
				_endNode = document.createElement("a");
				if (this.RecordCount != 0 && ((this.RecordCount % this.PageSize) == 0))
					_endNode.setAttribute("href", ("javascript:" + this._rtnFunc + "(" + (Math.floor(this.RecordCount / this.PageSize)-1) + ");"));
				else
					_endNode.setAttribute("href", ("javascript:" + this._rtnFunc + "(" + (Math.floor(this.RecordCount / this.PageSize)) + ");"));
				_endNode.appendChild(this.EndImageButton);
			} else 
				_endNode = this.EndImageButton;
				
			this._targetElement.appendChild(_endNode);
		}
	}else{
		_fontNode = document.createElement("font");
		_fontNode.setAttribute("color",this.SelectedItemColor);
		_textNode = document.createElement("b");
		_textNode.appendChild(document.createTextNode(" "));
		_fontNode.appendChild(_textNode);
		
		this._targetElement.appendChild(_fontNode);
	}
};

clsPageNavigator.prototype.toString = function(){
	var str = "_targetElement : " + this._targetElement + "\n";
	str += "_rtnFunc : " + this._rtnFunc + "\n";
	str += "PageSize : " + this.PageSize + "\n";
	str += "StartImageButton : " + this.StartImageButton + "\n";
	str += "EndImageButton : " + this.EndImageButton + "\n";
	str += "PreviousImageButton : " + this.PreviousImageButton + "\n";
	str += "NextImageButton : " + this.NextImageButton + "\n";
	str += "StartButtonEnable : " + this.StartButtonEnable + "\n";
	str += "EndButtonEnable : " + this.EndButtonEnable + "\n";
	str += "PreviousButtonEnable : " + this.PreviousButtonEnable + "\n";
	str += "NextButtonEnable : " + this.NextButtonEnable + "\n";
	str += "RecordCount : " + this.RecordCount + "\n";
	str += "PageButtonCount : " + this.PageButtonCount + "\n";
	str += "CurrentPage : " + this.CurrentPage + "\n";
	str += "SelectedItemColor : " + this.SelectedItemColor + "\n";
	str += "page_count : " + this.page_count + "\n";
	str += "startPage : " + this.startPage + "\n";
	str += "endPage : " + this.endPage + "\n";
	str += "isInit : " + this.isInit ;
	
	alert(str);
};