var modalHeight = 0;
var modalWidth = 0;
var includeCloseButton = true;
var modalBackgroundDiv = "page_overlay";
var modalPopUpDiv = "page_popup";
var modalPopUpContentDiv = "page_popup_content";

(function ($) {
    $.fn.extend({
        center: function () {
            return this.each(function () {
                //var top = ($(window).scrollTop() - $(this).outerHeight()) / 2;
                var top = (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop();
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({ position: 'absolute', margin: 0, top: (top > 0 ? top : 0) + 'px', left: (left > 0 ? left : 0) + 'px' });
            });
        }
    });
})(jQuery);

var ModalPopup = (function () {

    var modalPopup = {};

    modalPopup.dontAskAgain = false;

    modalPopup.onCancel = function () { };
    modalPopup.onOk = function () { };

    modalPopup.setHeight = function (iHeight) {
        modalHeight = iHeight;
        if (modalHeight > 0) {
            $('#' + modalPopUpDiv).height(modalHeight);
        }
    };
    modalPopup.setWidth = function (iWidth) {
        modalWidth = iWidth;
        if (modalWidth > 0) {
            $('#' + modalPopUpDiv).width(modalWidth);
        }
    };

    modalPopup.show = function (divID) {

        if (modalPopup.dontAskAgain) {
            return;
        }

        $('#' + modalBackgroundDiv).show();
        $('#' + modalPopUpContentDiv).html($('#' + divID).html());
        $('#' + modalPopUpDiv).show();
        $('#' + modalPopUpContentDiv).show();

        if (!includeCloseButton) {
            $('#modalCloseButton').css('display', 'none');
        }
        //if they click out side the pop-up content, close it
        $('#' + modalBackgroundDiv).bind('click', function() {
          modalPopup.hide();
        });
    };

    modalPopup.hide = function () {

        $('#' + modalBackgroundDiv).hide();
        $('#' + modalPopUpContentDiv).html('');
        $('#' + modalPopUpDiv).hide();
        try {
            $('#QapTcha').html('');
        } catch (e) { }
    };

    modalPopup.okClicked = function () {

        modalPopup.dontAskAgain = true;
        modalPopup.onOk();
        modalPopup.hide();
    };

    modalPopup.center = function () {
        $('#' + modalPopUpDiv).center();
    };

    modalPopup.hideControls = function () {
        includeCloseButton = false;
    };

    modalPopup.cancelClicked = function () {
        modalPopup.dontAskAgain = true;
        modalPopup.onCancel();
        modalPopup.hide();
    };

    return modalPopup;
})();

