//Please inclued common.js in your page,this class need reg onControlEvent on turn page linkbutton.
class TurnPage
{
	var _pageDiv = null;
    var _pageHtml = 
    {
        "firstPage": "<a class='prev disabled' href='#' onclick='pageObj.turnPage(-1)'></a><a class='next' href='#' onclick='pageObj.turnPage(1)'></a>",
        "middlePage": "<a class='prev' href='#' onclick='pageObj.turnPage(-1)'></a><a class='next' href='#' onclick='pageObj.turnPage(1)'></a>",
        "lastPage": "<a class='prev' href='#' onclick='pageObj.turnPage(-1)'></a><a class='next disabled' href='#' onclick='pageObj.turnPage(1)'></a>"
    };
    var data = [];
    var pageSize = 7;
    var _totalPage = 1;
    var _currentPage = 1;
    
    //function this(){}
	
    function init(data, divClass, instanceName)
    {
        instanceName = instanceName || "pageObj";
		this._pageHtml["firstPage"] = this._pageHtml["firstPage"].replace("pageObj", instanceName);
		this._pageHtml["middlePage"] = this._pageHtml["middlePage"].replace("pageObj", instanceName);
		this._pageHtml["lastPage"] = this._pageHtml["lastPage"].replace("pageObj", instanceName);
		
		divClass = divClass || "pageNav";
        this._pageDiv = $("." + divClass);
        this.data = data;
        this._totalPage = Math.ceil(this.data.length.toFloat() / this.pageSize.toFloat());
        this.initCallback && this.initCallback(this._totalPage);
    }
    
    //Page number is 1 or -1
    function turnPage(page)
    {
        this._currentPage += page;
        if (this._currentPage < 1) 
            this._currentPage = 1;
        if (this._currentPage > this._totalPage) 
            this._currentPage = this._totalPage;
        
        var startIndex = Integer.max(0, this._currentPage - 1) * this.pageSize;
        var endIndex = startIndex + this.pageSize;
        var pageData = this.data.slice(startIndex, endIndex);
        
        this.turnPageCallback && this.turnPageCallback(this._currentPage, pageData);
    }
    
    function _initCallback(totalCount)
    {
        if (totalCount > 1) 
            this._pageDiv.html = this._pageHtml["firstPage"];
    }
    function _turnPageCallback(page, pageData)
    {
        if (page == 1) 
            this._pageDiv.html = this._pageHtml["firstPage"];
        else if (page == this._totalPage) 
            this._pageDiv.html = this._pageHtml["lastPage"];
        else 
            this._pageDiv.html = this._pageHtml["middlePage"];
    }
    
    //You can override the following callback function
    function initCallback(totalCount)
    {
        this._initCallback(totalCount);
    }
    function turnPageCallback(page, pageData)
    {
        this._turnPageCallback(page, pageData);
    }
}

class TurnPage2
{
    var data = [];
    var pageSize = 8;
    var menuSize = 5;
    var actionType = 1; //1 left -> right, -1 right -> left
    var showCount = false;
    var _pageDiv = null;
    var _totalPage = 1;
    var _currentPage = 1;
    var _action = ""; // prev or next
    var _lastSelectPage = 0;
    var _instanceName = "";
    
    function init(data, elemBtnBox, instanceName)
    {
        this.data = data;
        this._pageDiv = elemBtnBox;
        this._totalPage = Math.ceil(this.data.length.toFloat() / this.pageSize.toFloat());
        this._currentPage = this.actionType == 1 ? 1 : this._totalPage;
        this._instanceName = instanceName;
        
        if (this._totalPage > 0) 
        {
            var html = "";
            
            html += "<div class='pageBox'>";
            html += "<span><a class='firstPage' href='#' onclick='" + instanceName + ".firstPage()'></a><a class='prevPage' href='#' onclick='" + instanceName + ".turnPage(-1)'></a>";
            html += "<em></em>";
            html += "<a class='nextPage' href='#' onclick='" + instanceName + ".turnPage(1)'></a><a class='lastPage' href='#' onclick='" + instanceName + ".lastPage()'></a></span>";
            var countStyle = this.showCount ? "" : " style='display:none' ";
            html += "<strong" + countStyle + ">" + strFormat(lang["page"], "<font>" + this._currentPage + "</font>", this._totalPage) + "</strong>";
            html += "</div>";
            
            this._pageDiv.html = html;
            this.go(this._currentPage);
        }
		else
		{
			this._pageDiv.html = "&nbsp;";
		}
    }
    
    function go(page)
    {
        this._currentPage = page;
        if (this._currentPage < 1) 
            this._currentPage = 1;
        if (this._currentPage > this._totalPage) 
            this._currentPage = this._totalPage;
        
        if (this._currentPage == this._lastSelectPage) 
        {
            print("Not change");
            return false;
        }
        this._lastSelectPage = page;
        
        var startIndex = Integer.max(0, this._currentPage - 1) * this.pageSize;
        var endIndex = startIndex + this.pageSize;
        var pageData = this.data.slice(startIndex, endIndex);
        
        this._pageDiv.$("font").text = this._currentPage;
        
        var imgBtns = this._pageDiv.$$("a.firstPage, a.prevPage, a.nextPage, a.lastPage");
        for (var i = 0; i < imgBtns.length; i++) 
        {
            var obj = imgBtns[i];
            obj.attributes.removeClass("disabled");
            if (this._currentPage == 1) 
            {
                if (obj.attributes["class"].indexOf("firstPage") != -1 || obj.attributes["class"].indexOf("prevPage") != -1) 
                    obj.attributes.addClass("disabled");
            }
            if (this._currentPage == this._totalPage) 
            {
                if (obj.attributes["class"].indexOf("nextPage") != -1 || obj.attributes["class"].indexOf("lastPage") != -1) 
                    obj.attributes.addClass("disabled");
            }
        }
        
        var forIndex1 = 0;
        var forIndex2 = 0;
        if (this._totalPage > this.menuSize) 
        {
            if (this._currentPage == 1 || this._currentPage == this._totalPage) 
            {
                if (this._currentPage == 1) 
                {
                    if (this.actionType == 1) 
                    {
                        forIndex1 = 1;
                        forIndex2 = this.menuSize;
                    }
                    else 
                    {
                        forIndex1 = 1;
                        forIndex2 = this._totalPage % this.menuSize == 0 ? this.menuSize : this._totalPage % this.menuSize;
                    }
                }
                else 
                {
                    if (this.actionType == 1) 
                    {
                        forIndex1 = this._totalPage % this.menuSize == 0 ? this._totalPage - this.menuSize + 1 : this._totalPage - this._totalPage % this.menuSize + 1;
                        forIndex2 = this._totalPage;
                    }
                    else 
                    {
                        forIndex1 = this._totalPage - this.menuSize + 1;
                        forIndex2 = this._totalPage;
                    }
                }
            }
            else 
            {
                var yu = this._currentPage % this.menuSize;
                if (this._action == "next") 
                {
                    if ((this.actionType == 1 && yu == 1) || (this.actionType == -1 && yu == this._totalPage % this.menuSize + 1)) 
                    {
                        forIndex1 = this._currentPage;
                        forIndex2 = Integer.min(this._currentPage + this.menuSize - 1, this._totalPage);
                    }
                }
                else 
                {
                    if ((this.actionType == 1 && yu == 0) || (this.actionType == -1 && yu == this._totalPage % this.menuSize)) 
                    {
                        forIndex1 = Integer.max(this._currentPage - this.menuSize + 1, 1);
                        forIndex2 = this._currentPage;
                    }
                }
            }
        }
        else 
        {
            forIndex1 = 1;
            forIndex2 = this._totalPage;
        }
        
        if (forIndex1 && forIndex2) 
        {
            var linksHtml = "";
            for (var i = forIndex1; i <= forIndex2; i++) 
                linksHtml += "<a href='#' onclick='" + this._instanceName + ".go(" + i + ")'>" + i + "</a>";
            this._pageDiv.$("em").html = linksHtml;
        }
        
        var numberLinks = this._pageDiv.$$("em a");
        for (var i = 0; i < numberLinks.length; i++) 
        {
            numberLinks[i].attributes.removeClass("current");
            if (numberLinks[i].text.toNumber() == this._currentPage) 
                numberLinks[i].attributes.addClass("current");
        }
        
        this.goCallback(this._currentPage, pageData);
    }
    
    function goCallback(page, pageData)
    {
    
    }
    
    function turnPage(step)
    {
        if (step == 1) 
        {
            this._action = "next";
            this.go(++this._currentPage);
        }
        else if (step == -1) 
        {
            this._action = "prev";
            this.go(--this._currentPage);
        }
    }
    
    function firstPage()
    {
        this._action = "prev";
        this.go(1);
    }
    
    function lastPage()
    {
        this._action = "next";
        this.go(this._totalPage);
    }
}