/**
* PopupModal class
*/
var PopupModal;

(function () {
    var modalSets = {};
    var modalSetsLabels = [];

    PopupModal = function (passedOptions) {
        this.options = jQuery.extend({
            tplDialog: false, 
            tplAlert: false, 
            tplConfirm: false
        }, passedOptions);

        if (this.options.tplDialog) {
            this.$tplDialog = $(this.options.tplDialog);
        }
        if (!this.$tplDialog || !this.$tplDialog.length) {
            this.$tplDialog = $('#PopupModalDialog');
            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.$tplAlert = $(this.options.tplAlert);
        }
        if (!this.$tplAlert || !this.$tplAlert.length) {
            this.$tplAlert = $('#PopupModalAlert');
            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.$tplConfirm = $(this.options.tplConfirm);
        }
        if (!this.$tplConfirm || !this.$tplConfirm.length) {
            this.$tplConfirm = $('#PopupModalConfirm');
            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) {
        var i;
        var modalSet = false;
        var labelIndex = false;
        var $content = jQuery(selector);
        for (i in modalSetsLabels) {
            if (modalSets[modalSetsLabels[i]]['content'][0] == $content[0]) {
                // 2011-10-03 - avoid "Loading" window blinking
                // this.popupHide(modalSetsLabels[i]);
                // break;
                return;
            }
        }
        if (!label) {
            labelIndex = 0;
            for (i in modalSets) {
                if (labelIndex <= modalSets[i]['labelIndex']) {
                    labelIndex = modalSets[i]['labelIndex'] + 1;
                }
            }
            label = '__PopupModalLabel' + labelIndex;
        }

        modalSetsLabels[modalSetsLabels.length] = label;

        if (modalSets[label]) {
            modalSet = modalSets[label];
        }
        else {
            var currentZIndex = 100;
            for ( i in modalSets) {
                if (currentZIndex <= modalSets[i]['zIndex']) {
                    currentZIndex = modalSets[i]['zIndex'] + 2;
                }
            }
            modalSet = jQuery.extend(jQuery.extend({
                // options
                easyClose: true,
                background: '#000',
                show: 200,
                hide: 200,
                contentContainerSelector: '.popupModalContentContainer', // previously was popupModalContent
                opacity: 0.4,
                maxHeight: 0.7,
                maxWidth: 0.7,
                DOMSafety: false
            }, passedOptions), {
                // internal data
                mask: false,
                content: false,
                zIndex: currentZIndex,
                labelIndex: labelIndex

                ,label: label
                ,selector: selector
            });
            // modalSet['contentContainer'] = $content.find(modalSet['contentContainerSelector']);

            modalSets[label] = modalSet;
        }

        if (modalSet['content']) {
            this.contentHide(label);
        }
        modalSet['content'] = $content;

        if (!modalSet['DOMSafety']) {
            // save DOM position to restore it when popup closing
            modalSet['contentPrevElement'] = modalSet['content'].prev();
            if (!modalSet['contentPrevElement'].length) {
                modalSet['contentPrevElement'] = false;
                modalSet['contentParentElement'] = modalSet['content'].parent();
            }
            else {
                modalSet['contentParentElement'] = false;
            }
            modalSet['content'].appendTo('body');
        }

        // mask
        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'
                    });
            }

            var maskHeight = '100%';
            var maskWidth = '100%';

            modalSet['mask'].css({
                width: maskWidth,
                height: maskHeight
            });
            if (modalSet.show) {
                modalSet['mask'].stop(true).fadeTo(modalSet['show'], modalSet['opacity']);
            }
            else {
                modalSet['mask'].show().css({opacity: modalSet['opacity']});
            }
        }
        modalSet['mask'].appendTo('body');

        // Content
        modalSet['content'].css({
            position: 'fixed',
            zIndex: modalSet['zIndex'] + 2
        });
        // modalSet['contentContainer'] = getContentContainer(modalSet);
        var contentContainer = getContentContainer(modalSet);

        // this should be restored when popup hides
        /*
        modalSet['keepMaxWidth'] = modalSet['contentContainer'].css('maxWidth');
        modalSet['keepMaxHeight'] = modalSet['contentContainer'].css('maxHeight');
        modalSet['keepOverflow'] = modalSet['contentContainer'].css('overflow');
        */
        modalSet['keepMaxWidth'] = contentContainer.css('maxWidth');
        modalSet['keepMaxHeight'] = contentContainer.css('maxHeight');
        modalSet['keepOverflow'] = contentContainer.css('overflow');

        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.popupExists = function(label) {
        return modalSets[label] ? modalSets[label] : false;
    };

    PopupModal.prototype.popupCenter = function(label) {
        popupCenter(label);
    };

    PopupModal.prototype.popupToFront = function(label) {
        var FrontZIndex = 0;
        var i;
        for (i in modalSets) {
            if (FrontZIndex < modalSets[i]['zIndex'] && i != label) {
                FrontZIndex = modalSets[i]['zIndex'];
            }
        }
        if (FrontZIndex) {
            modalSets[label]['zIndex'] = FrontZIndex + 2;
            modalSets[label]['mask'].css({
                zIndex: modalSets[label]['zIndex'] + 1
            });
            modalSets[label]['content'].css({
                zIndex: modalSets[label]['zIndex'] + 2
            });
        }
    };

    PopupModal.prototype.popupHide = function(label, resetContent) {
        if (!label) {
            label = modalSetsLabels[modalSetsLabels.length - 1];
        }
        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 + '" inside PopoupModal.popupHide');
            return ;
        }

        jQuery(document).unbind('keydown.popupmodal');
        this.maskHide(label);
        this.contentHide(label, resetContent);

        for (var i = 0; i < modalSetsLabels.length; i++) {
            if (modalSetsLabels[i] == label) {
                modalSetsLabels.splice(i, 1);
                modalSets[label]['content'] = false;
                break;
            }
        }
//        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 + '" inside PopoupModal.maskHide');
            return ;
        }

        if (modalSet['mask']) {
            if (modalSet.hide) {
                var maskFading = modalSet['mask'];
                modalSet['mask'].fadeOut(modalSet.hide, function () {
                    if (!modalSet['DOMSafety']) {
                        if (modalSet['contentPrevElement']) {
                            modalSet['contentPrevElement'].after(maskFading);
                        }
                        else {
                            modalSet['contentParentElement'].prepend(maskFading);
                        }
                    }
                });
            }
            else {
                modalSet['mask'].hide();
                if (!modalSet['DOMSafety']) {
                    if (modalSet['contentPrevElement']) {
                        modalSet['contentPrevElement'].after(modalSet['mask']);
                    }
                    else {
                        modalSet['contentParentElement'].prepend(modalSet['mask']);
                    }
                }
            }
            modalSet['mask'] = false;
        }
    };

    PopupModal.prototype.contentHide = function (label, resetContent) {
        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 + '" inside PopoupModal.contentHide');
            return ;
        }

        if (modalSet['content']) {
            var contentContainer = getContentContainer(modalSet);
            if (0 && modalSet.hide) {
                var contentFading = modalSet['content'];
                modalSet['content'].fadeOut(modalSet.hide, function () {
                    if (!modalSet['DOMSafety']) {
                        // restore DOM position
                        if (modalSet['contentPrevElement']) {
                            modalSet['contentPrevElement'].after(contentFading);
                        }
                        else {
                            modalSet['contentParentElement'].prepend(contentFading);
                        }
                    }
                    contentContainer.css({
                        'maxWidth': modalSet['keepMaxWidth'],
                        'maxHeight': modalSet['keepMaxHeight'],
                        'overflow': modalSet['keepOverflow']
                    });
                    if (resetContent) {
                        var $resetContent = $(resetContent);
                        if ($resetContent) {
                            $resetContent.html('');
                        }
                        else {
                            contentFading.html('');
                        }
                    }
                });
            }
            else {
                modalSet['content'].hide();
                if (!modalSet['DOMSafety']) {
                    // restore DOM position
                    if (modalSet['contentPrevElement']) {
                        modalSet['contentPrevElement'].after(modalSet['content']);
                    }
                    else {
                        modalSet['contentParentElement'].prepend(modalSet['content']);
                    }
                }
                contentContainer.css({
                    'maxWidth': modalSet['keepMaxWidth'],
                    'maxHeight': modalSet['keepMaxHeight'],
                    'overflow': modalSet['keepOverflow']
                });
                if (resetContent) {
                    var $resetContent = $(resetContent);
                    if ($resetContent) {
                        $resetContent.html('');
                    }
                    else {
                        modalSet['content'].html('');
                    }
                }
            }
            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 + '" inside popupCenter');
            return ;
        }

        if (modalSet['content']) {
            var winH = jQuery(window).height();
            var winW = jQuery(window).width();
            if (modalSet['maxHeight'] || modalSet['maxWidth']) {
                var contentContainer = getContentContainer(modalSet);
                contentContainer.css({
                    overflow: 'auto'
                });
                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
                });
            }
        }
    };
    var getContentContainer = function (modalSet) {
        var $contentContainer = modalSet['content'].find(modalSet['contentContainerSelector']);
        if (!$contentContainer.length) {
            $contentContainer = modalSet['content'];
        }
        return $contentContainer;
    };

    setInterval(function () {
        for (var label in modalSets) {
            popupCenter(label, true);
        }
    }, 1000);
})();
