﻿function Paging()
{
    this.pageSize = null;  //max page size from connectionSettings.config
    this.pageNumber = 1;
    this.totalPages = null;
	this.totalGroups = null;
    this.currentGroup = 1;
    this.groupStart = null;
    this.groupEnd = null;
    this.maxElementCount = 0;
}

Paging.prototype.initPagingLinks = function(elementCount)
 {
     this.totalPages = (elementCount>this.pageSize)?Math.ceil(elementCount/this.pageSize):1;  //total num of pages
     this.totalGroups = (this.totalPages>10)?Math.ceil(this.totalPages/10):1;                 //total num groups of 10  
     this.scrollPagingGroup(this.currentGroup);
}
Paging.prototype.scrollPagingGroup = function(group) {
    this.currentGroup = group;
    this.groupStart = (group*10)-9;
    this.groupEnd = group*10;

    var html="<span style=\"font-size:.8em;\">Page: </span>";
        
     //if(parseInt(this.currentGroup)>1){html+="<a href=\"javascript:_paging.scrollPagingGroup("+(parseInt(this.currentGroup)-1)+");\"><img align=\"middle\" src=\"../App_Themes/Default/Images/Map/arrow_black_LT.png\"></a>";}
     if(parseInt(this.pageNumber)>1){html+="<a href=\"javascript:_paging.gotoPage("+(parseInt(this.pageNumber)-1)+");\"><img align=\"middle\" src=\"../App_Themes/Default/Images/Map/arrow_black_LT.png\"></a>";}
     if(parseInt(this.totalPages)<parseInt(this.groupEnd)) {html+=this.getPagingGroup(this.groupStart,this.totalPages);}
     else {html+=this.getPagingGroup(this.groupStart,this.groupEnd);}   
     //if(parseInt(this.currentGroup)<parseInt(this.totalGroups)){html+="<a href=\"javascript:_paging.scrollPagingGroup("+(parseInt(this.currentGroup)+1)+");\"><img style=\"margin-left:3px;\" align=\"middle\" src=\"../App_Themes/Default/Images/Map/arrow_black_RT.png\"></a>";}
     if(parseInt(this.pageNumber)<this.totalPages){html+="<a href=\"javascript:_paging.gotoPage("+(parseInt(this.pageNumber)+1)+");\"><img style=\"margin-left:3px;\" align=\"middle\" src=\"../App_Themes/Default/Images/Map/arrow_black_RT.png\"></a>";}
     $('divPagingTop').innerHTML = html;
     $('divPagingBottom').innerHTML = html;
}
Paging.prototype.getPagingGroup = function(start,end) {
    var html='';
    var color='';
    for(var i=start;i<=end;i++) {
        color=(i==this.pageNumber)?"ff000f":"000000";
        html+="<a style=\"font-size:.8em;text-decoration:none;color:#"+color+";font-weight:bold;margin-left:5px;\" href=\"javascript:_paging.gotoPage('"+i+"');\">"+i+"</a>";
     }
     return html;
}
Paging.prototype.gotoPage = function(pageNum) {
    // Determine if we need to alert that we're passing over the max element count threshhold
    // TODO: Implement total element view counter so we can display it each time they hit the max element count threshhold
    var startElement = (pageNum*this.pageSize)-(this.pageSize-1);
    var endElement = pageNum*this.pageSize;
    if (this.maxElementCount > 0 && endElement > this.maxElementCount) {
        alert("You have reached the maximum number of items\nthat are allowed to be displayed at a single time.\nPlease click OK to resubmit your query.");
    }

    
    this.pageNumber=pageNum;
    if(pageNum > this.groupEnd){this.scrollPagingGroup(parseInt(this.currentGroup+1));}
    if(pageNum<this.groupStart){this.scrollPagingGroup(parseInt(this.currentGroup-1));}
    GetListings();
}
Paging.prototype.clearPaging = function() { 
    $('divPagingTop').innerHTML = "";
    $('divPagingBottom').innerHTML = "";
}
