﻿function kkpopup(_popUpButtonId, _popUpDivId, _onPopupShow) {
    var backgroundPopupId;
    var popUpDivId;
    var popupStatus;
    var hashcode;
    var popUpButtonId;
    var currentpopUpButtonId;
    var onPopupShow;
    function showPopup() {
        //loads popup only if it is disabled
        if (onPopupShow != null && onPopupShow != undefined) {
            onPopupShow();
        }
        if (popupStatus == 0) {
            $("#" + backgroundPopupId).css({
                "opacity": "0.7"
            });
            $("#" + backgroundPopupId).fadeIn("fast");
            $("#" + popUpDivId).fadeIn("fast");
            popupStatus = 1;
        }
    }
    function closePopup() {
        //disables popup only if it is enabled
        if (popupStatus == 1) {
            $("#" + backgroundPopupId).fadeOut("fast");
            $("#" + popUpDivId).fadeOut("fast");
            popupStatus = 0;
        }
        currentpopUpButtonId = "leeg";
    }
    function setCssClass(id, cssClass) {
        var element = document.getElementById(id);
        if (element != null) {
            element.setAttribute("class", cssClass);
            element.setAttribute("className", cssClass);
        }
    }
    function centerPopup() {
        //request data for centering  
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var newpopupHeight = $("#" + popUpDivId).height();
        var newpopupWidth = $("#" + popUpDivId).width();
        //centering
        $("#" + popUpDivId).css({
            "position": "absolute",
            "top": windowHeight / 2 - newpopupHeight / 2,
            "left": windowWidth / 2 - newpopupWidth / 2
        });
        //only need force for IE6
        $("#" + backgroundPopupId).css({
            "height": windowHeight
        });
    }
    function initPopUp(_popUpButtonId, _popUpDivId, _onPopupShow) {
        popUpButtonId = _popUpButtonId;
        popUpDivId = _popUpDivId;
        closeButtonId = 'closePopupButton' + _popUpDivId;
        backgroundPopupId = 'backgroundPopup';
        popupStatus = 0;
        onPopupShow = _onPopupShow;
        var divPopupBackground = document.getElementById(backgroundPopupId);
        if (divPopupBackground == null) {
            divPopupBackground = document.createElement("div");
            divPopupBackground.id = backgroundPopupId;
            document.body.appendChild(divPopupBackground);
            setCssClass(backgroundPopupId, "popUpScreenFiller");
        }
        var divPopup = document.getElementById(popUpDivId);
        if (divPopup != null && document.getElementById(closeButtonId) == null) {
            var closeButton = document.createElement("a");
            closeButton.innerHTML = "X";
            closeButton.id = closeButtonId;
            divPopup.insertBefore(closeButton, divPopup.firstChild);
            setCssClass(closeButtonId, "popUpCloseButton");
            //divPopup.appendChild(closeButton);
        }
        if (document.getElementById(popUpButtonId) != null && document.getElementById(popUpButtonId).disabled != true) {
            $("#" + popUpButtonId).click(function() {
                //centering with css
                currentpopUpButtonId = popUpButtonId;
                centerPopup();
                //load popup
                showPopup();
            });
            //CLOSING POPUP  
            //Click the x event!
            $("#" + closeButtonId).click(function() {
                if (currentpopUpButtonId == popUpButtonId) {
                    closePopup();
                }
            });
            //Click out event!
            $("#" + backgroundPopupId).click(function() {
                if (currentpopUpButtonId == popUpButtonId) {
                    closePopup();
                }
            });
            //Press Escape event!
            $(document).keypress(function(e) {
                if (currentpopUpButtonId == popUpButtonId) {
                    if (e.keyCode == 27 && popupStatus == 1) {
                        closePopup();
                    }
                }
            });
        }
    }
    initPopUp(_popUpButtonId, _popUpDivId, _onPopupShow);
}
