/**
* PopupModal class
*/

(function () {
    var modalSets = [];

    PopupModal = function (passedOptions) {
        this.options = jQuery.extend({
            tplDialog: false, 
            tplAlert: false, 
            tplConfirm: false
        }, passedOptions);

        if (!this.options.tplDialog) {
            this.options.tplDialog = '#PopupModalDialog';
        }
        this.$tplDialog = $(this.options.tplDialog);
        if (!this.$tplDialog.length) {
            this.$tplDialog = $('\
                <div id="PopupModalDialog">\
                    <div class="popupModalText"></div>\
                    <div class="popupModalButtons"></div>\
                </div>').css({display: 'none'}).appendTo('body');
        }

        if (!this.options.tplAlert) {
            this.options.tplAlert = '#PopupModalAlert';
        }
        this.$tplAlert = $(this.options.tplAlert);
        if (!this.$tplAlert.length) {
            this.$tplAlert = 
                $('<div id="PopupModalAlert">')
                .css({display: 'none'})
                .appendTo('body')
                .html($(this.options.tplDialog).html());
            this.$tplAlert.find('.popupModalButtons').append('<input type="button" value="Ok" class="popupModalOk">');
        }

        if (!this.options.tplConfirm) {
            this.options.tplConfirm = '#PopupModalConfirm';
        }
        this.$tplConfirm = $(this.options.tplConfirm);
        if (!this.$tplConfirm.length) {
            this.$tplConfirm = 
                $('<div id="PopupModalConfirm">')
                .css({display: 'none'})
                .appendTo('body')
                .html($(this.options.tplDialog).html());
            this.$tplConfirm.find('.popupModalButtons')
                .append('<input type="button" value="Yes" class="popupModalYes">')
                .append('<input type="button" value="No" class="popupModalNo">');
        }

        return this;
    }

    PopupModal.prototype.confirm = function(message, callbackYes, callbackNo, buttonYes, buttonNo) {
        var PopupModalInstance = this;
        var label = PopupModalInstance.popupShow(PopupModalInstance.options.tplConfirm);
        var buttonYesInstance = PopupModalInstance.$tplConfirm.find('.popupModalYes');
        if (buttonYes) {
            buttonYesInstance.val(buttonYes);
        }
        buttonYesInstance.unbind('click').bind('click', function () {
            PopupModalInstance.popupHide(label);
            if (callbackYes) {
                callbackYes();
            }
        });
        var buttonNoInstance = PopupModalInstance.$tplConfirm.find('.popupModalNo');
        if (buttonNo) {
            buttonNoInstance.val(buttonNo);
        }
        buttonNoInstance.unbind('click').bind('click', function () {
            PopupModalInstance.popupHide(label);
            if (callbackNo) {
                callbackNo();
            }
        });
        PopupModalInstance.$tplConfirm.find('.popupModalText').html(message);
    }

    PopupModal.prototype.alert = function(message, callbackOk, buttonOk) {
        var PopupModalInstance = this;
        var label = PopupModalInstance.popupShow(PopupModalInstance.options.tplAlert);
        var buttonOkInstance = PopupModalInstance.$tplAlert.find('.popupModalOk');
        if (buttonOk) {
            buttonOkInstance.val(buttonOk);
        }
        buttonOkInstance.unbind('click').bind('click', function () {
            PopupModalInstance.popupHide(label);
            if (callbackOk) {
                callbackOk();
            }
        });
        PopupModalInstance.$tplAlert.find('.popupModalText').html(message);
    }

    PopupModal.prototype.popupShow = function(selector, passedOptions, label) {

        if (!label) {
            var labelIndex = 0;
            for (var i in modalSets) {
                if (labelIndex <= modalSets[i]['labelIndex']) {
                    labelIndex = modalSets[i]['labelIndex'] + 1;
                }
            }
            label = '__PopupModalLabel' + labelIndex;
        }
        else {
            var labelIndex = false;
        }
        if (modalSets[label]) {
            var modalSet = modalSets[label];
        }
        else {
            var currentZIndex = 100;
            for (var i in modalSets) {
                if (currentZIndex <= modalSets[i]['zIndex']) {
                    currentZIndex = modalSets[i]['zIndex'] + 2;
                }
            }
            var modalSet = jQuery.extend({
                // internal data
                mask: false,
                content: false,
                zIndex: currentZIndex,
                labelIndex: labelIndex,

                // options
                easyClose: true,
                background: '#000',
                show: 200,
                hide: 200,
                contentContainerSelector: '.popupContent',
                maxHeight: 0.7,
                maxWidth: 0.7
            }, passedOptions);

            modalSets[label] = modalSet;
        }

        if (!modalSet['mask']) {
            modalSet['mask'] = jQuery('#PopupModalMask' + label);

            if (modalSet['mask'].length == 0) {
                modalSet['mask'] = jQuery('<div id="PopupModalMask' + label + '">').
                    css({
                        position: 'fixed',
                        left: 0,
                        top: 0,
                        zIndex: modalSet['zIndex'] + 1,
                        backgroundColor: modalSet.background,
                        display: 'none'
                    }).
                    appendTo("body");
            }

            var maskHeight = '100%';
            var maskWidth = '100%';

            modalSet['mask'].css({
                width: maskWidth,
                height: maskHeight
            });
            if (modalSet.show) {
                modalSet['mask'].stop(true).fadeTo(modalSet.show, 0.2);
            }
            else {
                modalSet['mask'].show().css({opacity: 0.2});
            }
        }

        // Content

        if (modalSet['content']) {
            this.contentHide(label);
        }
        modalSet['content'] = jQuery(selector);
        modalSet['content'].css({
            position: 'fixed',
            zIndex: modalSet['zIndex'] + 2
        });
        modalSet['contentContainer'] = $(selector + ' ' + modalSet['contentContainerSelector']);
        if (!modalSet['contentContainer'].length) {
            modalSet['contentContainer'] = modalSet['content'];
        }
        if (modalSet['maxHeight'] || modalSet['maxWidth']) {
            modalSet['contentContainer'].css({
                overflow: 'auto'
            });
        }
        popupCenter(label);

        if (modalSet.show) {
            modalSet['content'].stop(true).fadeTo(modalSet.show, 1, function () {
                if (jQuery.browser.msie) {
                    this.style.removeAttribute('filter');
                }
            });
        }
        else {
            modalSet['content'].show();
        }
        return label;
    }

    PopupModal.prototype.popupCenter = function(label) {
        popupCenter(label);
    }

    PopupModal.prototype.popupHide = function(label) {
        if (!label) {
            alert('popup label is not passed to popupHide');
            return ;
        }
        var modalSet = modalSets[label];
        if (!modalSet) {
            alert('modalSet is not exists for label "' + label + '"');
            return ;
        }

        jQuery(document).unbind('keydown.popupmodal');
        this.maskHide(label);
        this.contentHide(label);

        delete modalSets[label];
    }

    PopupModal.prototype.maskHide = function (label) {
        if (!label) {
            alert('popup label is not passed to maskHide');
            return ;
        }
        var modalSet = modalSets[label];
        if (!modalSet) {
            alert('modalSet is not exists for label "' + label + '"');
            return ;
        }

        if (modalSet['mask']) {
            if (modalSet.hide) {
                modalSet['mask'].fadeOut(modalSet.hide);
            }
            else {
                modalSet['mask'].hide();
            }
            modalSet['mask'] = false;
        }
    }

    PopupModal.prototype.contentHide = function (label) {
        if (!label) {
            alert('popup label is not passed to contentHide');
            return ;
        }
        var modalSet = modalSets[label];
        if (!modalSet) {
            alert('modalSet is not exists for label "' + label + '"');
            return ;
        }

        if (modalSet['content']) {
            if (modalSet.hide) {
                modalSet['content'].fadeOut(modalSet.hide);
            }
            else {
                modalSet['content'].hide();
            }
            modalSet['content'] = false;
        }
    }

    var popupCenter = function (label, soft) {
        if (!label) {
            alert('popup label is not passed to popupCenter');
            return ;
        }
        var modalSet = modalSets[label];
        if (!modalSet) {
            alert('modalSet is not exists for label "' + label + '"');
            return ;
        }

        var winH = jQuery(window).height();
        var winW = jQuery(window).width();

        if (modalSet['maxHeight'] || modalSet['maxWidth']) {
            modalSet['contentContainer'].css({
                maxHeight: winH * modalSet['maxHeight'],
                maxWidth: winW * modalSet['maxWidth']
            });
        }

        if (soft) {
            modalSet['content'].animate({
                left: winW / 2 - modalSet['content'].width() / 2,
                top: winH / 2 - modalSet['content'].height() / 2
            }, 100);
        }
        else {
            modalSet['content'].css({
                left: winW / 2 - modalSet['content'].width() / 2,
                top: winH / 2 - modalSet['content'].height() / 2
            });
        }
    }

    setInterval(function () {
        for (var label in modalSets) {
            popupCenter(label, true);
        }
    }, 500);
})();
