//SETTING UP GENERIC POPUPS
var currentPopupId = "";
var currentPopupObj = "";
var popupIsOpen = false;

function showPopupBackground() {
  var background = jQuery("<div />")
    .attr("id", "popupBackground")
    .css({
      "opacity": 0.7,
      "height": jQuery(window).height(),
      "background-image": "url(data/loading_icon_popup.gif)",
      "background-repeat": "no-repeat",
      "background-attachment": "fixed",
      "background-position": "50% 50%"
    })
    .appendTo("body")
    .fadeIn("fast");
}

function hidePopupBackground() {
  jQuery("#popupBackground")
    .fadeOut("fast", function() {
      jQuery(this).remove();
    });
}

function showPopup(popupId) {
  if(popupId == "popupSubscribe") {
    showPopupAjax(14927, "", function() {
      disableRegistrationEnter();
    });
    return;
  }
  //loads popup only if it is disabled
  var popupTop = Math.round(jQuery(window).height()/2 - jQuery("#"+popupId).height()/2 + jQuery(document).scrollTop());
  if(popupTop<0) {
    popupTop = 0;
  }
  var popupLeft = Math.round(jQuery(window).width()/2 - jQuery("#"+popupId).width()/2 + jQuery(document).scrollLeft());
  if(popupLeft<0) {
    popupLeft = 0;
  }
  if(currentPopupId != popupId) {
    showPopupBackground();
    jQuery("#"+popupId)
      .css({
        "position": "absolute",
        "top": popupTop+"px",
        "left": popupLeft+"px"
      })
      .fadeIn("fast");
    currentPopupId = popupId;
    popupIsOpen = true;
  }
}

function showPopupAjax(componentId, urlParameters, onReadyCallback) {
  //loads popup only if it is disabled
  if(!popupIsOpen) {
    popupIsOpen = true;
    showPopupBackground();
    var url = "index.php?article_id=" + componentId;
    if(typeof urlParameters == "object") {
      for(var i in urlParameters) {
        url += "&" + i + "=" + urlParameters[i];
      }
    }
    jQuery.ajax({
      url: url,
      type: "POST",
      timeout: 60000,
      error: function(){
        alert("Error loading the popup!\nThe connection to the server was too slow.\n\nPlease try again.");
        hidePopup();
      },
      success: function(response) {
        jQuery("#popupBackground")
          .css({
            "background-image": "none"
          });
        var box = jQuery("<div />")
          .addClass("popupBox")
          .append(response)
          .appendTo("body");
        var popupTop = Math.round(jQuery(window).height()/2 - box.height()/2 + jQuery(document).scrollTop());
        if(popupTop<0) {
          popupTop = 0;
        }
        var popupLeft = Math.round(jQuery(window).width()/2 - box.width()/2 + jQuery(document).scrollLeft());
        if(popupLeft<0) {
          popupLeft = 0;
        }
        box
          .css({
            "position": "absolute",
            "top": popupTop+"px",
            "left": popupLeft+"px"
          })
          .fadeIn("fast");
        currentPopupObj = box;
        if(jQuery.isFunction(onReadyCallback)) {
          onReadyCallback();
        }
      }
    });
  }
}

function showPopupHtml(html) {
  //loads popup only if it is disabled
  if(!popupIsOpen) {
    popupIsOpen = true;
    showPopupBackground();

    jQuery("#popupBackground")
      .css({
        "background-image": "none"
      });
    var box = jQuery("<div />")
      .addClass("popupBox")
      .append(html)
      .appendTo("body");
    var popupTop = Math.round(jQuery(window).height()/2 - box.height()/2 + jQuery(document).scrollTop());
    if(popupTop < 0) {
      popupTop = 0;
    }
    var popupLeft = Math.round(jQuery(window).width()/2 - box.width()/2 + jQuery(document).scrollLeft());
    if(popupLeft < 0) {
      popupLeft = 0;
    }
    box
      .css({
        "position": "absolute",
        "top": popupTop + "px",
        "left": popupLeft + "px"
      })
      .fadeIn("fast");
    currentPopupObj = box;
  }
}

function hidePopup() {
  hidePopupBackground();
  if(currentPopupId != "") {
    jQuery("#"+currentPopupId)
      .fadeOut("fast");
    currentPopupId = "";
  }
  if(typeof currentPopupObj == "object") {
    currentPopupObj
      .fadeOut("fast")
      .remove();
    currentPopupObj = "";
  }
  popupIsOpen = false;
}

function resizePopup(box) {
  var popupTop = Math.round(jQuery(window).height()/2 - box.height()/2);
  if(popupTop<0) {
    popupTop = 0;
  }
  var popupLeft = Math.round(jQuery(window).width()/2 - box.width()/2);
  if(popupLeft<0) {
    popupLeft = 0;
  }
  box
    .css({
      "top": popupTop+"px",
      "left": popupLeft+"px"
    });
}

jQuery(document).ready(function() {
  jQuery(document).keypress(function(e) {
    if(e.keyCode==27 && currentPopupId != "") {
      hidePopup();
    }
  });
});
