﻿var Pager = {
    button:1,
    CachedPages:new Array(),
    GetPagerInfo: function(pagerId, infoType) {
        var infocnt = Control.GetControlInControl(document.getElementById(pagerId), 'PagerInfo');
        var value = infocnt.value;
        if (value.split('c:clm').length > 1) {
            var infos = value.split('c:clm');
            if (infoType == 'parent') {
                return infos[0];
            }
            if (infoType == 'id') {
                return infos[1];
            }
            if (infoType == 'currentpage') {
                return infos[2];
            }
            if (infoType == 'selectmethod') {
                return infos[3];
            }
            if (infoType == 'countmethod') {
                return infos[4];
            }
            if (infoType == 'rows') {
                return infos[5];
            }
            if (infoType == 'showpagenumbers') {
                return infos[6];
            }
            if (infoType == 'nextpagetext') {
                return infos[7];
            }
            if (infoType == 'prevpagetext') {
                return infos[8];
            }
            if (infoType == 'showcurrentpage') {
                return infos[9];
            }
            if (infoType == 'content') {
                return infos[10];
            }
            if (infoType == 'keepoldrows') {
                return infos[11];
            }
            if (infoType == 'useajax') {
                return infos[12];
            } 
            if (infoType == 'ajaxmethod') {
                return infos[13];
            }
            if (infoType == 'imageFolder') {
                return infos[14];
            }
            return "";
        }
    },
    
    SetPagerInfo: function(pagerId, infoType, value) {
        var infocnt = Control.GetControlInControl(document.getElementById(pagerId), 'PagerInfo');
        var values = infocnt.value;
        if (values.split('c:clm').length > 1) {
            var infos = values.split('c:clm');
            if (infoType == 'parent') {
                infos[0] = value;
            }
            if (infoType == 'id') {
                infos[1] = value;
            }
            if (infoType == 'currentpage') {
                infos[2] = value;
            }
            if (infoType == 'selectmethod') {
                infos[3] = value;
            }
            if (infoType == 'countmethod') {
                infos[4] = value;
            }
            if (infoType == 'rows') {
                infos[5] = value;
            }
            if (infoType == 'showpagenumbers') {
                infos[6] = value;
            }
            if (infoType == 'nextpagetext') {
                infos[7] = value;
            }
            if (infoType == 'prevpagetext') {
                infos[8] = value;
            }
            if (infoType == 'showcurrentpage') {
                infos[9] = value;
            }
            if (infoType == 'content') {
                infos[10] = value;
            }
            if (infoType == 'keepoldrows') {
                infos[11] = value;
            }
            if (infoType == 'useajax') {
                infos[12] = value;
            } 
            if (infoType == 'ajaxmethod') {
                infos[13] = value;
            } 
            if (infoType == 'imageFolder') {
                infos[14] = value;
            }
            infocnt.value = infos[0] + 'c:clm' + infos[1] + 'c:clm' + infos[2] + 'c:clm' + infos[3] + 'c:clm' + infos[4] + 'c:clm' + infos[5] + 'c:clm' + infos[6] + 'c:clm' + infos[7] + 'c:clm' + infos[8] + 'c:clm' + infos[9]+'c:clm'+infos[10] +'c:clm' +infos[11] +'c:clm'+infos[12]+'c:clm'+infos[13] +'c:clm'+infos[14];
        }
    },
    
    Open:function(parent,id,ajaxmethod,countmethod,selectmethod,page)
    {
            this.SetPagerInfo(id,'currentpage',page);
        this.Refresh(parent, id,ajaxmethod, countmethod,selectmethod);
    },
     
    Next: function(parent, id,ajaxmethod, countmethod, selectmethod) {
        this.button=1;
        if(this.GetPagerInfo(id, 'useajax')=="True")
        {
            var page =( this.GetPagerInfo(id, 'currentpage')-1)+2;
            this.RunAjaxMethod(parent,id,page,ajaxmethod);
        }
        else
        {
            var count= countmethod();
            this.NextRender(parent,id,count);
            var content= selectmethod(cpage);
            this.Render(parent, id, countmethod, selectmethod);
        }
    },

    Prev: function(parent, id,ajaxmethod, countmethod, selectmethod) {
        this.button=0;
        if(this.GetPagerInfo(id, 'useajax')=="True")
        {
            var page =( this.GetPagerInfo(id, 'currentpage')-1);
            this.RunAjaxMethod(parent,id,page,ajaxmethod);
        }
        else
        {
            var count= execScript( countmethod);
            this.PrevRender(parent,id,count);
            var content= selectmethod(cpage);
            this.Render(parent, id, countmethod, selectmethod,count,content);
        }
    },
    Refresh:function(parent,id,ajaxMethod,selectMethod,countMethod)
    {
        if(this.GetPagerInfo(id, 'useajax')=="True")
        {
            var page =( this.GetPagerInfo(id, 'currentpage'));
           // this.RunAjaxMethod(parent,id,page,ajaxMethod);
        }
        else
        {
            var count= countmethod(); 
            var content= selectmethod(cpage);
            this.Render(parent, id, countmethod, selectmethod,count,content);
        }
    },
    
    RunAjaxMethod:function(parent,id,page,ajaxmethod)
    {
            ajaxmethod=  String.Replace( ajaxmethod,'#Id#',id);
            ajaxmethod = String.Replace(ajaxmethod,'#Parent#',parent);
            ajaxmethod = String.Replace(ajaxmethod,'#Page#',page);
            ajaxmethod= String.Replace(ajaxmethod,'#Button#',this.button);
            
            if (window.execScript) {
window.execScript(ajaxmethod); //for IE
} else {
eval( ajaxmethod); //for Firefox
}
           // execScript(ajaxmethod);
    }, 
    
    ResponseFromAjax:function(button,parent,id,count,content)
    {
        if(this.GetPagerInfo(id, 'useajax')=="True")
        {
    
            if(button==1)
            {
                this.NextRender(parent,id,count);
                this.Render(parent, id, count,content);
            }
            else
            {
                this.PrevRender(parent,id,count);
                this.Render(parent, id, count,content);
            }
        }
    },
     
    
    PrevRender:function(parent,id, count)
    {
     var page = this.GetPagerInfo(id, 'currentpage');
        var row = this.GetPagerInfo(id, 'rows');
        
        var pages = count / row;
        if (count < row) {
            pages = 1;
        }
        var current = page;
        if (page > 1) {
            current--;
        }
        else {
            current = pages;
        }
        this.SetPagerInfo(id, 'currentpage', current);

        
    },

    NextRender:function(parent,id, count)
    {
      var page = this.GetPagerInfo(id, 'currentpage');
            var row = this.GetPagerInfo(id, 'rows');
           
            var pages = count / row;
            if (count < row) {
            pages = 1;
            }
            var current = page;
            if (pages > page) {
            current++;
            }
            this.SetPagerInfo(id, 'currentpage', current);
    },
    
    Render: function(parent, id, count,contents) {
        var cpage = this.GetPagerInfo(id, 'currentpage');
        var row = this.GetPagerInfo(id, 'rows');
        var pages = count / row;
        if (count < row) {
            pages = 1;
        }
        if(count%row!=0)
        {
            pages++;
        }
        if (this.GetPagerInfo(id, 'showcurrentpage') == "True") {
            var pageViewer = Control.GetControlInControl( Control.GetControlInControl(document.getElementById(parent),id), 'PagerContent');
        pageViewer.innerHTML = cpage + ' / ' + pages;
        }
        if (this.GetPagerInfo(id, 'showpagenumbers') == "True") {
            var pageViewer = Control.GetControlInControl( Control.GetControlInControl(document.getElementById(parent),id), 'PagerContent');
            var pagecont ='';
            for(var p=1; p<=pages; p++)
            {
            if(p==cpage)
            {
                pagecont  +="&nbsp;<a style=\"color:black;\">" + p + "</a>&nbsp;";
                
            }
            else{
            var ajm = this.GetPagerInfo(id, 'ajaxmethod');
            ajm=String.Replace(ajm,"'","%TekTirnak%");
            ajm=String.Replace(ajm,"%TekTirnak%","\\'");
            var cm=this.GetPagerInfo(id, 'countmethod');
            if(cm.length==0)
            {
            cm='null';
            }
            var sm =this.GetPagerInfo(id, 'selectmethod') ;
            if(sm.length==0)
            {
            sm='null';
                }
                pagecont +=" &nbsp;<a href=\"javascript:Pager.Open('" + parent + "','" + id + "','" + ajm  + "'," + cm + "," + sm + "," + p + ")\">" + p + "</a>&nbsp; ";
                }
            }
        pageViewer.innerHTML = pagecont;
        }
        
        var content =  this.GetPagerInfo(id,'content');
        if(this.GetPagerInfo(id, 'keepoldrows')=="True")
        {
            if(this.button==1)
            {
                 Control.GetControlInControl(document.getElementById(parent), content).innerHTML = contents+document.getElementById(content).innerHTML;
            }
            else
            {
                 Control.GetControlInControl(document.getElementById(parent), content).innerHTML +=contents;
            }
        }
        else
        {
                Control.GetControlInControl(document.getElementById(parent), content).innerHTML = contents;
        }
      },
   
    GetContent: function(id, countmethod, selectmethod) {
        return selectmethod(this.GetPagerInfo(id, 'currentpage'));
    },

    AddItem: function(parent,id, itemId,count)
    {  
      var row = this.GetPagerInfo(id, 'rows');
   var pages =Math.round( count/row);
     if (count < row) {
            pages = 1;
        }
        if(count%row<row/2 && count%row!=0)
        {
            pages++;
        }
        
            this.SetPagerInfo(id, 'currentpage', pages);
        
            var pager= Control.GetControlInControl( document.getElementById(parent),id);
            pager.style.visibility="visible";
           pager.style.display=''; 
            var ajm = this.GetPagerInfo(id, 'ajaxmethod'); 
           
            var cm=this.GetPagerInfo(id, 'countmethod');
            if(cm.length==0)
            {
            cm='null';
            }
            var sm =this.GetPagerInfo(id, 'selectmethod') ;
            if(sm.length==0)
            {
            sm='null';
                }
                 
         this.Refresh(parent,id,ajm,sm,cm);
    }
    
}

