﻿var HistoryListCellId
var HistoryListProfileId
var curHistoryList
var curHLType


function setupProfileCells()
{

    $(".HistoryList").each(function (index) {                    
       getHistoryList($(this),"History");             
    });
    
    $(".PopularList").each(function (index) {                
       getHistoryList($(this),"Popular");             
    });
    
    $(".RecentList").each(function (index) {                      
       getHistoryList($(this),"Recent");             
    });
}

function getHistoryList(tgList, hltype)
{
  curHLType = hltype;
  cellid = $(tgList).attr("cellid");
  profileid = $(tgList).attr("profileid");
  HistoryListCellId = cellid;
  HistoryListProfileId = profileid;
  curHistoryList = tgList;


  modToGet = moreCtlPath + "getContentCellFeed.aspx?contentType=" + hltype + "&cellid=" + cellid + "&profileid=" + profileid + "&basePath=" + moreDNNBasePath + "&pageSize=16&pageNum=1" ;
  
  $.ajax({
            type: "GET",
            async: false,
            url: modToGet,
            dataType: "xml",
            success: parseProfileXml,            
            error: tgerror2,
            cache: false
       });   
}

function tgerror2(xreq,tstat,err)
{
//alert(tstat);
}


function parseProfileXml(xml)
{

    
          feed = xml;
          $(xml).children("rss").children("channel").each(function () {
              var channelTitle = $(this).children("title").text();
              if (channelTitle == "Cell Feed") {
                  var itemiter = 1;
                  $(curHistoryList).empty();

                  if ($(this).children("item").length == 0 && curHLType == "History") {                      
                      var blankli;
                      blankli = "<li>Next time you browse the Career Centre website check here to find content you've recently accessed</li>";
                      $(curHistoryList).append(blankli);
                  }
                  else {
                      $(this).children("item").each(function () {
                          var tItems = parseInt($(curHistoryList).attr("maxitems"))

                          if (itemiter <= tItems) {
                              var xmlItemId = $(this).children("itemid").text();
                              var xmlTitle = $(this).children("title").text();
                              var xmlSummary = $(this).children("[nodeName=InceptionPoint.CellFeed:ShortSummary]").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 = $(curHistoryList).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\">"
                              }
                              var newli = "<li>";
                              newli += linkstub;

                              newli += xmlImag;
                              newli += "<span class=\"ContentListTitle\">" + xmlTitle + "</span>";
                              newli += "<span class=\"ContentListSummary\">" + xmlSummary + "</span>";
                              newli += "</a>";
                              newli += "</li>";

                              if (xmlTitle != "") $(curHistoryList).append(newli);

                              //$("#"+cellid).html(linkstub+xmlImag+"<div class=\"thumbGridText\">"+xmlTitle+"</div></a>");
                          }

                          itemiter++;
                      });
                  }
              }
          });          
          
          $(curHistoryList).append("<li><br/><div hltype=\""+curHLType+"\" cellid=\""+HistoryListCellId+"\" profileid=\""+HistoryListProfileId+"\" class=\"morepopup contentlistmore\">More</div></li>");
          setupMorePopupLinks();
          if (window.setupLinkPopupLinks())
          {
             window.setupLinkPopupLinks();
          }
          
          if (window.setupArticlePopupLinks())
          {
              setupArticlePopupLinks()
          }
}
