﻿
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function loadPopup(popupTitle,popupWidth,popupHeight,popupIframeSrc) {
  //loads popup only if it is disabled
  if (popupStatus == 0) {
    //set title
   
    //set width
    if (popupWidth != '') {
      $("#popup").css({ width: popupWidth + "px" });
    }
    //set height
    if (popupHeight != '') {
      $("#popup").css({ height: popupHeight + "px" });
    }
    //control iframe
    if (popupIframeSrc != '') {
      $("#popupIframe").css({ display: "block" });
      $("#popupIframe").css({ width: $("#popup").width() - 24 + "px" });
      $("#popupIframe").css({ height: $("#popup").height() - 60 + "px" });
      //$("#popupIframe").location = popupIframeSrc;
      $('#popupIframe').attr('src', popupIframeSrc);
      //window.frames["popupIframe"].location.reload();
     
      //hide the copy container
      $("#popupCopy").css({ display: "none" });
      
    }

    $("#backgroundPopup").css({
      "opacity": "0.7"
    });
    $("#backgroundPopup").fadeIn("slow");
    $("#popup").fadeIn("slow");
    popupStatus = 1;

    //need to load the title after the popup is set to display:block ($("#popup").fadeIn("slow");)
    if (popupTitle != '') {
      $("#popupTitle").html(popupTitle);
    }
  }
}

//disabling popup 
function disablePopup() {
  //disables popup only if it is enabled
  if (popupStatus == 1) {
    $("#backgroundPopup").fadeOut("slow");
    $("#popup").fadeOut("slow");
    popupStatus = 0;
  }
}

//centering popup
function centerPopup() {
  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = $("#popup").height();
  var popupWidth = $("#popup").width();
  //centering
  $("#popup").css({
    "position": "absolute",
    "top": windowHeight / 2 - popupHeight / 2 + $(window).scrollTop(),
    "left": windowWidth / 2 - popupWidth / 2
  });
  //only need force windowheight for IE6
  if ($.browser.msie & $.browser.version == '6.0') {
   windowHeight = $("body").outerHeight() 
  }

  $("#backgroundPopup").css({
    "height": windowHeight
  });

}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {

  //LOADING POPUP
  //Click the button event!
  $("#button").click(function () {
    //centering with css
    centerPopup();
    //load popup
    loadPopup();
  });

  //CLOSING POPUP
  //Click the x event!
  $("#popupClose").click(function () {
    disablePopup();
  });
  //Click the close event!
  $("#popupClose2").click(function () {
    disablePopup();
  });
  //Click out event!
  $("#backgroundPopup").click(function () {
    disablePopup();
  });
  //Press Escape event!
  $(document).keypress(function (e) {
    if (e.keyCode == 27 && popupStatus == 1) {
      disablePopup();
    }
  });

});
