var prettyGalleryWebBit_currentPopupObj = "";


function prettyGalleryWebBit_showPopupBackground(imageURL) {
  var background = jQuery("<div />").attr("id", "prettyGalleryWebBit_popupBackground").css({"opacity": 0.7, "height": jQuery(window).height(), "background-image": "url(imageURL)", "background-repeat": "no-repeat", "background-attachment": "fixed", "background-position": "50% 50%" }).appendTo("body").fadeIn("fast");
}


function prettyGalleryWebBit_hidePopupBackground() {
  jQuery("#prettyGalleryWebBit_popupBackground").fadeOut("fast", function() {
    jQuery(this).remove();
  });
}


function prettyGalleryWebBit_showPopup(componentId, urlParameters, imageURL) {
  prettyGalleryWebBit_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.");
      prettyGalleryWebBit_hidePopup();
    },
    success: function(response) {
      jQuery("#prettyGalleryWebBit_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");
      prettyGalleryWebBit_currentPopupObj = box;
    }
  });
}


function prettyGalleryWebBit_hidePopup() {
  prettyGalleryWebBit_hidePopupBackground();
  if(typeof prettyGalleryWebBit_currentPopupObj == "object") {
    prettyGalleryWebBit_currentPopupObj.fadeOut("fast").remove();
    prettyGalleryWebBit_currentPopupObj = "";
  }
}


