//If you want to apply the css class, you should make sure discuss.css has been included
//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
//20090720:Henry modified it for thread view page.Add a new parameter useArrowButton
//         When useArrowButton,it will also show the first page button and the last page control 
function DClientPager(pageSize,totalCount,containerName,callback,useArrowButton)
{
    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.GetPage=function()
    {
         var args = location.hash;
         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;
         }
//         if(turnTo==this.PagerIndex)
//         {
//            return;
//         }
         var hash =this.RenerUrl(turnTo).replace(/^.*#/, '');
         $.historyLoad(hash); 
         this.RenderHTML();
    }
    
    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 += '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(this.PagerIndex==1)
        {
            if(useArrowButton)
            {
                //strHTML=strHTML+'<a class="dpage2 text_a2 firsticon" href="javascript:'+this.Name+'.GoToPage(1);"></a>'; 
                strHTML=strHTML+'<div class="dpage2 text_a2 firsticon"></div>';                
                strHTML=strHTML+'<div class="dpage2 text_a2 previcon"></div>';
            }
            else
            {
                strHTML=strHTML+'<div class="dpageOff text_a2">Previous</div>';
            }
        }
        else
        {
            if(useArrowButton)
            {
                strHTML=strHTML+'<a class="dpage2 text_a2 firsticon" href="javascript:'+this.Name+'.GoToPage(1);"></a>';
                strHTML=strHTML+'<a class="dpage2 text_a2 previcon" href="javascript:'+this.Name+'.GoToPage('+prevPage+');"></a>'
            }
            else
            {
                strHTML=strHTML+' '+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+prevPage+');">Previous</a>';
            }
        }
        if(this.PageCount<6)
        {
               for(var i=1;i<=this.PageCount;i++)
               {
                    if(i==this.PagerIndex)
                    {
                        strHTML=strHTML+' '+'<div class="dpageOn text_a2">'+i+'</div>';
                    }
                    else
                    {
                        strHTML=strHTML+' '+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                    }
               }          
        }
        else
        {
           if(this.PagerIndex==1)
           {
               strHTML=strHTML+'<div class="dpageOn text_a2">1</div>';
               for(var i=2;i<=5;i++)
               {
                    strHTML=strHTML+' '+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
               }
           }
           else
           {               
               if(this.PagerIndex==2)
               {
                    for(var i=1;i<6;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+'<div class="dpageOn text_a2">'+i+'</div>';
                        }
                        else
                        {
                            strHTML=strHTML+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }
               }
               else if(this.PagerIndex==this.PageCount)
               {
                    for(var i=this.PageCount-4;i<=this.PageCount;i++)
                    {
                        if(i==this.PagerIndex)
                        {
                            strHTML=strHTML+'<div class="dpageOn text_a2">'+i+'</div>';
                        }
                        else
                        {
                            strHTML=strHTML+'<a class="dpage text_a2" 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+'<div class="dpageOn text_a2">'+i+'</div>';
                        }
                        else
                        {
                            strHTML=strHTML+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</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+'<div class="dpageOn text_a2">'+i+'</div>';
                        }
                        else
                        {
                            strHTML=strHTML+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+i+');">'+i+'</a>';
                        }
                    }
               }
           }
        }
        if(this.PagerIndex==this.PageCount)
        {
            if(useArrowButton)
            {
                strHTML=strHTML+'<div class="dpage2Off text_a2 nexticon"></div>';
//                strHTML=strHTML+'<a class="dpage2 text_a2 lasticon" href="javascript:'+this.Name+'.GoToPage('+this.PageCount+');"></a>'
                strHTML=strHTML+'<div class="dpage2 text_a2 lasticon"></div>';
            }
            else
            {
                strHTML=strHTML+'<div class="dpageOff text_a2">Next</div>';
            }
        }
        else
        {
           if(useArrowButton)
           {
                strHTML=strHTML+'<a class="dpage2 text_a2 nexticon" href="javascript:'+this.Name+'.GoToPage('+nextPage+');"></a>'
                strHTML=strHTML+'<a class="dpage2 text_a2 lasticon" href="javascript:'+this.Name+'.GoToPage('+this.PageCount+');"></a>'
           }
           else
           {
                strHTML=strHTML+'<a class="dpage text_a2" href="javascript:'+this.Name+'.GoToPage('+nextPage+');">Next</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();
    }
}

