﻿
var curPopupItemId
var articlePopped = false;
var documentTitle;
var ArticleTitle;
var socialShareText;

socialShareText = "<!-- AddThis Button BEGIN --><div class=\"addthis_toolbox addthis_default_style \"><a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a><a class=\"addthis_button_tweet\"></a><a class=\"addthis_button_google_plusone\" g:plusone:size=\"medium\"></a><a class=\"addthis_counter addthis_pill_style\"></a></div> <script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e821891379e08da\"><";
socialShareText+="/script><!-- AddThis Button END -->";


function popArticle(articleid)
{
    $(document).ready(function() { 
        handleArticlePopupById(articleid);
    });
}

function setGPlus () {
    gapi.plusone.go();   
}

function setupArticlePopupLinks()
{

    $(".articlePopupLink").unbind('click',handleArticlePopup);
    $(".articlePopupLink").click(handleArticlePopup);
}

function handleArticlePopup()
{
    if (morePopped) $(".MorePopupDiv").hide();
    $("#mainArticleTitle").html("");
    $("#mainArticlePopup").css({top:($(window).scrollTop()+50)+"px"}).show(); 
    articlePopped = true; 
      
    $("#mainArticlePopup").show();
    $("#mainArticleContent").html("<img src=\"images/ajax-loader.gif\" />");
   // var popupitemid =  $(this).attr("postingItemId");   
    //var popupitemid =  $(this).attr("profilerItemId");   
    var popupitemid = $(this).attr("articleitemid");   

    getPopupArticle(popupitemid);
    
}

function handleArticlePopupById(articleid)
{
    if (morePopped) $(".MorePopupDiv").hide();
    $("#mainArticleTitle").html("");
     $("#mainArticlePopup").css({top:($(window).scrollTop()+50)+"px"}).show();  
      articlePopped = true;
    $("#mainArticlePopup").show();
    $("#mainArticleContent").html("<img src=\"images/ajax-loader.gif\" />");
    
    
    getPopupArticle(articleid);
}

function getPopupArticle(popupitemid)
{

    if (popupitemid == "9999")
    {    
       $("#mainArticlePopup").hide();
       if (window.handlePostingListPopup) window.handlePostingListPopup() ;
    }
    else
    {
        curPopupItemId = popupitemid;
    
        var modToGet  = articleCtlPath+"GetArticle.aspx?cmd=GetArticle&ModuleId="+articleModuleId+"&ItemId="+popupitemid+"&basepath="+DNNBasePath
    
        $.ajax({
            type: "GET",
                async: true,
                url: modToGet,
                dataType: "xml",
                success: showSingleArticleFromFeed,                       
                error: function(req,stat,er){},
                cache: false
            
           });   
    }
}



function showSingleArticleFromFeed(xml)
{
    
      var xmlModuleId;
      feed = xml;
      $(xml).children("rss").children("channel").each(function () {
          var channelTitle = $(this).children("title").text();
          if (channelTitle == "Articles") {
              xmlModuleId = $(this).children("moduleid").text();
              $(this).children("item").each(function () {


                  var xmlItemId = $(this).children("itemid").text();
                  var imageElement = $(this).children("*|image")
                  var image = $(imageElement).children("url").text();
                  ArticleTitle = $(this).children("title").text();
                  var desc = $(this).children("description").text();

                  $("#mainArticleContent").html("");
                  //$("#mainArticleContent").append("<img class=\"popupImage\" src=\""+image+"\" >"+desc);                                                         


                  $("#mainArticleContent").append(desc);

                  var title = $(this).children("title").text();

                  $("#mainArticleTitle").html("");

                  $("#mainArticleTitle").append(title);
                  $("#mainArticlePopup").attr("profilerModuleId", xmlModuleId);
                  $("#mainArticlePopup").attr("profilerItemId", xmlItemId);



              });
              
              showSimilarItems();
          }
      });
      
         showArticlePopup();                    
           
}

function showSimilarItems()
{
    
   var modToGet

   modToGet = moreCtlPath + "getContentCellFeed.aspx?profileid=" + articleProfileId + "&contentType=Similar&moduleid=" + articleModuleId + "&moduleitemid=" + curPopupItemId + "&pageSize=16&pageNum=1&basePath=" + moreDNNBasePath;
   
   
      
      
    //alert(modToGet);
    $.ajax({
            type: "GET",
            async: false,
            url: modToGet,
            dataType: "xml",
            success: parseSimilarStubThumbGridXml,                       
            cache: false
            
       });   
}


function parseSimilarStubThumbGridXml(xml)
{   

          feed = xml;



          $(xml).children("rss").children("channel").each(function () {

              var channelTitle = $(this).children("title").text();

              var cellTitle = $(this).children("[nodeName=InceptionPoint.CellFeed:CellTitle]").text();

              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 (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);
                          if (xmlTitle != "") {
                              //var tagMatch = $(this).children("[nodeName=InceptionPoint.CellFeed:FirstMatchedTag]").text();
                              var tagMatch = "";
                              var xmlImag = $(this).children("[nodeName=InceptionPoint.CellFeed:Image]").text();
                              xmlImag = xmlImag.replace("//", "/");
                              xmlImage = xmlImag.replace("http:/", "http://");
                              var xmlItemType = $(this).children("[nodeName=InceptionPoint.CellFeed:ItemType]").text();
                              var xmlItemModuleId = $(this).children("[nodeName=InceptionPoint.CellFeed:ItemModuleId]").text();
                              //var cellid = $(curSTGId).attr("id")                   
                              //if (xmlItemModuleId != articleModuleId && curPopupItemId != xmlItemId) {
                                  gridhtm += "<td class=\"StubThumbSpacer\"></td><td class=\"StubThumbGridContentCell\" id=\"StubThumbGridMoreCell" + itemiter.toString() + "\">";
                                  gridhtm += "<div class=\"StubThumbOuterdiv\">";
                                  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 += linkstub + "<div class=\"stubThumbGridTag\">" + tagMatch + "</div>" + xmlImag + "<div class=\"stubThumbGridTitleText\">" + xmlTitle + "</div><div class=\"stubThumbGridText\">" + xmlDescription + "</div></a>";
                                  gridhtm += "</div>";

                                  gridhtm += "</td>";
                                  curcol++;
                              //}
                              //else
                              //    itemiter--;
                          }
                      }
                      itemiter++;
                  });
                  gridhtm += "</tr><tr><td colspan=\"8\">";
                  gridhtm += "<br/><div  profileid=\"" + articleProfileId + "\" moreType=\"article\" moduleid=\"" + articleModuleId + "\" moduleitemid=\"" + curPopupItemId + "\" sourceTitle=\"" + $("#mainArticleTitle").html() + "\" class=\"morepopup articleMore\">More</div>";
                  gridhtm += "</td></tr></table>";
                  $("#mainArticleSimilarContent").html(gridhtm);
                  if (window.setupMorePopupLinks()) {
                      setupMorePopupLinks();
                  }

              }




          });          
         
         
         
          
         
      
}




function showArticlePopup() {

    
    var hashStr = "showArticle=Module:" + articleModuleId + ",Article:" + curPopupItemId;
    $("#mainMorePopup").attr("hash", hashStr);
    $("#mainMorePopup").attr("title",ArticleTitle+" - "+documentTitle);
    document.title = ArticleTitle + " - "+ documentTitle ;
    window.location.hash = hashStr;
    if (window.collectProfileItem)
    {               
       collectProfileItem($("#mainArticlePopup"));
    }

   
    if (window.setupArticlePopupLinks()) 
    {
      setupArticlePopupLinks()
    }
    
    if (window.setupLinkPopupLinks())
    {
      window.setupLinkPopupLinks();
    }

  replaceLinks();
  
  if (window.setupPostingPopupLinks()) {      
      window.setupPostingPopupLinks();
  }

  $('.btn_fb').each(function () {      
      FB.XFBML.parse(this);
  });


  
  setGPlus();
  
  if (window.collectDiggs) window.collectDiggs();
}


function replaceLinks() {


    $("#mainArticlePopup").find("a").each(function () {
        if ($(this).attr("href") != undefined) {
            //find href in links database
            replaceLink(this,$(this).attr("href"));
            // if found replace href with pqcc link
        }

    });
    
    

    
}

var replaceAnchor;
function replaceLink(anchor,link) {
    replaceAnchor = anchor;
    modToGet = articleCtlPath + "FindzLink.aspx?link=" + link;
   
    $.ajax({
        type: "GET",
        async: false,
        url:modToGet ,
        dataType: "xml",
        success: parseFindLink,
        error: replError,
        cache: false

    });
}

function replError(jqXHR, textStatus, errorThrown) {
    //alert(textStatus);
}
function parseFindLink(xml) {
    
    if ($(xml).children("article").children().length != 0) {
        var modid = $(xml).children("article").children("moduleid").text();
        var itemid = $(xml).children("article").children("itemid").text();
        //$(replaceAnchor).attr("href", "");
        $(replaceAnchor).removeAttr("href");
        $(replaceAnchor).attr("profilerModuleId", modid);
        $(replaceAnchor).attr("profilerItemId", itemid);
        $(replaceAnchor).attr("linkitemid", itemid);
        $(replaceAnchor).attr("class", "profileCollect zLinkStub");
    }
   

    if (window.setupLinkPopupLinks()) {
        window.setupLinkPopupLinks();
    }

    
    
}

