﻿(function($) {

  var modalStatus = 0; // 0 closed, 1 open

  // ================== open modal window block =========================== 

  $.fn.openModalWindow = function(modalDivID, shadeDivID) {

    var launchModal = function(modalDivID, shadeDivID) {
      if (modalStatus == 0) {
        $("#" + shadeDivID).css({ "opacity": "0.4" });
        $("#" + shadeDivID).fadeIn("slow");
        $("#" + modalDivID).fadeIn("slow");
        modalStatus = 1;
      }
    }

    var positionModal = function(modalDivID) {

      var screenHeight = screen.height;
      var screenWidth = screen.width;
      var popupHeight = $("#" + modalDivID).height();
      var popupWidth = $("#" + modalDivID).width();

      switch (modalDivID) {
        case "EditMetadataDiv":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 30,
              //"top": screenHeight / 2 - popupHeight / 2 - 200,
              "left": screenWidth / 2 - popupWidth / 2 + 70
            });
          }
          break;
        case "SetLocationDiv":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 10,
              "left": screenWidth / 2 - popupWidth / 2 + 70
            });
          }
          break;
        case "EmbedOverlay":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 10,
              "left": screenWidth / 2 - popupWidth / 2 + 160
            });
          }
          break;
        case "DownloadFormatOverlay":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 10,
              "left": screenWidth / 2 - popupWidth / 2 + 70
            });
          }
          break;
        case "ShareOverlay":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 10,
              "left": screenWidth / 2 - popupWidth / 2 + 70
            });
          }
          break;
        case "EnterpriseCustomersLoginInstructions":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 50,
              "left": screenWidth / 2 - popupWidth / 2 + 130
            });
          }
          break;
        case "EnterpriseCustomersSignupInstructions":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 50,
              "left": screenWidth / 2 - popupWidth / 2
            });
          }
          break;
        case "GetLiveCastViaMobileInstructions":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 150,
              "left": (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) ? (screenWidth / 2 - popupWidth / 2 + 140) : (screenWidth / 2 - popupWidth / 2)
            });
          }
          break;
        case "Olympics2010DIV":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 50,
              "left": (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) ? (screenWidth / 2 - popupWidth / 2 + 140) : (screenWidth / 2 - popupWidth / 2)
            });
          }
          break;        
        case "BlackBerryInstructions":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 50,
              "left": (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) ? (screenWidth / 2 - popupWidth / 2 + 140) : (screenWidth / 2 - popupWidth / 2)
            });
          }
          break;
        case "NokiaESeriesInstructions":
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": 50,
              "left": (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) ? (screenWidth / 2 - popupWidth / 2 + 140) : (screenWidth / 2 - popupWidth / 2)
            });
          }
          break;
        default:
          {
            $("#" + modalDivID).css({
              "position": "absolute",
              "top": screenHeight / 2 - popupHeight / 2,
              "left": screenWidth / 2 - popupWidth / 2
            });
          }

      }

      // we need this for IE
      if (screenHeight >= 1000) {
        $("#" + shadeDivID).css({ "height": screenHeight, "width": screenWidth });
      }
      else {
        $("#" + shadeDivID).css({ "height": 1000, "width": screenWidth });
      }
    }

    launchModal(modalDivID, shadeDivID);
    positionModal(modalDivID);

  }; //end function openModalWindow


  // public call
  $.openModalWindow = function(modalDivID, shadeDivID) { $.fn.openModalWindow(modalDivID, shadeDivID); };

  // =============== end open modal window block ==========================




  // =============== close modal window block ==========================

  // function closeModalWindow
  $.fn.closeModalWindow = function(modalDivID, shadeDivID) {

    var dissolveModal = function(modalDivID, shadeDivID) {
      if (modalStatus == 1) {
        $("#" + shadeDivID).fadeOut("slow");
        $("#" + modalDivID).fadeOut("slow");
        modalStatus = 0;
      }
    } // end if

    dissolveModal(modalDivID, shadeDivID);

  }; //end function closeModalWindow

  // public call
  $.closeModalWindow = function(modalDivID, shadeDivID) { $.fn.closeModalWindow(modalDivID, shadeDivID); };

  // =============== end close modal window block ==========================


})(jQuery);


