﻿var zlinkmousex;
var zlinkmousey;

var canhide=true;
var showElement;
var zltopoff;
var zlleftoff;
var elementtop;
var elementleft;




function setupLinkPopupLinks()
{
    $("body").append($("#zLinkPopup"));
    
    $(".zLinkStub").unbind('mouseenter',handleLinkPopup);
    $(".zLinkStub").mouseenter(handleLinkPopup);
    
    $(".zLinkStub").unbind('mouseleave',handleLinkPopupHide);
    $(".zLinkStub").mouseleave(handleLinkPopupHide);
    
    $(".zLinkStub").unbind('mousemove',handleLinkPopup);
    $(".zLinkStub").mousemove(handleLinkPopup);
    
    $(".zLinkStub").unbind('click',handleLinkClick);
    $(".zLinkStub").click(handleLinkClick);
    
    $(".zLinkPopupDiv").unbind('mouseenter',handlePopupHover);
    $(".zLinkPopupDiv").mouseenter(handlePopupHover);
 //   $(".zLinkPopupDiv").unbind('mouseleave',zLinkPopupHide);
 //   $(".zLinkPopupDiv").mouseenter(zLinkPopupHide);
    
   
}




function handlePopupHover(evt)
{
    canhide = false;
}

function handleLinkPopupHide(evt)
{
  
   canhide=true;
   setTimeout(delayLinkHide,100);
   
}

function delayLinkHide()
{

    if (canhide==true)
    {
        canhide = true;
        $("#zLinkPopup").hide();  
         
    }
    
}

function handleLinkClick(evt)
{
    var popupitemid =  $(this).attr("linkItemId");          
    
    followPopupLink(popupitemid);
}

function followPopupLink(popupitemid)
{
  
   
        var modToGet  = LinksCtlPath+"GetzLinks.aspx?cmd=GetLink&ModuleId="+LinksModuleId+"&ItemId="+popupitemid+"&basepath="+DNNBasePath
     
        $.ajax({
                type: "GET",
                url: modToGet,
                dataType: "xml",
                success: followSingleLinkFromFeed, 
              //  error: function(req,stat,er){alert(stat);},  
                cache: false
                
           });   
    
}


function handleLinkPopup(evt)
{
    
     if ( $(".zLinkPopupDiv").is(":hidden") ||  $(this).attr("linkItemId") !=showElement  ) 
    {
        
        showElement = $(this).attr("linkItemId");
       
        zlinkmousex = evt.pageX  ;            
        zlinkmousey=evt.pageY ;  
        var elmheight;
        var elmwidth;
        elmwidth = $(this).width();
        elmheight = $(this).height();
        elementtop= $(this).offset().top+(elmheight /2);
        elementleft = $(this).offset().left + (elmwidth / 3);
        
       
            
        //elementtop = zlinkmousey - elementtop;
        //elementleft = zlinkmousex - elementleft;
        
        
        var popupitemid =  $(this).attr("linkItemId");   
        
        
        $("#zLinkPopupTitle").html("");
          $("#zLinkPopupImage").html("");      
          $("#zLinkPopupUrl").html("");   
          $("#zLinkPopupDescription").html("<img src=\"images/ajax-loader.gif\" />");                
          showzLinkPopup();
          
        getPopupLink(popupitemid);
    }
    else
    {
        //canhide = false;
    }
    
}

function getPopupLink(popupitemid)
{
  
   
    var modToGet  = LinksCtlPath+"GetzLinks.aspx?cmd=GetLink&ModuleId="+LinksModuleId+"&ItemId="+popupitemid+"&basepath="+DNNBasePath
 
    $.ajax({
            type: "GET",
            url: modToGet,
            dataType: "xml",
            success: showSingleLinkFromFeed, 
           // error: function(req,stat,er){alert(stat);},  
            cache: false
            
       });   
}


function followSingleLinkFromFeed(xml) {

    
    var xmlModuleId;
    feed = xml;
    $(xml).children("rss").children("channel").each(function () {
        var channelTitle = $(this).children("title").text();
        if (channelTitle == "zLinks") {
            xmlModuleId = $(this).children("moduleid").text();
            $(this).children("item").each(function () {
                var url = $(this).children("link").text();
                if (isNumber(url) == false)
                    window.open(url);
                else {
                    if (url == "26") {
                        if (window.handleSubmitPostingPopup) window.handleSubmitPostingPopup();
                    }
                    else if (url == "9999") {
                        if (window.handlePostingListPopup) window.handlePostingListPopup();
                    }
                    else {

                        //if (window.getPopupArticle) window.getPopupArticle(url);
                        if (window.handleArticlePopupById) window.handleArticlePopupById(url)
                    }
                }
            });
        }
    });
}


function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function showSingleLinkFromFeed(xml)
{



      var xmlModuleId;
      feed = xml;
      $(xml).children("rss").children("channel").each(function () {
          var channelTitle = $(this).children("title").text();

          if (channelTitle == "zLinks") {

              xmlModuleId = $(this).children("moduleid").text();
              $(this).children("item").each(function () {
                  var isArticleLink = false;
                  var xmlItemId = $(this).children("itemid").text();
                  var desc = $(this).children("description").text();
                  $("#zLinkPopupDescription").html("");
                  $("#zLinkPopupDescription").append(desc);
                  var title = $(this).children("title").text();
                  var url = $(this).children("link").text();
                  var linkArticleItemId = "";
                  if (isNumber(url)) {
                      isArticleLink = true;
                      linkArticleItemId = url;
                  }

                  $("#zLinkPopupTitle").html("");
                  var imageElement = $(this).children("*|image")
                  var image = $(imageElement).children("url").text();
                  if (!isArticleLink) {
                      $("#zLinkPopupTitle").append("<a target=\"_new\" profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\" class=\"zLinkUrl profileCollect\" href=\"" + url + "\">" + title + "</a>");
                      $("#zLinkPopupImage").html("");
                      $("#zLinkPopupImage").html("<a target=\"_new\"  profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\"  class=\"zLinkUrl profileCollect\" href=\"" + url + "\"><img class=\"zlinkPopupImage\" src=\"" + image + "\"></a>");
                      $("#zLinkPopupUrl").html("");
                      $("#zLinkPopupUrl").html("<a target=\"_new\"  profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\"  class=\"zLinkUrl profileCollect\" target=\"_new\" href=\"" + url + "\">" + url + "</a>");
                  }
                  else {
                      var popClass = "articlePopupLink";
                      if (linkArticleItemId == "26") popClass = "SubmitPostingPopup";
                      if (linkArticleItemId == "9999") {
                          popClass = "postingListPopupLink";
                          //alert(popClass);
                      }
                      $("#zLinkPopupTitle").append("<a profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\" articleitemid=\"" + linkArticleItemId + "\" class=\"" + popClass + " profileCollect\" >" + title + "</a>");
                      $("#zLinkPopupImage").html("");
                      $("#zLinkPopupImage").html("<a  profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\" articleitemid=\"" + linkArticleItemId + "\" class=\"" + popClass + " profileCollect\" ><img class=\"zlinkPopupImage\" src=\"" + image + "\"></a>");
                      $("#zLinkPopupUrl").html("");
                      $("#zLinkPopupUrl").html("<a profilerModuleId=\"" + xmlModuleId + "\" profilerItemId=\"" + xmlItemId + "\" articleitemid=\"" + linkArticleItemId + "\"  class=\"" + popClass + " profileCollect\" >" + title + "</a>");
                  }
                  $("#zLinkPopup").attr("profilerModuleId", xmlModuleId);
                  $("#zLinkPopup").attr("profilerItemId", xmlItemId);
                  $("#zLinkMore").attr("profileid", zLinksProfileId);
                  $("#zLinkMore").attr("moreType", "zLink");
                  $("#zLinkMore").attr("moduleid", xmlModuleId);
                  $("#zLinkMore").attr("sourceTitle", title);
                  $("#zLinkMore").attr("moduleitemid", xmlItemId);
              });
          }
      });
         if (window.setProfileCollect)
         {
           window.setProfileCollect();
         }

         setzLinkProfileCapture();

         if (window.setupPostingPopupLinks) window.setupPostingPopupLinks();
         if (window.setupArticlePopupLinks) window.setupArticlePopupLinks();
       //  showzLinkPopup();                    
      
}

function setzLinkProfileCapture()
{
  $(".zLinkUrl").unbind("click",zLinkClick);
  $(".zLinkUrl").click(zLinkClick);
}

function zLinkClick(e)
{
   //e.preventDefault(); 
   
}

function showzLinkPopup()
{

  //  if (window.collectProfileItem)
  //  {               
       //collectProfileItem($("#zLinkPopup"));
  //  }
  
    var topval = zlinkmousey - ( $("#zLinkPopup").height()/2);
    
   
   // $("#zLinkPopup").css({top:zlinkmousey-10,left:zlinkmousex-10});
    $("#zLinkPopup").css({ top: elementtop, left: elementleft });

    var winWidth = $(window).width();
    
    
    $("#zLinkPopup").show();
    if ($("#zLinkPopup").width() + $("#zLinkPopup").offset().left > winWidth) {
        var newOffset = $("#zLinkPopup").offset().left - (($("#zLinkPopup").width() + $("#zLinkPopup").offset().left) - winWidth) - 20;
        $("#zLinkPopup").css({ left: newOffset });
    }
        //$("#zLinkPopup").css("left",(offset().left = $("#zLinkPopup").offset().left - (($("#zLinkPopup").width() + $("#zLinkPopup").offset().left) - winWidth)).toString());
      //$("#zLinkPopup").offset().left = $("#zLinkPopup").offset().left - (($("#zLinkPopup").width() + $("#zLinkPopup").offset().left) - winWidth )
    $("#zLinkPopup").unbind("mouseleave",zLinkPopupHide);
    $("#zLinkPopup").mouseleave(zLinkPopupHide);
}


function zLinkPopupHide(){
    canhide=true;
   setTimeout(delayLinkHide,100);
}

