﻿
/******************************************************************************
*   Author:             Dada
*   E-Mail:             dada_dotnet@hotmail.com
*   Create Time:        2008.01.16.1031
*   Last Modify Time:   2008.02.21.1119
*   Version:            v1.0
*   Summary:            System.UI.PagingBar.js
*                       http://www.iamdada.com/jsfx/system.ui.pagingbar
******************************************************************************/

// Namespace
var System; if (!System) System = {};
if(!System.UI) System.UI = {};

// Class
System.UI.PagingBar = function()
{
    // Private Variable
    var copyright = 'http://www.iamdada.com';                                   // 版权信息
    var version = 'v1.0';                                                       // 版本信息
    
    var self = this;                                                            // 
    var currentPageLevel = 0;                                                   // 
    var prevPageLevel = 0;                                                      // 
    var maxPageLevel = 0;                                                       // 
    
    // Public Variable
    this.Id = 'SystemUIPager_' + new Date().getTime();                          // 控件ID
    this.CurrentPageIndex = 1;                                                  // 当前页码
    this.PrevPageIndex = 0;                                                     // 
    this.RecordTotal = 0;                                                       // 总记录数
    this.RecordShowCount = 10;                                                  // 每页显示记录数
    this.PageTotal = 0;                                                         // 页码总数
    this.PageShowCount = 10;                                                    // 显示页数
    this.IsAutoComplete = true;                                                 // 
    this.IsEnabled = true;                                                      // 是否禁止控件
    
    this.IsUrlPaging = false;                                                   // 是否Url分页
    this.ParamPage = 'page';                                                    // 分页参数
    this.ParamRecord = 'num';                                                   // 显示记录数参数
    
    this.ChangeMode = 'None';                                                   // None, Auto, Random
    this.AutoChangeDelay = 1000;                                                // 自动变换延迟时间（毫秒）
    
    this.CssClass = 'system_ui_pager';                                          // CSS样式
    this.PageItemContainerCssClass = 'pager_item_container';                    // 
    this.FirstPageCssClass = 'pager_firstpage';                                 // 
    this.PrevPageCssClass = 'pager_prevpage';                                   // 
    this.PrevLevelCssClass = 'pager_prevlevel';                                 // 
    this.PageItemCssClass = 'pager_item';                                       // 页码CSS样式
    this.NextLevelCssClass = 'pager_nextlevel';                                 // 
    this.NextPageCssClass = 'pager_nextpage';                                   // 
    this.LastPageCssClass = 'pager_lastpage';                                   // 
    this.PageAlternatingItemCssClass = 'pager_alternatingitem';                 // 偶数页码CSS样式
    this.CurrentPageCssClass = 'pager_currentpage';                             // 当前页码CSS样式
    this.PageItemDisabledCssClass = 'pager_item_disabled';                      // 禁用页码CSS样式
    this.InputCssClass = 'pager_input';                                         // 输入框页码CSS样式
    this.SubmitCssClass = 'pager_submit';                                       // 按扭CSS样式
    this.SelectCssClass = 'pager_select';                                       // 下拉菜单CSS样式
    this.MessageCssClass = 'pager_message_container';                           // 
    this.MessageCurrentPageIndexCssClass = 'pager_message_currentpageindex';    // 
    this.MessagePageTotalCssClass = 'pager_message_pagetotal';                  // 
    this.MessageRecordShowCountCssClass = 'pager_message_recordshowcount';      // 
    this.MessageRecordTotalCssClass = 'pager_message_recordtotal';              // 
    
    this.IsShowFirstPage = true;                                                // <<
	this.IsShowPrevPage = true;                                                 // <
	this.IsShowPrevLevel = true;                                                // ..
	this.IsShowPageItems = true;                                                // 1 2 3 ...
	this.IsShowNextLevel = true;                                                // ..
	this.IsShowNextPage = true;                                                 // >
    this.IsShowLastPage = true;                                                 // >>
    this.IsShowMessage = true;                                                  // Message
    this.IsShowInput = true;                                                    // Input
    this.IsShowSubmit = true;                                                   // Submit
    this.IsShowSelect = false;                                                  // Select
    
    this.FirstPageTextFormat = '&lt;&lt;';                                      // <<
	this.PrevPageTextFormat = '&lt;';                                           // <
	this.PrevLevelTextFormat = '..';                                            // ..
	this.PageItemTextFormat = '{0}';                                            // 页码项Text显示格式
	this.PageAlternatingItemTextFormat = '';                                    // 页码项Text显示格式
	this.NextLevelTextFormat = '..';                                            // ..
	this.NextPageTextFormat = '&gt;';                                           // >
    this.LastPageTextFormat = '&gt;&gt;';                                       // >>
    this.SubmitText = 'Go';                                                     // Go
    
    this.PageItemAltFormat = '转到第[{0}]页';                                   // 页码项Alt显示格式
    this.PageAlternatingItemAltFormat = '';                                     // 
    this.MessageFormatHtml = '页次: {CurrentPageIndex}/{PageTotal}页';          // {CurrentPageIndex}, {PageTotal}, {RecordShowCount}, {RecordTotal}
    
    this.FirstPageTemplate = '<td>{0}</td>';                                             // 最前页模板
    this.PrevPageTemplate = '<td>{0}</td>';                                              // 上一页模板
    this.PrevLevelTemplate = '<td>{0}</td>';                                             // 上一层模板
    this.PageItemTemplate = '<td>{0}</td>';                                              // 页码项模板
    this.PageAlternatingItemTemplate = '';                                      // 偶数页码模板
    this.NextLevelTemplate = '<td>{0}</td>';                                             // 下一层模板
    this.NextPageTemplate = '<td>{0}</td>';                                              // 下一页模板
    this.LastPageTemplate = '<td>{0}</td>';                                              // 最后页模板
    this.HeaderTemplate = '<table class="tb_pager_item" cellspacing="0" cellpadding="0"><tr>';                                                   // 
    this.FooterTemplate = '</tr></table>';                                                   // 
    this.PageItemHeaderTemplate = '';                                           // 
    this.PageItemSeparatorTemplate = '';                                        // 
    this.PageItemFooterTemplate = '';                                           // 
    
    // Event
    this.OnLoadHandler = null;                                                  // 
    this.OnClickHandler = null;                                                 // 
    this.OnMouseOverHandler = null;                                             // 
    this.OnErrorHandler = function(e) {alert(e);};                              // 默认对错误信息进行alert处理
    
    // 
    this.Create = function(containerId)
    {
        // 
        if(this.IsUrlPaging == true)
        {
            var pageIndex = parseInt(this.getQueryString(this.ParamPage));
            if(pageIndex > 0)
                this.SetCurrentPageIndex(pageIndex);
        }
        
        // create pages container
        var controlHTML = '<div id="' + this.Id + '" class="' + this.CssClass + '">';
        if(this.IsShowMessage)
        {
            // Pager Message Container
            controlHTML += '<div id="' + this.Id + '_MessageContainer" class="' + this.MessageCssClass + '"></div>';
        }
        // Page Items Container
        controlHTML += '<div id="' + this.Id + '_PageItemContainer" class="' + this.PageItemContainerCssClass + '"></div>';
        controlHTML += '</div>';
        
        // 
        if(typeof(containerId)=='string' && containerId!='')
        {
            // create the pager to container
            document.getElementById(containerId).innerHTML = controlHTML;
        }
        else
        {
            // write the pager to document.
            document.write(controlHTML);
        }
        
        // 
        if(typeof(this.OnLoadHandler) == 'function')
        {
            this.OnLoadHandler(this);
        }
        
        // rebuild
        if(this.IsAutoComplete)
        {
            this.BuildByPageIndex(this.CurrentPageIndex);
        }
    };
    
    // 
    this.BuildByPageIndex = function(pageIndex)
    {
        this.SetCurrentPageIndex(pageIndex);
        this.Build();
    }
    
    // 
    this.SetCurrentPageIndex = function(pageIndex)
    {
        // set current page index
        this.PrevPageIndex = this.CurrentPageIndex;
        this.CurrentPageIndex = pageIndex;
    };
    
    // 
    this.Build = function()
    {
        // 计算页码总数
        if(this.PageTotal<=0 && this.RecordTotal>0)
        {
            this.PageTotal = parseInt(this.RecordTotal/this.RecordShowCount);
            if(this.RecordTotal%this.RecordShowCount != 0)
            {
                this.PageTotal += 1;
            }
        }
        
        if(this.PageTotal > 1) // 总页数2页以上才显示
        {
            if(this.CurrentPageIndex>0 && this.CurrentPageIndex<=this.PageTotal)
            {
                // 计算最大层数
                maxPageLevel = parseInt(this.PageTotal/this.PageShowCount) + 1;
                if(this.PageTotal%this.PageShowCount == 0) {maxPageLevel -= 1;}
                
                // 计算当前层数
                prevPageLevel = currentPageLevel;
                currentPageLevel = parseInt(this.CurrentPageIndex/this.PageShowCount) + 1;
                if(this.CurrentPageIndex%this.PageShowCount == 0) {currentPageLevel -= 1;}
                                
                // rebuild pager items
//                if(currentPageLevel != prevPageLevel || prevPageLevel <= 0)
                {
                    document.getElementById(this.Id + '_PageItemContainer').innerHTML = this.RenderPageItemsHTML();
                }
                this.InitPageItems();
                
                // 
                if(this.IsShowMessage)
                {
                    var messageFormatHtml = this.MessageFormatHtml;
                    messageFormatHtml = messageFormatHtml.replace('{CurrentPageIndex}', '<span id="' + this.Id + '_Message_CurrentPageIndex" class="' + this.MessageCurrentPageIndexCssClass + '">' + this.CurrentPageIndex + '</span>');
                    messageFormatHtml = messageFormatHtml.replace('{PageTotal}', '<span id="' + this.Id + '_Message_PageTotal" class="' + this.MessagePageTotalCssClass + '">' + this.PageTotal + '</span>');
                    messageFormatHtml = messageFormatHtml.replace('{RecordShowCount}', '<span id="' + this.Id + '_Message_RecordShowCount" class="' + this.MessageRecordShowCountCssClass + '">' + this.RecordShowCount + '</span>');
                    messageFormatHtml = messageFormatHtml.replace('{RecordTotal}', '<span id="' + this.Id + '_Message_RecordTotal" class="' + this.MessageRecordTotalCssClass + '">' + this.RecordTotal + '</span>');
                    document.getElementById(this.Id + '_MessageContainer').innerHTML = messageFormatHtml;
                }
                
                // set currentPageIndex to input
                if(this.IsShowInput)
                {
                    document.getElementById(this.Id + '_txtInput').value = this.CurrentPageIndex;
                }
            }
            else
            {
                // 
                if(typeof(this.OnErrorHandler) == 'function')
                {
                    this.OnErrorHandler('请输入有效范围页码！');
                }
                
                // 
                if(this.IsShowInput)
                {
                    document.getElementById(this.Id + '_txtInput').select();
                }
            }
            document.getElementById(this.Id).style.display = '';
        }
        else
        {
            document.getElementById(this.Id).style.display = 'none';
        }
    };
    
    // Page Items; This can be overwirte
    this.RenderPageItemsHTML = function()
    {        
        var pageItemsHTML = '';
        
        // 
        if(this.CurrentPageIndex>0 && this.PageTotal>=0 && this.PageShowCount>0 && this.CurrentPageIndex<=this.PageTotal)
        {
            // 
            var id = '';
            var pageIndex = 0;
            
            // Header
            pageItemsHTML += this.HeaderTemplate;
            
            // FirstPage item
            if(this.IsShowFirstPage)
            {                
                id = this.Id + '_PageItem_FirstPage';
                pageIndex = 1;
                pageItemsHTML += this.FirstPageTemplate.replace('{0}', '<a id="' + id + '">' + this.FirstPageTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // PrevPage Item
            if(this.IsShowPrevPage)
            {
                id = this.Id + '_PageItem_PrevPage';
                pageIndex = this.CurrentPageIndex - 1;
                pageItemsHTML += this.PrevPageTemplate.replace('{0}', '<a id="' + id + '">' + this.PrevPageTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // PrivLevel item
            if(this.IsShowPrevLevel)
            {
                id = this.Id + '_PageItem_PrevLevel';
                pageIndex = (currentPageLevel-1) * this.PageShowCount;
                pageItemsHTML += this.PrevLevelTemplate.replace('{0}', '<a id="' + id + '">' + this.PrevLevelTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // PageItem Header
            pageItemsHTML += this.PageItemHeaderTemplate;
            
            // Page items
            if(this.IsShowPageItems)
            {
                var itemHtml = '';
                for(var i=1; i<=this.PageShowCount; i++)
                {
                    // 页码 = 显示页数 * 当前层级 + i
                    pageIndex = this.PageShowCount * (currentPageLevel-1) + i;
                    if(pageIndex > 0 && pageIndex <= this.PageTotal)
                    {
                        id = this.Id + '_PageItem_Item' + pageIndex;
                        
                        // ItemTextFormat
                        if(typeof(pageIndex%2==0 && this.PageAlternatingItemTextFormat)=='string' && this.PageAlternatingItemTextFormat!='')
                        {
                            itemHtml = '<a id="'+ id + '">' + this.PageAlternatingItemTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>';
                        }
                        else
                        {
                            itemHtml = '<a id="'+ id + '">' + this.PageItemTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>';
                        }
                        
                        // ItemTemplate
                        if(pageIndex%2 == 0 && typeof(this.PageAlternatingItemTemplate) == 'string' && this.PageAlternatingItemTemplate != '')
                        {
                            pageItemsHTML += this.PageAlternatingItemTemplate.replace('{0}', itemHtml);
                        }
                        else
                        {
                            pageItemsHTML += this.PageItemTemplate.replace('{0}', itemHtml);
                        }
                        
                        // PageItemSeparator
                        if(i < this.PageShowCount && pageIndex < this.PageTotal)
                        {
                            pageItemsHTML += this.PageItemSeparatorTemplate;
                        }
                    }
                }
            }
            
            // PageItem Footer
            pageItemsHTML += this.PageItemFooterTemplate;
            
            // NextLevel item
            if(this.IsShowNextLevel)
            {
                id = this.Id + '_PageItem_NextLevel';
                pageIndex = currentPageLevel * this.PageShowCount + 1;
                pageItemsHTML += this.NextLevelTemplate.replace('{0}', '<a id="' + id + '">' + this.NextLevelTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // NextPage item
            if(this.IsShowNextPage)
            {
                id = this.Id + '_PageItem_NextPage';
                pageIndex = this.CurrentPageIndex + 1;
                pageItemsHTML += this.NextPageTemplate.replace('{0}', '<a id="' + id + '">' + this.NextPageTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // LastPage item
            if(this.IsShowLastPage)
            {
                id = this.Id + '_PageItem_LastPage';
                pageIndex = this.PageTotal;
                pageItemsHTML += this.LastPageTemplate.replace('{0}', '<a id="' + id +'">' + this.LastPageTextFormat.replace('{0}', this.formatPageItemText(pageIndex)) + '</a>');
            }
            
            // Footer
            pageItemsHTML += this.FooterTemplate;
            
            // 
            if(this.IsShowInput)
            {
                // Input
                pageItemsHTML += '<input id="' + this.Id + '_txtInput" type="text" class="' + this.InputCssClass + '" />';
                
                // Submit
                if(this.IsShowSubmit)
                {
                    pageItemsHTML += '<input id="' + this.Id + '_txtSubmit" type="button" value="' + this.SubmitText + '" class="' + this.SubmitCssClass + '" />';
                }
                
                // Bug fix
                pageItemsHTML += '<input type="text" style="display:none;" />';
            }
            
            // Select
            if(this.IsShowSelect)
            {
                pageItemsHTML += '<select id="' + this.Id + '_ddlSelect" class="' + this.SelectCssClass + '">';
                for(var i=1; i<=this.PageTotal; i++)
                {
                    pageItemsHTML += '<option value="' + i + '">' + this.formatPageItemText(i) + '</option>';
                }
                pageItemsHTML += '</select>';
            }
        }
        else
        {
            var helpHTML = '<ul>';
            helpHTML += '<li>分页控件参数设置错误！</li>';
            helpHTML += '<li>Copyright：<a href="http://www.iamdada.com">IamDada.Com</a> &copy;2008</li>';
            helpHTML += '<li>Version：v1.0</li>';
            helpHTML += '</ul>';
            pageItemsHTML = helpHTML;
        }
        
        return pageItemsHTML;
    };
    
    // Init page items
    this.InitPageItems = function()
    {
        // 
        var href = 'javascript:void(0);';
        var index = location.href.indexOf('?');
        var queryString = '';
        if(index > 0)
        {
            index += 1;
            queryString = location.href.substr(index);            
            var reg = new RegExp("(^|\\?|&)"+ this.ParamPage +"=([^&]*)(\\s|&|$)", "i");
            if (reg.test(location.href))
                queryString = queryString.replace(this.ParamPage + '=' + RegExp.$2.replace(/\+/g, " "), '');
            var reg = new RegExp("(^|\\?|&)"+ this.ParamRecord +"=([^&]*)(\\s|&|$)", "i");
            if (reg.test(location.href))
                queryString = queryString.replace(this.ParamRecord + '=' + RegExp.$2.replace(/\+/g, " "), '');
            queryString = this.trim(queryString, '&');
        }
        
        // Page items
        var objPager = document.getElementById(this.Id + '_PageItemContainer');
        items = objPager.getElementsByTagName('a');
        
        for(var i=0; i<items.length; i++)
        {
            // First Page
            id = this.Id + '_PageItem_FirstPage';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass + ' ' + this.FirstPageCssClass;
                if(this.CurrentPageIndex > 1)
                {
                    items[i].pageIndex = 1;
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
            
            // Prev Page
            id = this.Id + '_PageItem_PrevPage';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass + ' ' + this.PrevPageCssClass;
                if(this.CurrentPageIndex > 1)
                {
                    items[i].pageIndex = this.CurrentPageIndex - 1;
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
            
            // Prev Level
            id = this.Id + '_PageItem_PrevLevel';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass +  ' ' + this.PrevLevelCssClass;
                if(currentPageLevel > 1)
                {
                    items[i].pageIndex = (currentPageLevel-1) * this.PageShowCount;
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
            
            // Page Item
            id = this.Id + '_PageItem_Item';
            if(items[i].id.indexOf(id) >= 0)
            {
                var pageIndex = parseInt(items[i].id.replace(id, ''));
                items[i].pageIndex = pageIndex;
                
                var columnIndex = (items[i].pageIndex % this.PageShowCount);
                if(items[i].pageIndex>0 && items[i].pageIndex % this.PageShowCount==0) {columnIndex = this.PageShowCount;}
                
                items[i].className = this.PageItemCssClass + " pager_item_column" + columnIndex;
                if(items[i].pageIndex%2 == 0 && typeof(this.PageAlternatingItemTemplate) == 'string' && this.PageAlternatingItemTemplate != '')
                {
                    items[i].className = this.PageAlternatingItemCssClass;
                }
                if(this.CurrentPageIndex != items[i].pageIndex)
                {
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.CurrentPageCssClass;
                }
            }
            
            // Next Level
            id = this.Id + '_PageItem_NextLevel';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass + ' ' + this.NextLevelCssClass;
                if(currentPageLevel < maxPageLevel)
                {
                    items[i].pageIndex = currentPageLevel * this.PageShowCount + 1;
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
            
            // Next Page
            id = this.Id + '_PageItem_NextPage';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass + ' ' + this.NextPageCssClass;
                if(this.CurrentPageIndex < this.PageTotal)
                {
                    items[i].pageIndex = parseInt(this.CurrentPageIndex + 1);
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
            
            // Last Page
            id = this.Id + '_PageItem_LastPage';
            if(items[i].id == id)
            {
                items[i].className = this.PageItemCssClass + ' ' + this.LastPageCssClass;
                if(this.CurrentPageIndex < this.PageTotal)
                {
                    items[i].pageIndex = this.PageTotal;
                    items[i].title = this.PageItemAltFormat.replace('{0}', items[i].pageIndex);
                    items[i].href = href;
                    items[i].onclick = function()
                    {
                        // set currentPageIndex
                        self.SetCurrentPageIndex(this.pageIndex);
                        
                        // execute onclick event
                        if(typeof(self.OnClickHandler) == 'function')
                        {
                            self.OnClickHandler(self);
                        }
                        
                        // rebuild
                        if(self.IsAutoComplete && !self.IsUrlPaging)
                        {
                            self.Build();
                        }
                    };
                    if(typeof(self.OnMouseOverHandler) == 'function')
                    {
                        items[i].onmouseover = function()
                        {
                            // set currentPageIndex
                            self.SetCurrentPageIndex(this.pageIndex);
                            
                            // execute onclick event
                            self.OnMouseOverHandler(self);
                            
                            // rebuild
                            if(self.IsAutoComplete && !self.IsUrlPaging)
                            {
                                self.Build();
                            }
                        };
                    }
                    if(this.IsUrlPaging == true)
                    {
                        items[i].href = '?{0}={1}&{2}={3}'
                                        .replace('{0}', this.ParamPage)
                                        .replace('{1}', items[i].pageIndex)
                                        .replace('{2}', this.ParamRecord)
                                        .replace('{3}', this.RecordShowCount);
                                        
                        if(queryString.length > 0)
                            items[i].href += '&' + queryString;
                    }
                }
                else
                {
                    items[i].removeAttribute('href');
                    items[i].onclick = null;
                    items[i].onmouseover = null;
                    items[i].className += ' ' + this.PageItemDisabledCssClass;
                }
            }
        }
        
        // Button submit event
        if(this.IsShowInput)
        {
            if(this.PageTotal > 1)
            {
                // method
                var submit = function()
                {
                    // set currentPageIndex
                    self.SetCurrentPageIndex(parseInt(document.getElementById(self.Id + '_txtInput').value));
                    
                    // execute onclick event
                    if(typeof(self.OnClickHandler) == 'function')
                    {
                        self.OnClickHandler(self);
                    }
                    
                    // 
                    self.Build();
                };
                
                // Input
                document.getElementById(self.Id + '_txtInput').onmouseover = function()
                {
                    // set focus to input
                    this.select();
                    this.focus();
                };
                document.getElementById(self.Id + '_txtInput').onkeydown = function(e)
                {
                    // if(typeof(event) == 'object') {e = event;}
                    e = e || event;
                    
                    // 
                    if((e.keyCode>=48 && e.keyCode<=57) || (e.keyCode>=96 && e.keyCode<=105) || e.keyCode==8 || e.keyCode==9) // 允许数字、Back、Tab键。
                    {
                        // 计算允许长度
                        var len = parseInt(self.PageTotal/10) + 1;
                        
                        // 长度不超过len，允许Back、Tab键。
                        if(this.value.length<len || e.keyCode==8 || e.keyCode==9)
                        {
                            return true;
                        }
                    }
                    else if(e.keyCode == 13) // Enter键
                    {
                        submit();
                    }
                    
                    return false;
                };
                
                // Submit
                if(this.IsShowSubmit)
                {
                    document.getElementById(self.Id + '_txtSubmit').onclick = submit;
                }
            }
            else
            {
                document.getElementById(self.Id + '_txtInput').disabled = true;
                if(this.IsShowSubmit)
                {
                    document.getElementById(self.Id + '_txtSubmit').disabled = true;
                }
            }
        }
        
        // 
        if(this.IsShowSelect)
        {
            document.getElementById(self.Id + '_ddlSelect').value = this.CurrentPageIndex;
            document.getElementById(self.Id + '_ddlSelect').onchange = function()
            {
                // set currentPageIndex
                var pageIndex = parseInt(this.value);
                self.SetCurrentPageIndex(pageIndex);
                
                // execute onclick event
                if(typeof(self.OnClickHandler) == 'function')
                {
                    self.OnClickHandler(self);
                }
                
                // 
                self.Build();
                
                // set focus to select
                document.getElementById(self.Id + '_ddlSelect').focus();
            };
            document.getElementById(self.Id + '_ddlSelect').onmouseover = function()
            {
                // set focus to input
                this.focus();
            };
        }
    };
    
    // 
    this.formatPageItemText = function(pageIndex)
    {
        if(pageIndex < 100)
        {
            pageIndex = '' + (100 + pageIndex);
            return pageIndex.substr(1, 2);
        }
        return pageIndex;
    };
    
    this.getQueryString = function(name)
    {
        var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
        if (reg.test(location.href))
            return unescape(RegExp.$2.replace(/\+/g, " "));
        return "";
    };
    
    this.trim = function(str) 
    {
        return str.replace(/(^&*)|(&*$)/g, ""); 
    };
};