//Created by Henry 2008/06/19
//When you want to add the control, you should new an instance in proper place
//When you want to set the original status, please call the function Reset
function ClientPager(pageSize,totalCount,containerName,showTotalCount,totalCountMessage,threadView,pageIndex)
{
    this.PagerIndex=1;
    this.PageSize=pageSize;
    this.TotalCount=totalCount; //The count of the records
    this.PageCount=1;
    this.Name=containerName;//it will help to render the pager container
    this.ResetFlag=false;
    this.ShowTotalCount=showTotalCount;
    this.TotalCountMessage=totalCountMessage;
    this.GetPage=function()
    {
        if(!isNaN(parseInt(pageIndex)))
        {
            this.PagerIndex =pageIndex
        }
        else
        {
             var args = location.search;
             var reg = new RegExp('[\?&]?PageIndex=([^&]*)[&$]?', 'gi');
             var chk = args.match(reg);
             if(!this.ResetFlag&&chk!=null)
                this.PagerIndex = RegExp.$1;
         }
         this.ResetFlag=false;
    }
    
    this.FixParams=function()
    {
        if (isNaN(parseInt(this.PagerIndex))||this.PagerIndex<1) this.PagerIndex = 1;
        if (isNaN(parseInt(this.PageSize))||this.PageSize<1) this.PageSize=1;
        if (isNaN(parseInt(this.TotalCount))||this.TotalCount<0) this.TotalCount = 0;
        this.PagerIndex=parseInt(this.PagerIndex);
        this.PageSize=parseInt(this.PageSize);
        this.TotalCount=parseInt(this.TotalCount);
        this.PageCount=Math.ceil(this.TotalCount/this.PageSize);
        if(this.PageCount==0)
        {
            this.PageCount=1;
        }
        if(this.PagerIndex>this.PageCount)
           this.PagerIndex=this.PageCount;
    }  
    
    this.GoToPage = function(page){
         var turnTo = 1;
         if (typeof(page) == 'object') {
          turnTo = page.options[page.selectedIndex].value;
         } else {
          turnTo = page;
         }
         self.location.href = this.RenerUrl(turnTo);
    }
    
    this.RenerUrl=function (page) 
    { 
        if (isNaN(parseInt(page))) page = 1;
        if (page < 1) page = 1;
        if (page > this.pageCount) page = this.pageCount;
        var url = location.protocol + '//' + location.host + location.pathname;
        var args = location.search;
        var reg = new RegExp('([\?&]?)PageIndex=[^&]*[&$]?', 'gi');
        args = args.replace(reg,'$1');
        if (args == '' || args == null) 
        {
            args += '?PageIndex=' + page;
        } 
        else if (args.substr(args.length - 1,1) == '?' || args.substr(args.length - 1,1) == '&') 
        {
            args += 'PageIndex=' + page;
        } else {
            args += '&'+'PageIndex=' + page;
        }
        return url + args;
    }
    
    this.RenderHTML=function()
    {

        this.GetPage();
        this.FixParams();
        var strHTML = '', prevPage = this.PagerIndex - 1, nextPage = this.PagerIndex + 1;
        if(threadView)
        {
            strHTML=strHTML+'Page '+this.PagerIndex.toString() + ' of ' + this.PageCount.toString();
        }
        else
        {
            strHTML=strHTML+'<span class="sp_pgrangetotal">Page '+this.PagerIndex.toString() + ' of ' + this.PageCount.toString();
            strHTML=strHTML+'</span>';
        }
        if(this.ShowTotalCount)
        {
            if(threadView)
            {
                strHTML=strHTML+' (';
                if(this.TotalCountMessage)
                    strHTML=strHTML+this.TotalCount.toString()+'&nbsp;'+this.TotalCountMessage+')'
            }
            else
            {
                strHTML=strHTML+' |';
                if(this.TotalCountMessage)
                    strHTML=strHTML+'<span class="sp_pgrangetotal">&nbsp;'+this.TotalCount.toString()+'&nbsp;'+this.TotalCountMessage+'</span>';
                else
                    strHTML=strHTML+'<span class="sp_pgrangetotal">&nbsp;'+this.TotalCount.toString()+' Records'+'</span>';
            }
        }
        if(this.PageCount==1)
            return strHTML;
        if(!threadView)
        {
            strHTML=strHTML+'|';
        }
        if(this.PageCount<6)
        {
           if(this.PagerIndex==1)
           {
               strHTML=strHTML+"<span> 1 </span>";
               for(var i=2;i<=this.PageCount;i++)
               {
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
               }
               strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';
           }
           else if(this.PagerIndex==this.PageCount)
           {
                strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+prevPage+');">&#60; Previous</a>';
                for(var i=1;i<this.PageCount;i++)
                {
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                }
                strHTML=strHTML+' '+'<span>'+this.PageCount+'</span>';
           }
           else
           {
               strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+prevPage+');">&#60; Previous</a>';
               for(var i=1;i<=this.PageCount;i++)
               {
                    if(i==this.PagerIndex)
                    {
                        strHTML=strHTML+' '+'<span">'+i+'</span>';
                    }
                    else
                    {
                        strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                    }
               }
               strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';             
           }
        }
        else
        {
           if(this.PagerIndex==1)
           {
               strHTML=strHTML+"<span> 1 </span>";
               for(var i=2;i<=5;i++)
               {
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
               }
               strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';
               strHTML=strHTML+'...'+'<a href="javascript:'+this.Name+'.GoToPage('+this.PageCount+');">Last &#187;</a>';
           }
           else
           {
               //Add first button
               if(this.PagerIndex>3)
               {
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+1+');">&#171; First</a>'+'...';
               }
               strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+prevPage+');">&#60; Previous</a>';
               
               if(this.PagerIndex==2)
               {
                    for(var i=1;i<6;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+' '+'<span">'+i+'</span>';
                        }
                        else
                        {
                            strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';
               }
               else if(this.PagerIndex==this.PageCount)
               {
                    for(var i=this.PageCount-4;i<=this.PageCount;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+' '+'<span">'+i+'</span>';
                        }
                        else
                        {
                            strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }                   
               }               
               else if((this.PagerIndex+2)<=this.PageCount)
               {
                    for(var i=this.PagerIndex-2;i<=this.PagerIndex+2;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+' '+'<span">'+i+'</span>';
                        }
                        else
                        {
                            strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';
               }
               else if(this.PagerIndex==this.PageCount-1)
               {
                    for(var i=this.PagerIndex-3;i<=this.PagerIndex+1;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+' '+'<span">'+i+'</span>';
                        }
                        else
                        {
                            strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }
                    strHTML=strHTML+' '+'<a href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next &#62;</a>';
               }
             
//               //Add last button
               if((this.PagerIndex+2)<=this.PageCount)
               {
                    strHTML=strHTML+'...'+'<a href="javascript:'+this.Name+'.GoToPage('+this.PageCount+');">Last &#187;</a>';
               }
           }
        }
        return strHTML;
    }
    
    this.Initialize = function()
    {

        var pagerContainer=document.getElementById('pager_'+this.Name);
        if(!pagerContainer)
        {
            document.write('<div id="pager_' + this.Name+'" class="sp_pgrange"></div>');
            pagerContainer=document.getElementById('pager_'+this.Name);
        }
            	    
        pagerContainer.innerHTML=this.RenderHTML();
    }
   
    this.Initialize();
    
    
    this.Reset=function(totalCount)
    {
        this.ResetFlag=true;
        this.PagerIndex=1;
        this.TotalCount=totalCount;
        this.Initialize();
    }
}

