﻿  function paginate(id,followUp,newPageNum){
    displayPage=newPageNum;
    var searchType = $('#searchType').val();
    $("#showPeople ul").html('<center><img src="/graphics/builder/loading_v2.gif" width="127" height="50" style="margin-top: 10px;" /></center>');
    var tohtml="";
    if(searchType=='people')  {
      DirectoryPaginator.getPersons(followUp, {
        callback:function(results) {buildPeoplePage(results);},
        errorHandler:function(message) { alert("Oops: " + message); }
      });
    } else {
      DirectoryPaginator.getCommunitiesWithCount(followUp, {
        callback:function(results) { buildCommunitiesPage(results);},
        errorHandler:function(message) { alert("Oops: " + message); }
      });
    }
    //need to refactor
    //pagination();

  }
  function buildPeoplePage(results){
    var tohtml="",record="";
    var listCount=results.length;
    for (var i = 0; i < results.length; i++) {
      record = results[i];
      var name=record.firstName+' '+record.currentLastName;
      if(record.lastName!=record.currentLastName){
        name+=" ("+ record.lastName +")";
      }
      var index=i+1;
      tohtml += '<li><div style=\"padding: 29px 0px 0px 0px;\"><div><span class=\"pubFloatRight\"><a rel=\"nofollow\" href=\"#\"  alt=\"View '+ name + ' Profile\" title=\"View ' + name + ' Profile\" class=\"registerToday\" id=\"' + index + '\">View Profile &raquo;<\/a><\/span><a rel=\"nofollow\" href=\"#\" alt=\"'+ name +'\" title=\"' + name + '\" class=\"registerToday public-viewPro '+ index +'\" id=\"theName'+ index + '\">'+ name +'<\/a>';
      tohtml+='<div id=\"theFirName'+ index +'\" class=\"pubHide\">'+record.firstName+'<\/div><div class=\"'+ index +'RegId pubHide">'+ index +'<\/div>';
      var affils = record.affils;
      if(affils!=null){
        var sortAffil="";
        for(var j=affils.length-1 ; j >=0 ;j--){
          var affil = affils[j];
          var city=(affil.city)?', '+affil.city:'';
          var state=(affil.state)?', '+affil.state:'';
          tohtml = tohtml + '<div class=\"'+ index + '\">'+ affil.name + city + state + ' '+ affil.startYear+"-"+affil.endYear +'<\/div>';
          var comType=affil.id.substring(0,1);
          if(comType=='s'){comTypeName='school';}
          if(comType=='c'){comTypeName='college';}
          if((comType=='s') || (comType=='c')){
            tohtml = tohtml + '<div class=\"'+ index +'Comm pubHide\">'+ affil.name +';'+ affil.city + state + ':'+ affil.id.substring(1,affil.id.length) +':'+ affil.cityId +':'+ comTypeName +'|<\/div>';
          }
          //tohtml = tohtml+affil.name+"("+affil.id+")"+' '+affil.city+","+ affil.state+" "+affil.startYear+"-"+affil.endYear+"<br>";
          tohtml = tohtml + '';
        }
      }
      //tohtml+ = tohtml;
      tohtml = tohtml + '<\/div><\/li>';
      
    }
    $("#showPeople ul").empty().html(tohtml);
    pagination(listCount);
    showPopUp();
  }
  function buildCommunitiesPage(results){
    var tohtml="",record="",aluminiCount="";
    var listCount=results.length;
    for (var i = 0; i < results.length; i++) {
      record = results[i].clientCommunity;
      aluminiCount = results[i].count;
      var index=i+1;
      var orgName=record.orgName;
      var orgType=record.orgType;
      var state=(record.state)?','+record.state:'';
      tohtml += '<li><div style=\"padding: 29px 0px 0px 0px;\"><div>';
      tohtml +='<a href=\"/directory/'+ orgType.toLowerCase() +'/'+ record.orgName +'?org=' + record.orgId + '\" alt=\"'+ orgName +'\" title=\"'+ orgName +'\" class=\"public-viewPro '+ index +'\">'+ record.orgName +'<\/a>';
      tohtml +='<\/div><div class=\"'+ index +' pubHide\">; <\/div>';
      tohtml +='<div class=\"'+ index +'\">'+ record.city + state + '<\/div><div class=\"'+ index +' pubHide\">:'+ record.orgId +':'+ orgName +':'+ orgType +'|<\/div>';
      if (aluminiCount<1) {
       aluminiCount='';
      }
      tohtml +='<div><a id=\"'+ index +'\" class=\"registerToday\" href=\"#\" alt=\"Register to join the alumni at this school\" title=\"Register to join the alumni at this school\" rel=\"nofollow\">Register to join the <b>' + aluminiCount+ '</b> alumni at this school &raquo;<\/a><\/div>';
      tohtml = tohtml + '<\/div><\/li>';
    }
    $("#showPeople ul").html(tohtml);
    showPopUp();
    pagination(listCount);
  }
  function pagination(listCount){
    var result=$(".pagination");
    if (totalPages > 1) {
      var paginationHtml='';
       paginationHtml="<ul style=\"padding: 0px;\">";
      // Setup beginning of pagination
      if (displayPage != 1) {
        // Previous Link
        var c=buildPageLink(displayPage-1, " &#171; previous ");
        paginationHtml+=buildPageLink(displayPage-1, " &#171; previous ");;
      }

      // Rule for at least one set of elipses, depends on surrounding pages -
      var leftDeltaRange = 1;
      var rightDeltaRange = 1;
      if (displayPage == 1) {
        rightDeltaRange = 2;
      } else if (displayPage == totalPages) {
        leftDeltaRange = 2;
      }

      var leftRange = displayPage - leftDeltaRange;
      var rightRange = parseInt(displayPage)+parseInt(rightDeltaRange);
      var leftElipses = 'false';
      var rightElipses = 'false';

      for (var i = 1; i <= totalPages; i++) {

        if (i == displayPage) {
          paginationHtml+="<li>" + i + " <\/li> ";
        } else if ((i <= rightRange && i >= leftRange) || i == totalPages
            || i == 1) {
          paginationHtml+=""+buildPageLink(i, i)+"";
        } else if (i < displayPage && leftElipses=='false') {
          paginationHtml+="<li> ... <\/li>";
          leftElipses = 'true';
        } else if (i > displayPage && rightElipses=='false') {
          paginationHtml+="<li> ... <\/li>";
          rightElipses = 'true';
        }

      }

      // Next link
      if (displayPage != totalPages) {
        paginationHtml+=buildPageLink(parseInt(displayPage)+1, " next &#187; ");
      }
      paginationHtml+="</ul>";
      result.html(paginationHtml);
      $(".pagination ul li").css('display', 'inline');
    }
    if(listCount!='undefined'){
      var endIndex,strtIndex;
      endIndex=displayPage*32;
      if(endIndex>totalCount){
        endIndex=totalCount;
        strtIndex=totalCount-listCount+1;
      } else {
        strtIndex=endIndex-31;
      }
      $("#resultCount").html(strtIndex +"-"+endIndex);
    }
  }
  function buildPageLink(pageNumber, pageContent){
    return "<li><a href=\"javascript: paginate(\'\',\'" + paginateBaseQuery + (pageNumber-1) + "\',\'" + pageNumber + "\');\">" + pageContent + " <\/a><\/li>";
  }