﻿/*
 * jQuery UI Progressbar 1.7.1
 *
 * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Progressbar
 *
 * Depends:
 *   ui.core.js
 */
(function($) {

$.widget("ui.pager", {

	_init: function() {
		this.element.addClass(this._getData("pagerCN"));
	},

	destroy: function() {		
		$.widget.prototype.destroy.apply(this, arguments);
	},
	
	setPageNo: function(pn){
		this._setData("pageNo",pn);
	},
	
	setPageSize: function(pn){
		this._setData("pageSize",pn);
	},
	
	getPageNo: function(){
		return this._getData("pageNo");
	},
	
	getPageSize: function(){
		return this._getData("pageSize");
	},
	
	setButtonType: function(bt){
		this._setData("buttonType",bt);
	},	
	
	render:function(cntTotal){				
		this._setData("cntTotal", cntTotal);
		this._refresh();		
	},
	
	_refresh:function(){
		this.element.empty();
		
		var buttonSet = {};
		
		switch (this._getData("buttonType")){
			case 1 : 
				buttonSet = this._getData("buttonSet1");
				break;
			case 2 : 
				buttonSet = this._getData("buttonSet2");
				break;
		}
		
		var pageNo = this._getData("pageNo");
		var pagerSize = this._getData("pagerSize");
		var pageSize = this._getData("pageSize");
		var pageCount = parseInt((this._getData("cntTotal") - 1) / pageSize) + 1;
		
		var start = parseInt((pageNo - 1) / pagerSize) * pagerSize + 1;
		var end = parseInt((pageNo - 1) / pagerSize + 1) * pagerSize;
		
    if (end > pageCount)
		{
			end = pageCount;
		}
		
		if (start > pagerSize)
		{                
			var $firstBtn = $('<a href="#"/>').addClass(this._getData("firstBtnCN")).css("cursor", "pointer")
				.click(function(){
					$(this).parent().data("pager")._setData("pageNo", 1);
					$(this).parent().data("pager")._getData("change")(1);
					return false;
				});
			var $prevBtn = $('<a href="#" />').addClass(this._getData("prevBtnCN")).css("cursor", "pointer")
				.click(function(){
					$(this).parent().data("pager")._setData("pageNo", start-1);
					$(this).parent().data("pager")._getData("change")(start-1);
					return false;
				});
				
			if(this._getData("buttonType") != 0){		
				$firstBtn.append($('<img />').attr(buttonSet.firstBtnAttr));
				$prevBtn.append($('<img />').attr(buttonSet.prevBtnAttr));
			}
			else{
				$firstBtn.append(this._getData("firstBtnText"));
				$prevBtn.append(this._getData("prevBtnText"));
			}
			
			$firstBtn.appendTo(this.element);
			$prevBtn.appendTo(this.element);
		}

		if (start > end)
		{
			//페이징 할 로우가 없을 경우
		}
		else
		{	
			for (var i = start; i <= end; i++) {
				//no = this._getData("prevPageNo") + i + 1;
				var btn = $('<a href="#" />').css("cursor", "pointer").attr('pageNo', i).text(i)
					.click(function(){
						$(this).parent().data("pager")._setData("pageNo", $(this).attr("pageNo"));
						var pageNo = $(this).parent().data("pager")._getData("pageNo");
						$(this).parent().data("pager")._getData("change")(pageNo);
						return false;
					});
				if (this._getData("pageNo") == i) {
						btn.addClass(this._getData("currentBtnCN"));
				}
				else {
						btn.addClass(this._getData("otherBtnCN"));
				}
				      
				this.element.append(btn);
				
				if (i < end ){
					this.element.append($('<img />').attr(buttonSet.altBarAttr));					
				}  
			}
    }

		if (pageCount > end)
		{
			var $nextBtn =  $('<a />').addClass(this._getData("nextBtnCN")).css("cursor", "pointer")	
				.click(function(){
					$(this).parent().data("pager")._setData("pageNo", end+1);
					$(this).parent().data("pager")._getData("change")(end+1);
					return false;
				});						
			var $lastBtn = $('<a />').addClass(this._getData("lastBtnCN")).css("cursor", "pointer")
				.click(function(){
					$(this).parent().data("pager")._setData("pageNo", pageCount);
					$(this).parent().data("pager")._getData("change")(pageCount);
					return false;
				});
				
			if(this._getData("buttonType") != 0){		
				$nextBtn.append($('<img />').attr(buttonSet.nextBtnAttr));
				$lastBtn.append($('<img />').attr(buttonSet.lastBtnAttr));
			}
			else{
				$nextBtn.append(this._getData("nextBtnText"));
				$lastBtn.append(this._getData("lastBtnText"));
			}
			$nextBtn.appendTo(this.element);
			$lastBtn.appendTo(this.element);
		}
	}
});
//$.ui.pager
$.extend($.ui.pager, {
	version: "1.7.1",
	getter:"getPageNo, getPageSize",
	
	defaults: {		
		buttonType: 1,
			
		pagerSize: 10,
    pageSize: 10,
    pageNo: 1,
    //resultSet:{},   
    cntTotal:0,
    change:null,

    //Style
    pagerCN: "pageSec postVPage",
    currentBtnCN: "currentPage",
    otherBtnCN: "otherPage",
    firstBtnCN: "firstPage",
    prevBtnCN: "prePage",
    nextBtnCN: "nextPage",
    lastBtnCN: "lastPage",
    firstBtnText: "<<",
    prevBtnText: "<",
    nextBtnText: ">",
    lastBtnText: ">>",
    useTextBtn: false,

    //Image Attribute
    buttonSet1:{
			firstBtnAttr: {
					src: "http://nbimg.nexon.com/Blog/button/bt_first7.gif",
					width: 16,
					height: 14,
					alt: "처음"
			},
			prevBtnAttr: {
					src: "http://nbimg.nexon.com/Blog/button/bt_prev7.gif",
					width: 16,
					height: 14,
					alt: "이전"
			},
			nextBtnAttr: {
					src: "http://nbimg.nexon.com/Blog/button/bt_next7.gif",
					width: 16,
					height: 14,
					alt: "다음"
			},
			lastBtnAttr: {
					src: "http://nbimg.nexon.com/Blog/button/bt_last7.gif",
					width: 16,
					height: 14,
					alt: "끝"
			},
			altBarAttr: {
					src: "http://nbimg.nexon.com/Blog/global/bar_page.gif",
					width: 1,
					height: 11
			}
    },
    buttonSet2:{
			firstBtnAttr: {
				src: "http://nbimg.nexon.com/Blog/button/bt_first8.gif",
				width: 12,
				height: 11,
				alt: "first"
			},
			prevBtnAttr: {
				src: "http://nbimg.nexon.com/Blog/button/bt_prev8.gif",
				width: 8,
				height: 11,
				alt: "prev"
			},
			nextBtnAttr: {
				src: "http://nbimg.nexon.com/Blog/button/bt_next8.gif",
				width: 8,
				height: 11,
				alt: "next"
			},
			lastBtnAttr: {
				src: "http://nbimg.nexon.com/Blog/button/bt_last8.gif",
				width: 12,
				height: 11,
				alt: "last"
			},
			altBarAttr: {
				src: "http://nbimg.nexon.com/Blog/global/bar_page.gif",
				width: 1,
				height: 11
			}
    }
	}
});

})(jQuery);
