/**
 * Paging Object
 * @param {String} id 		The id of Paging element
 * @param {String} section	Separator
 * @param {int} pageScale 	Page Scale
 * @param {int} listScale 	List Scale
 */
var Paging = function(id, section, pageListElement, pagingFunction){
	this.init(id, section, pageListElement, pagingFunction);
}


Paging.config = {
	imageroot: '/images'
};


Paging.prototype = {
	/**
	 * List Scale
	 * @property listScale
	 * @type int
	 */
	listScale: 10,
	/**
	 * Page Scale
	 * @property pageScale
	 * @type int
	 */
	pageScale: 10,
	/**
	 * The id of Paging Object
	 * @property id
	 * @type String
	 */
	id: null,
	/**
	 * Separator
	 * @property section
	 * @type String
	 */
	section:"-",
	/**
	 * Page List HTML Element Name
	 * @property pageListElement
	 * @type String
	 */
	pageListElement:"pagingNumberView",

	/**
	 * Generates the link that will invoke this page list method
	 * @property pagingFunction
	 * @type Object
	 */
	pagingFunction:null,

	/**
	 * use image button. default true.
	 * @property isImageButton
	 * @type boolean
	 */
	isImageButton:true,

	defaultImgGoFirst:"◀◀",
	defaultImgGoPrev:"◀",
	defaultImgGoNext:"▶",
	defaultImgGoLast:"▶▶",

	/**
	 * Initializes the Paging
	 * @method init
	 * @param {String|HTMLElement} 	id the id of the element that will hold the Paging
	 * @param {String} section		Separator
	 * @param {int} pageScale		Page Scale
	 * @param {int} listScale		List Scale
	 * @param {function} pagingFunction	Generates the link that will invoke this page list method
     * @private
	 */
	init: function(id, section, pageListElement, pagingFunction){
		if(id){
			this.id = id;
		}
		if(section){
			this.section = section;
		}
		if(pageListElement){
			this.pageListElement = pageListElement;
		}
		if(pagingFunction){
			this.pagingFunction = pagingFunction;
		}else{
			this.pagingFunction = null;
		}

		// store a global reference
        Paging.pagings[this.id] = this;
	},

	/**
	 * Set isImageButton
	 * @param {Object} bool
	 */
	setIsImageButton: function(bool){
		this.isImageButton = bool;
	},

	/**
	 * Set id
	 * @param {String} id
	 */
	setId:function(id){
		this.id = id;
	},

	/**
	 * Set section
	 * @param {String} section
	 */
	setSection:function(section){
		this.section = section;
	},

	/**
	 * Set pageScale
	 * @param {int} pageScale
	 */
	setPageScale:function(pageScale){
		this.pageScale = pageScale;
	},

	/**
	 * Set listScale
	 * @param {int} listScale
	 */
	setListScale:function(listScale){
		this.listScale = listScale;
	},

	/**
	 * Set pagingFunction
	 * @param {function} pagingFunction
	 */
	setPagingFunction:function(pagingFunction){
		this.pagingFunction = pagingFunction;
	},

	/**
	 * Generate Paging HTML
	 * @param {int} currentPage
	 * @param {int} totalPageCnt
	 * @param {int} totalCnt
	 */
	generatePaging:function(currentPage, totalPageCnt, totalCnt) {
		var startPage	= 0;
		if( isNull(totalCnt) || Number(totalCnt) == 0 ) {
			startPage = 0;
		} else {
			startPage = parseInt((currentPage-1) - (currentPage-1)%this.pageScale + 1);
		}
		var endPage		= totalPageCnt;

		var innerTextStr = '';
		innerTextStr += "<table border='0' cellspacing='0' cellpadding='0' id='"+this.id+"'>";
		innerTextStr += '<tr>';
		innerTextStr += "<td>";

		innerTextStr += "<ul class='paging'>";

		/*	First 버튼 생성	*/
		if( startPage > 0 ) {
			innerTextStr += "<li>";

			innerTextStr += "<a href='javascript:Paging.pagingFunc(\""+this.id+"\",1)'>";
			if(this.isImageButton == true){
				innerTextStr += "<img src='"+Paging.config.imageroot+"/paging/btn_first.gif'>";
			}else{
				innerTextStr += this.defaultImgGoFirst;
			}
			innerTextStr += "</a>&nbsp;";

			innerTextStr += "</li>";
		}
		/*	Prev 버튼 생성	*/
		if(startPage > this.pageScale) {
			innerTextStr += "<li>";

			innerTextStr += "<a href='javascript:Paging.pagingFunc(\""+this.id+"\","+parseInt(startPage-1)+")'>";
			if (this.isImageButton == true){
				innerTextStr += "<img src='"+Paging.config.imageroot + "/paging/btn_prev.gif'>";
			}else{
				innerTextStr += this.defaultImgGoFirst;
			}
			innerTextStr += "</a>&nbsp;";
			innerTextStr += "</li>";
		}


		/*	페이지 번호 생성	*/
		innerTextStr += "<li>";

		var idx = 0;
		for(idx=0; idx<this.pageScale; idx++) {
			if( startPage == 0 ) {
				innerTextStr += currentPage;
				break;
			}
			if( (currentPage == parseInt(startPage+idx)) ) {
				innerTextStr += "<a class='on' href='javascript:void(0);'>"+currentPage+"</a>";
			} else {
				if ((endPage >= startPage + idx) && (startPage > 0)) {
					innerTextStr += "<a class='num' href='javascript:Paging.pagingFunc(\"" + this.id + "\"," + parseInt(startPage + idx) + ")'>" + parseInt(startPage + idx) + "</a>";
				}else {
					break;
				}
			}
			if(idx !=9 && parseInt(startPage+idx) != endPage){
				innerTextStr += '&nbsp;' + this.section + '&nbsp;';
			} else {
				innerTextStr += '&nbsp;';
			}
		}
		innerTextStr += '</li>';

		/*	Next 버튼 생성	*/
		if( (endPage > parseInt(startPage+idx-1)) && (endPage > 0) ){
			innerTextStr += "<li>";
			innerTextStr += "&nbsp;<a href='javascript:Paging.pagingFunc(\""+this.id+"\","+parseInt(startPage+idx)+")'>";
			if (this.isImageButton == true) {
				innerTextStr += "<img src='"+Paging.config.imageroot + "/paging/btn_next.gif'>";
			}else{
				innerTextStr += this.defaultImgGoNext;
			}
			innerTextStr += "</a>&nbsp;";
			innerTextStr += "</li>";
		}
		/*	Last 버튼 생성	*/
		if( endPage > 0 ) {
			innerTextStr += "<li>";
			innerTextStr += "<a href='javascript:Paging.pagingFunc(\"" + this.id + "\"," + endPage + ")'>";
			if (this.isImageButton == true) {
				innerTextStr += "<img src='"+Paging.config.imageroot + "/paging/btn_last.gif'>";
			}else{
				innerTextStr += this.defaultImgGoLast;
			}
			innerTextStr += "</a>";
			innerTextStr += "</li>";
		}

		innerTextStr += "</ul>";

		innerTextStr += '</td>';
		innerTextStr += '</tr>';
		innerTextStr += '</table>';

		$(this.pageListElement).update(innerTextStr);
	}
};

/**
 * Global cache of Paging instances
 * @property Paging.pagings
 * @type Array
 * @static
 * @private
 */
Paging.pagings = [];

/**
 * Get Paging Object
 * @param {Object} id
 * @returns Paging Object
 */
Paging.getPagingObject = function(id){
	return Paging.pagings[id];
}

/**
 * invoke move page function
 * @param {Object} id
 * @param {Object} pageIndex
 */
Paging.pagingFunc = function(id, pageIndex){
	var pg = Paging.getPagingObject(id);
	if(pg.pagingFunction){
		pg.pagingFunction(pageIndex);
	}else{
		goPage(pageIndex);
	}
}