﻿
var morePageSize = 16;
var moreCurPage = 1;
var moreCurCell;
var moreCurProfile;
var morePopped = false;
var moreCurHLType;
var moreType;
var moreModuleId;
var moreItemId;
var moreTag;
var moreSourceTitle;

function setupMorePopupLinks()
{

    $(".morepopup").unbind('click',handlemorePopup);
    $(".morepopup").click(handlemorePopup);
    
    $(".stubThumbGridTag").unbind('click',handlemorePopup);
    $(".stubThumbGridTag").click(handlemorePopup);
}

function setMorePagerHandles()
{
  $(".morePagerButton").unbind('click',handleMorePage);
  $(".morePagerButton").click(handleMorePage);
  
  $(".morePagerPreviousButton").unbind('click',handleMorePrevious);
  $(".morePagerPreviousButton").click(handleMorePrevious);
  
  $(".morePagerNextButton").unbind('click',handleMoreNext);
  $(".morePagerNextButton").click(handleMoreNext);
}

function handleMorePrevious()
{
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupMore(moreCurCell,moreCurProfile, moreCurPage-1);
}

function handleMoreNext()
{
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupMore(moreCurCell,moreCurProfile, moreCurPage+1);
}

function handleMorePage()
{
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupMore(moreCurCell,moreCurProfile, parseInt($(this).text()));
    
}

function handlemorePopup()
{

    moreCurPage = 1;
    var popupcellid =  $(this).attr("cellid");   
    var popupprofileid = $(this).attr("profileid");
    moreCurHLType = $(this).attr("hltype");
    moreType = $(this).attr("moreType");
    if (moreType != undefined)
    {
       moreCurProfile = $(this).attr("articleProfileId");
       moreModuleId = $(this).attr("moduleid");
       moreItemId = $(this).attr("moduleitemid");
       moreTag = $(this).attr("tag");

   }
   
   moreSourceTitle = $(this).attr("sourceTitle") ;
   if (moreSourceTitle == undefined)
       moreSourceTitle = "";
   else
     moreSourceTitle +="\"";
  
    
    $("#mainMorePopup").css({top:($(window).scrollTop()+50)+"px"}).show(); 
    $("#mainMorePopup").show();
    $("#mainMoreContent").html("<img src=\"images/ajax-loader.gif\" />");
   
    getPopupMore(popupcellid, popupprofileid, moreCurPage);
    
}

function getPopupMore(cellid,profileid,page)
{

   moreCurCell = cellid;
   moreCurProfile = profileid;
   moreCurPage= page;
   var modToGet
   
   if (moreCurHLType == undefined && moreType == undefined)
   {
   // var modToGet  = moreCtlPath+"GetCellMore.aspx?cellid="+cellid.toString()+"&profileid="+profileid+"&basepath="+moreDNNBasePath;
       modToGet = moreCtlPath + "getContentCellFeed.aspx?cellid=" + cellid + "&displayType=morePopup&profileid=" + profileid + "&basePath=" + moreDNNBasePath + "&pageSize=" + morePageSize + "&pageNum=" + moreCurPage;
   }    
    else if (moreType != undefined)
    {     
      if (moreType == 'Tag')            
        modToGet = moreCtlPath+"getContentCellFeed.aspx?profileid="+moreCurProfile+"&contentType=Tag&tag="+moreTag+"&basePath="+moreDNNBasePath+"&pageSize="+morePageSize+"&pageNum="+moreCurPage;
      else
      
        modToGet = moreCtlPath+"getContentCellFeed.aspx?profileid="+moreCurProfile+"&contentType=Similar&moduleid="+moreModuleId+"&moduleitemid="+moreItemId+"&basePath="+moreDNNBasePath+"&pageSize="+morePageSize+"&pageNum="+moreCurPage;    
    }
    else
    {
        modToGet = moreCtlPath + "getContentCellFeed.aspx?contentType=" + moreCurHLType + "&cellid=" + cellid + "&displayType=morePopup&profileid=" + profileid + "&basePath=" + moreDNNBasePath + "&pageSize=" + morePageSize + "&pageNum=" + moreCurPage;    
    }
      
      
    //alert(modToGet);
    $.ajax({
            type: "GET",
            async: false,
            url: modToGet,
            dataType: "xml",
            success: ajshowmorePopup,                       
            cache: false
            
       });   
}



function ajshowmorePopup(xml)
{

    //$("#mainMoreContent").html(data);
    parseMoreStubThumbGridXml(xml, (moreCurPage * morePageSize) - (morePageSize-1));
    
    
    showmorePopup();
   
}


function parseMoreStubThumbGridXml(xml,startitem)
{   

          feed = xml;
          var totalResults;

          $(xml).children("rss").children("channel").each(function () {

              var channelTitle = $(this).children("title").text();

              var cellTitle = $(this).children("[nodeName=InceptionPoint.CellFeed:CellTitle]").text();
              $("#mainMoreTitle").html(cellTitle + moreSourceTitle);
              if (channelTitle == "Cell Feed") {
                  var itemiter = 1;
                  var curcol = 1;
                  var tcells = 4
                  var gridhtm = "<table class=\"StubThumbGrid\"><tr>";
                  $(this).children("item").each(function () {

                      if ($(this).children("title").text() != "") {
                          totalResults = $(this).children("[nodeName=InceptionPoint.CellFeed:TotalResults]").text();
                          //  if (itemiter < startitem+morePageSize && itemiter >= startitem )
                          //  {                                  
                          if (curcol > tcells) {
                              gridhtm += "</tr><tr>";
                              curcol = 1;
                          }
                          gridhtm += "<td class=\"StubThumbSpacer\"></td><td class=\"StubThumbGridContentCell\" id=\"StubThumbGridMoreCell" + itemiter.toString() + "\">";
                          gridhtm += "<div class=\"StubThumbOuterdiv\">";
                          var xmlItemId = $(this).children("itemid").text();
                          var xmlTitle = $(this).children("title").text();
                          var xmlDescription = $(this).children("description").text();
                          //xmlDescription = xmlDescription.substring(0, 100);
                          var tagMatch = $(this).children("[nodeName=InceptionPoint.CellFeed:FirstMatchedTag]").text();
                          var xmlImag = $(this).children("[nodeName=InceptionPoint.CellFeed:Image]").text();
                          xmlImag = xmlImag.replace("//", "/");
                          var xmlItemType = $(this).children("[nodeName=InceptionPoint.CellFeed:ItemType]").text();
                          var xmlItemModuleId = $(this).children("[nodeName=InceptionPoint.CellFeed:ItemModuleId]").text();
                          var cellid = $(curSTGId).attr("id")
                          var linkArticleItemId = "";

                          var linkstub = "<a ";
                          if (xmlItemType == 'zLinks') {
                              linkstub += "profilerModuleId=\"" + xmlItemModuleId + "\" profilerItemId=\"" + xmlItemId + "\" linkitemid=\"" + xmlItemId + "\" class=\"profileCollect zLinkStub\">"
                          }
                          else if (xmlItemType == 'Popup Articles') {

                              linkstub += "profilerModuleId=\"" + xmlItemModuleId + "\" profilerItemId=\"" + xmlItemId + "\" articleitemid=\"" + xmlItemId + "\" class=\"profileCollect articlePopupLink\">"
                          }
                          cellid = cellid + "cell" + itemiter.toString();

                          gridhtm += "<div profileid=\"" + ThumbStubProfileId + "\" moreType=\"Tag\" tag=\"" + tagMatch + "\" class=\"stubThumbGridTag\">" + tagMatch + "</div>" + linkstub + xmlImag + "<div class=\"stubThumbGridTitleText\">" + xmlTitle + "</div><div class=\"stubThumbGridText\">" + xmlDescription + "</a></div>";
                          gridhtm += "</div>";
                          gridhtm += "</td>";
                          curcol++;
                          // }
                          itemiter++;
                      }
                  });
                  gridhtm += "</tr></table>";
                  $("#mainMoreContent").html(gridhtm);
                  setupMorePopupLinks();
              }



              var numpages = Math.round(totalResults / morePageSize);
              var pagerHtm = "";
              var curClass = "";
              if (moreCurPage != 1) {
                  pagerHtm += "<a class=\"morePagerPreviousButton\" >Previous</a> ";
              }
              for (i = 1; i <= numpages; i++) {
                  if (i == moreCurPage)
                      pagerHtm += "<span class=\"moreCurPage\" >" + i.toString() + "</span>";
                  else
                      pagerHtm += "<a class=\"morePagerButton\" >" + i.toString() + "</a> ";

              }
              if (moreCurPage < numpages) {
                  pagerHtm += "<a class=\"morePagerNextButton\" >Next</a> ";
              }
              $("#mainMorePager").html(pagerHtm);
              setMorePagerHandles();

          });          
         
         
         
          
         
       //   if (window.setupLinkPopupLinks())
       //   {
       //      window.setupLinkPopupLinks();
       //   }
       //   if (window.setupArticlePopupLinks())
       //   {
       //     setupArticlePopupLinks()
       //   }
}


function adjustTitleSizes() {
    $(".stubThumbGridTitleText").each(function () {
        tlen = $(this).text().length;
        if (tlen > 60) 
        {
            $(this).text( $(this).text().substring(0,60)+"...") ;
            tlen = $(this).text().length;
        }
        if (tlen <= 20)
            $(this).css("font-size", "10.5pt");
        else if (tlen > 20 && tlen < 30)
            $(this).css("font-size", "9.5pt");
        else if (tlen > 30 && tlen < 40)
            $(this).css("font-size", "8.5pt");
        else if (tlen >50)
            $(this).css("font-size", "8pt");
    });
}

function showmorePopup() {

    adjustTitleSizes();
   morePopped = true;
  $("#mainMorePopup").show();
  if (articlePopped) $("#mainArticlePopup").hide();
  $("#zLinkPopup").hide();
  
    if (window.setupLinkPopupLinks())
    {
      window.setupLinkPopupLinks();
    }
    
    if (window.setupArticlePopupLinks())
    {
        setupArticlePopupLinks()
    }
}


