﻿(function($) {

    $.blockUI.defaults.centerX = true;
    $.blockUI.defaults.centerY = true;
    //override css setting as they are defined in template.css
    $.blockUI.defaults.overlayCSS = {};
    $.blockUI.defaults.css = {};
    if (typeof FR == 'undefined') { FR = {}; }

    FR.modalPopups = {
        popups: [],
        add: function(ID, ModalContainerID, TargetControlID, PopupControlID, OKControlID, CancelControlID, opts) {
            //debugger;
            this.popups[ID] = new modalPopup(ModalContainerID, TargetControlID, PopupControlID, OKControlID, CancelControlID, opts);
            return this.popups[ID];
        },
        find: function(ID) {
            if (typeof (this.popups[ID]) != 'undefined') {
                return this.popups[ID];
            }
            return null;
        },
        showInfo: function(msg, divID) {
            var opts = { message: '<div class="info">' + msg + '</div>' };
            if (divID) {
                $('#' + divID).block(opts);
            } else {
                $.blockUI(opts);
            }
        },
        hideInfo: function(divID) {
            if (divID) {
                $('#' + divID).unblock();
            } else {
                $.unblockUI();
            }
        },
        disable: function(TargetControlID) {
            $(document).ready(function() {
                $(TargetControlID).attr("disabled", true);
            });
        },
        alert: function(msg) {
            var sMsg = "";
            var sHeading = "";
            if (typeof msg == 'string') {
                sMsg = msg;
            } else if (typeof msg == 'object') {
                if (typeof msg.message == 'string') {
                    sMsg = msg.message;
                }
                if (typeof msg.heading == 'string') {
                    sHeading = msg.heading;
                }
            }
            if (sMsg.length > 0) {
                if (sHeading.length > 0) {
                    sHeading = '<h2>' + sHeading + '</h2>';
                }
                var opts = { message: '<div class="modalPos">' + sHeading + '<div>' + sMsg + '</div><div class="centre"><input type="button" text="OK" value="OK" onclick="$.unblockUI();return false;"/></div></div>' };
                $.blockUI(opts);
            }
        }
    };

    var modalPopup = function(ModalContainerID, TargetControlID, PopupControlID, OKControlID, CancelControlID, opts) {
        this._origOKOnclick = null;
        this._origCancelOnclick = null;
        this._TargetControlID = TargetControlID;
        this._PopupControlID = PopupControlID;
        this._OKControlID = OKControlID;
        this._CancelControlID = CancelControlID;
        this._opts = opts || {};
        this._opts.message = $('#' + PopupControlID);
        this._opts.message.css('position', 'static');
        this._opts.focusInput = true;
        this._opts.css = { width: 'auto' };
        this.show = function() {
            this._opts.ModalContainerID = ModalContainerID;
            this._opts.css.position = 'fixed';
            this._opts.css.left = ((($(document).width() - this._opts.message.width()) / 2) + 'px');
            this._opts.css.top = ((($(window).height() - this._opts.message.height()) / 2) + 'px');
            this._opts.css.width = (this._opts.message.width() + 'px');
            $.blockUI(this._opts);
            $('.blockMsg .FRdialogue').hide().css('visibility', 'visible').fadeIn();
            if ($('.blockMsg').height() === 0) {
                $('.blockMsg').height($('.blockMsg .FRdialogue').height() + 'px');
                var l = ($(window).width() - $('.blockMsg').width()) / 2;
                var t = ($(window).height() - $('.blockMsg').height()) / 2;
                $('.blockMsg').css({ "top": t + "px", "left": l + "px" });
            }
            $(window).resize(function() {
                var l = ($(window).width() - $('.blockMsg').width()) / 2;
                var t = ($(window).height() - $('.blockMsg').height()) / 2;
                $('.blockMsg').css({ "top": t + "px", "left": l + "px" });
            });
            if (this._OKControlID && this._OKControlID != '') {
                $('#' + this._OKControlID).click(this.clickOK.bind(this));
            }
            if (this._CancelControlID && this._CancelControlID != '') {
                $('#' + this._CancelControlID).click(this.clickCancel.bind(this));
            }
        };

        this.hide = function() {
            $.unblockUI(this._opts);
            if (this._OKControlID && this._OKControlID != '') {
                $('#' + this._OKControlID).unbind('click', this.clickOK);
            }
            if (this._CancelControlID && this._CancelControlID != '') {
                $('#' + this._CancelControlID).unbind('click', this.clickCancel);
            }
        };
        this.clickOpener = function(e) {
            e.preventDefault();
            this.show();
        };
        this.clickOK = function(e) {
            e.preventDefault();
            this.hide();
        };
        this.clickCancel = function(e) {
            e.preventDefault();
            this.hide();
        };
        if (TargetControlID && TargetControlID != '') {
            $('#' + TargetControlID).click(this.clickOpener.bind(this));
        }

    };

})(jQuery);

