﻿
var curSTGId;
var ThumbStubProfileId;
function setupThumbStubGrids()
{

    $(".StubThumbGrid").each(function (index) {        

       getStubThumbGrid($(this));      
       
    });
}

function getStubThumbGrid(tgTable)
{
  cellid = $(tgTable).attr("cellid");
  ThumbStubProfileId = $(tgTable).attr("profileid");
  curSTGId = tgTable;
  modToGet = moreCtlPath + "getContentCellFeed.aspx?cellid=" + cellid + "&profileid=" + ThumbStubProfileId + "&basePath=" + moreDNNBasePath + "&pageSize=16&pageNum=1";    
  $.ajax({
            type: "GET",
            async: false,
            url: modToGet,
            dataType: "xml",
            success: parseStubThumbGridXml,            
            error: tgerror,
            cache: false
       });   
}

function tgerror(xreq,tstat,err)
{
//alert(tstat);
}

function parseStubThumbGridXml(xml)
{   

          feed = xml;
          $(xml).children("rss").children("channel").each(function() {           
            var channelTitle = $(this).children("title").text();                       
            if (channelTitle == "Cell Feed")
            {
              var itemiter = 1;
               $(this).children("item").each(function() {
               
                 var tcells = parseInt($(curSTGId).attr("totalCells"))                                  
                 if (itemiter <= tcells)
                 {
                   
                   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 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();

                   $("#" + cellid).html("<div class=\"StubThumbOuterdiv\"><div profileid=\"" + ThumbStubProfileId + "\" moreType=\"Tag\" tag=\"" + tagMatch + "\" class=\"stubThumbGridTag\">" + tagMatch + "</div>" + linkstub + xmlImag + "<div class=\"stubThumbGridTitleText\">" + xmlTitle + "</div><div class=\"stubThumbGridText\">" + xmlDescription + "</a></div></div>");
                   setupMorePopupLinks();
                 }
                   itemiter++;
               });
            }
          });

       if (window.adjustTitleSizes()) {
           window.adjustTitleSizes();
          }
          if (window.setupLinkPopupLinks())
          {
             window.setupLinkPopupLinks();
          }
          if (window.setupArticlePopupLinks())
          {
            setupArticlePopupLinks()
        }


}
