﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//loading popup with jQuery magic!

/*************************************************
// How to use this dynamic popup.
-Add two below files
------ <script language="javascript" src="../../includes/javascript/DynamicPopup.js" type="text/javascript"></script>
------ <link rel="Stylesheet" type="text/css" href="../../includes/stylesheets/DynamicPopup.css" />
- Add a popup div, which will contain a cross button to close the popup 
  add class="DynamicPopupClose" in that hyperlink like:
  ----- <a id="CloseAddConditionPopup" class="DynamicPopupClose"><img src='../../Images/cross.gif' /> </a>
- Add one blank div which will be shown as background. Add class = "DynamicPopupBackground" in that div. like below:
  ----- <div id="AddConditionBackgroundPopup" class="DynamicPopupBackground"> </div>
- to open the popup you should call below function:
   ---- DynamicShowPopup(PassedPopupId, PassedBackgroundPopupId, PassedClosedButtonId)
      - Above function accept three parameters PassedPopupId, PassedBackgroundPopupId and PassedClosedButtonId
- to close the popup  you should call below function
   ----- CloseDynamicPopup(PopupId, BackgroundPopupId)
      - Above function takes 2 parameters PopupId and BackgroundPopupId
      
 @@@@ >>>> For reference you can see below listed pages where this popup is used.
            1) MemberHome.aspx [It contains 3 popup]
            2) FaxRecords.aspx [It contains 1 popup]
            3) MedicalCondition.aspx [It contains 1 popup]

*************************************************/

function DynamicShowPopup(PassedPopupId, PassedBackgroundPopupId, PassedClosedButtonId)
{
    //centering with css
    DynamicCenterPopup(PassedPopupId, PassedBackgroundPopupId);
     
    //load popup
    DynamicLoadPopup(PassedPopupId, PassedBackgroundPopupId);  

    $("#" + PassedClosedButtonId).click(function()
    {
        CloseDynamicPopup(PassedPopupId, PassedBackgroundPopupId);
    });

    $(document).keypress(function(e)
    {
        if (e.keyCode == 27)
        {
            CloseDynamicPopup(PassedPopupId, PassedBackgroundPopupId);
        }
    });
}

function DynamicLoadPopup(PopupId, BackgroundPopupId)
{
    $("#" + BackgroundPopupId).css({
        "opacity": "0.3"
    });
    $("#" + BackgroundPopupId).fadeIn("fast");
    $("#" + PopupId).fadeIn("fast");
}

function DynamicShowPopupWithOpacity(PassedPopupId, PassedBackgroundPopupId, PassedClosedButtonId, PassedOpacity)
{
    //centering with css
    DynamicCenterPopup(PassedPopupId, PassedBackgroundPopupId);
    
    //load popup
    DynamicLoadPopupWithOpacity(PassedPopupId, PassedBackgroundPopupId, PassedOpacity);

    $("#" + PassedClosedButtonId).click(function()
    {
        CloseDynamicPopup(PassedPopupId, PassedBackgroundPopupId);
    });

    $(document).keypress(function(e)
    {
        if (e.keyCode == 27)
        {
            CloseDynamicPopup(PassedPopupId, PassedBackgroundPopupId);
        }
    });
}

function DynamicLoadPopupWithOpacity(PopupId, BackgroundPopupId, Opacity)
{

    $("#" + BackgroundPopupId).css({
        "opacity": Opacity
    });
    $("#" + BackgroundPopupId).fadeIn("fast");
    $("#" + PopupId).fadeIn("fast");
}

//disabling popup with jQuery magic!
function CloseDynamicPopup(PopupId, BackgroundPopupId)
{
    $("#" + BackgroundPopupId).fadeOut("slow");
    $("#" + PopupId).fadeOut("slow");
}

//centering popup
function DynamicCenterPopup(PopupId, BackgroundPopupId) {
    //$("#" + PopupId).height("500px"); 
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight ;
    var popupHeight = $("#" + PopupId).height();
    var popupWidth = $("#" + PopupId).width();

    var parentHeight = $("#" + PopupId).parent().height();
    var clientHeight = $("#" + PopupId).height();
    var scrollTop = $("#" + PopupId).parent().scrollTop();

    var parentWidth = $("#" + PopupId).parent().width();
    var clientWidth = $("#" + PopupId).width();
    var scrollLeft = $("#" + PopupId).parent().scrollLeft();

    //var top = (windowHeight / 2) - (clientHeight / 2) + scrollTop;   
   //alert("n :clientHeight : " + clientHeight + " windowHeight : " + windowHeight + " top= " + top);
////    //centering
    if (clientHeight >= (windowHeight - 50))
    {       
        $("#" + PopupId).height(windowHeight - 500);            
           
        $("#" + PopupId).css({
            "position": "fixed",
            "top": 10 + scrollTop,
            "left": (windowWidth / 2) - (clientWidth / 2) + scrollLeft

        });
    }
    else
    {        
        $("#" + PopupId).css({
        
            "position": "fixed",
            "top": (windowHeight / 2) - (clientHeight / 2) + scrollTop,
            "left": (windowWidth / 2) - (clientWidth / 2) + scrollLeft

       });
    }

    //only need force for IE6
    $("#" + BackgroundPopupId).css({
        "height": windowHeight
    });

}

function DynamicResetPostion(PopupId)
{
    $("#" + PopupId).height("auto"); 
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#" + PopupId).height();
    var popupWidth = $("#" + PopupId).width();

    var parentHeight = $("#" + PopupId).parent().height();
    var clientHeight = $("#" + PopupId).height();
    var scrollTop = $("#" + PopupId).parent().scrollTop();

    var parentWidth = $("#" + PopupId).parent().width();
    var clientWidth = $("#" + PopupId).width();
    var scrollLeft = $("#" + PopupId).parent().scrollLeft();

    //centering
    if (clientHeight >= (windowHeight - 50)) {      
        $("#" + PopupId).height(windowHeight - 100);

        $("#" + PopupId).css({
            "position": "fixed",
            "top": 10 + scrollTop,
            "left": (windowWidth / 2) - (clientWidth / 2) + scrollLeft

        });
    }
    else {
        $("#" + PopupId).css({

            "position": "fixed",
            "top": (windowHeight / 2) - (clientHeight / 2) + scrollTop,
            "left": (windowWidth / 2) - (clientWidth / 2) + scrollLeft

        });
    }
}
