﻿/*! 
*   jQuery Modal Dialog Version 1.0
*   Developed by CJ Amodeo (cjamodeo@gmail.com)
*   Page should be XHTML 1.0 Transitional
*   Requires jQuery 1.4.2 or later
*
*   displaySettings - Default display settings
********************************************************************************
*   All function calls should be made after the page is loaded (jQuery(document).ready)
********************************************************************************
*   show(objSelector, appendToNodeSelector, settings) - Displays the dialog
*       objSelector: selector of obj to display as modal
*       appendToNodeSelector: -will move the object and append it to the sepcified node
*       settings: overrides default display settings
*
*   hide() - Hides the dialog
*
*   refresh() - repositions the dialog
*
*   getCurrentSetting(settingName) - returns the current setting
*   setCurrentSetting(settingName, settingValue) - updates the currently used setting
*
*   settings/jQuery.modalDialog.displaySettings map variables
*       dialogPosition: string, fixed or absolute, default fixed
*       hideOnClick: boolean, true/false, default true
*       backgroundColor: string, hex color, default #000000
*       opacity: float, 0-1, default 0.6
*       left: integer, default -1 (causes centering)
*       top: integer, default -1 (causes centering)
*       width: integer, default -1 (forces use of CSS width)
*       height: integer, default -1 (forces use of CSS height)
*       show: function(dialogSelector), default null, must call showCallback after
*       hide: function(dialogSelector), default null, must call hideCallback after
*       onHide: function, default null, called at the end of hide()
*       fullScreen: boolean, default false, sets the dialog's width and height to the window's width and height
*       padding: integer, default 50, window/dialog padding when using fullScreen=true
*       animation: string, default "fadeIn", values are "fadeIn", "expand"
*/

jQuery.modalDialog = (function() {
    var currentSettings = null;
    var dialogSelector = null;
    var divWidth = 0;
    var divHeight = 0;
    var grayoutDivID = "evokeModalDialogGrayout";
    var grayoutDivObj = null;
    var scrollTop = 0;
    var scrollLeft = 0;
    var position = { x: -1, y: -1 };

    //    var browser = (function() {
    //        var ua = window.navigator.userAgent.toLowerCase();
    //        function b() { }
    //        b.isIE = function() {
    //            return /msie/.test(ua);
    //        }
    //        b.isIE6 = function() {
    //            return /msie/.test(ua) && (ua.match(/.+msie (\d+)/))[1] == 6;
    //        }
    //        b.isIDevice = function() {
    //            return /(ipod|ipad|iphone)/.test(ua);
    //        }
    //        return b;
    //    })();
    var browser = {
        ua: window.navigator.userAgent.toLowerCase(),
        isIE: function() {
            return /msie/.test(this.ua);
        },
        isIE6: function() {
            return /msie/.test(this.ua) && (this.ua.match(/.+msie (\d+)/))[1] == 6;
        },
        isIDevice: function() {
            return /(ipod|ipad|iphone)/.test(this.ua);
        }
    };
    function getType(obj) {
        if (obj instanceof Array)
            return 'array';
        else {
            var t = typeof (obj);
            if (t == 'object' && !obj) return 'null';
            else return t;
        }
    }
    function getSetting(settingName) {
        if (currentSettings != null && getType(currentSettings[settingName]) != 'undefined')
            return currentSettings[settingName];
        else
            return jQuery.modalDialog.displaySettings[settingName];
    }

    function getDialogWidth() {
        if (getSetting("fullScreen")) {
            return jQuery(window).width() - (getSetting("padding") * 2);
        } else {
            var width = parseInt(getSetting("width"));
            if (width < 0) {
                width = parseInt(jQuery(dialogSelector).css("width"));
                if (isNaN(width) || getSetting("usePadding")) {
                    width = jQuery(dialogSelector).width();
                }
                return width;
            }
            else {
                return width;
            }
        }
    }
    function getDialogHeight() {
        if (getSetting("fullScreen")) {
            return jQuery(window).height() - (getSetting("padding") * 2);
        } else {
            var height = parseInt(getSetting("height"));
            if (height < 0) {
                height = parseInt(jQuery(dialogSelector).css("height"));
                if (isNaN(height) || getSetting("usePadding"))
                    height = jQuery(dialogSelector).height();
                return height;
            }
            else {
                return height;
            }
        }
    }
    function getGrayWidth() {
        var w = 0;
        w = jQuery(document).width();
        if (browser.isIE6()) {
            w -= 21; //vertical scrollbar width
        }
        return w;
    }
    function getGrayHeight() {
        return jQuery(document).height();
    }
    function getCenterX() {
        if (position.x > -1) {
            return position.x + getScrollX();
        } else {
            //jQuery(dialogSelector).outerWidth causes a bug so get padding this way
            var padLeft = parseInt(jQuery(dialogSelector).css("padding-left"));
            var padRight = parseInt(jQuery(dialogSelector).css("padding-right"));
            var pad = 0;
            if (!isNaN(padLeft))
                pad += padLeft;
            if (!isNaN(padRight))
                pad += padRight;
            return Math.floor((jQuery(window).width() - getDialogWidth() - pad) / 2) + getScrollX();
        }
    }
    function getCenterY() {
        if (position.y > -1) {
            return position.y + getScrollY();
        } else {
            var padTop = parseInt(jQuery(dialogSelector).css("padding-top"));
            var padBot = parseInt(jQuery(dialogSelector).css("padding-bottom"));
            var pad = 0;
            if (!isNaN(padTop))
                pad += padTop;
            if (!isNaN(padBot))
                pad += padBot;
            return Math.floor((jQuery(window).height() - getDialogHeight() - pad) / 2) + getScrollY();
        }
    }
    function getScrollX() {
        var dPos = getSetting("dialogPosition");
        if (dPos == "absolute" || browser.isIE6() || browser.isIDevice()) {
            return jQuery(document).scrollLeft();
        } else {
            return 0;
        }
    }
    function getScrollY() {
        var dPos = getSetting("dialogPosition");
        if (dPos == "absolute" || browser.isIE6() || browser.isIDevice()) {
            return jQuery(document).scrollTop();
        } else {
            return 0;
        }
    }
    function getStyle(el, style) {
        return jQuery(el).css(style);
    }
    function positionGray() {
        jQuery(dialogSelector).css({
            "left": getCenterX() + "px",
            "top": getCenterY() + "px",
            "width": getDialogWidth() + "px",
            "height": getDialogHeight() + "px"
        });
        jQuery(grayoutDivObj).css({
            "width": getGrayWidth() + "px",
            "height": getGrayHeight() + "px",
            "left": "0px",
            "top": "0px"
        });
    }
    function showDialog() {
        c.initOpacity();
        jQuery(grayoutDivObj).css("display", "block");
        jQuery(dialogSelector).css("display", "block");
    }

    function c() { }
    c.exec = function(strFunc) { return eval(strFunc)(); }
    c.getGrayoutDivID = function() { return grayoutDivID; }
    c.displaySettings = {
        dialogPosition: "fixed",
        hideOnClick: true,
        backgroundColor: "#000000",
        opacity: 0.6,
        left: -1,
        top: -1,
        width: -1,
        height: -1,
        show: null,
        hide: null,
        onHide: null,
        fullScreen: false,
        padding: 50,
        animation: "fadeIn"
    };
    c.refresh = function() {
        positionGray();
    }
    c.setCurrentSetting = function(settingName, settingValue) {
        currentSettings[settingName] = settingValue;
    }
    c.getCurrentSetting = function(settingName) {
        return getSetting(settingName);
    }
    c.showCallback = function() {
    }
    c.hideCallback = function() {
        jQuery(dialogSelector).css("display", "none");
        jQuery(grayoutDivObj).css({
            "display": "none",
            "width": "0px",
            "height": "0px"
        });
        if (browser.isIE6()) {
            jQuery("select").each(function() {
                jQuery(this).css("visibility", "visible");
            });
        }
    }
    c.initOpacity = function() {
        var opac = getSetting("opacity");
        jQuery(grayoutDivObj).css("opacity", opac);
        if (browser.isIE()) {
            grayoutDivObj.filters["alpha"].opacity = parseFloat(opac).toFixed(2) * 100;
        }
    }
    c.show = function(objSelector, appendToNodeSelector, settings) {
        if (dialogSelector != null && dialogSelector != objSelector) {
            jQuery(dialogSelector).css("display", "none");
        }
        if (settings != undefined)
            currentSettings = settings;
        else
            currentSettings = c.displaySettings;

        position.x = parseInt(getSetting("left"));
        position.y = parseInt(getSetting("top"));

        //        jQuery(objSelector).css({
        //            "width": getDialogWidth() + "px",
        //            "height": getDialogHeight() + "px"
        //        });

        if (appendToNodeSelector != undefined && appendToNodeSelector != null) {
            jQuery(appendToNodeSelector).append(jQuery(objSelector));
        }

        if (browser.isIE6()) {
            scrollTop = getScrollY();
            scrollLeft = getScrollX();
        }
        if (grayoutDivObj == null)
            grayoutDivObj = document.getElementById(grayoutDivID);

        jQuery(grayoutDivObj).css({
            "background-color": getSetting("backgroundColor")
        });

        dialogSelector = objSelector;

        jQuery(dialogSelector).css({
            "z-index": "99"
        });

        if (browser.isIE6()) {
            jQuery(dialogSelector).css("position", "absolute");
            jQuery(grayoutDivObj).css("position", "absolute");
            jQuery("select").each(function() {
                jQuery(this).css("visibility", "hidden");
            });
            jQuery(dialogSelector + " > select").each(function() {
                jQuery(this).css("visibility", "visible");
            });
        } else {
            jQuery(dialogSelector).css("position", getSetting("dialogPosition"));
            jQuery(grayoutDivObj).css("position", "fixed");
        }
        positionGray();

        if (getSetting("animation") != null) {
            switch (getSetting("animation")) {
                case "fadeIn":
                    jQuery.modalDialog.animations.fadeIn(dialogSelector);
                    break;
                case "expand":
                    jQuery.modalDialog.animations.expand(dialogSelector);
                    break;
                default:
                    showDialog();
                    break;
            }
        } else {
            if (typeof (currentSettings.show) != "undefined") {
                if (typeof (currentSettings.show) == "function") {
                    currentSettings.show(dialogSelector);
                } else {
                    showDialog();
                }
            } else if (typeof (this.displaySettings.show) == "function") {
                this.displaySettings.show(dialogSelector);
            } else {
                showDialog();
            }
        }
    }
    c.hide = function() {
        if (getSetting("animation") != null) {
            switch (getSetting("animation")) {
                case "fadeIn":
                    jQuery.modalDialog.animations.fadeOut(dialogSelector);
                    break;
                case "expand":
                    jQuery.modalDialog.animations.collapse(dialogSelector);
                    break;
                default:
                    this.hideCallback();
                    break;
            }
        } else {
            if (typeof (currentSettings.hide) != "undefined") {
                if (typeof (currentSettings.hide) == "function") {
                    currentSettings.hide(dialogSelector);
                } else {
                    this.hideCallback();
                }
            } else if (typeof (this.displaySettings.hide) == "function") {
                this.displaySettings.hide(dialogSelector);
            } else {
                this.hideCallback();
            }
        }

        if (typeof (currentSettings.onHide) == "function") {
            currentSettings.onHide();
        }
    }
    c.outsideClick = function() {
        if (currentSettings.hideOnClick != undefined) {
            if (currentSettings.hideOnClick) {
                this.hide();
            }
        } else if (this.displaySettings.hideOnClick) {
            this.hide();
        }
    }
    c.attachClickEvent = function(handler) {
        jQuery(grayoutDivObj).bind("click", handler);
    }
    c.detachClickEvent = function(handler) {
        jQuery(grayoutDivObj).unbind("click", handler);
    }

    jQuery(document).ready(function() {
        jQuery("body").append("<div id=\"" + grayoutDivID + "\"></div>");
        jQuery("#" + grayoutDivID).css({
            "position": "fixed",
            "left": "0px",
            "top": "0px",
            "width": "0px",
            "height": "0px",
            "display": "none",
            "z-index": "98",
            "background-color": c.displaySettings.backgroundColor,
            "opacity": c.displaySettings.opacity,
            "filter": "alpha(opacity=" + (parseFloat(c.displaySettings.opacity).toFixed(2) * 100) + ")"
        });
        jQuery("#" + grayoutDivID).click(function() { c.outsideClick(); });
        jQuery(window).resize(function() { positionGray(); });
        if (browser.isIE6()) {
            jQuery(window).scroll(function() {
                if (jQuery("#" + grayoutDivID).css("display") != "none" && getSetting("dialogPosition") == "fixed") {
                    jQuery(window).scrollLeft(scrollLeft);
                    jQuery(window).scrollTop(scrollTop);
                }
            });
        }
    });
    return c;
})();
jQuery.modalDialog.animations = {
    fadeIn: function(dialogSelector) {
        jQuery("#" + jQuery.modalDialog.getGrayoutDivID()).fadeTo("normal", jQuery.modalDialog.getCurrentSetting("opacity"));
        jQuery(dialogSelector).fadeIn("normal", jQuery.modalDialog.showCallback);
    },
    fadeOut: function(dialogSelector) {
        jQuery("#" + jQuery.modalDialog.getGrayoutDivID()).fadeOut("normal");
        jQuery(dialogSelector).fadeOut("normal", jQuery.modalDialog.hideCallback);
    },
    expand: function(dialogSelector) {
        jQuery("#" + jQuery.modalDialog.getGrayoutDivID()).fadeTo("normal", jQuery.modalDialog.getCurrentSetting("opacity"));
        var dialog = jQuery(dialogSelector);
        var width = parseInt(dialog.css("width"));
        var height = parseInt(dialog.css("height"));
        var left = jQuery.modalDialog.exec("getCenterX");
        var top = jQuery.modalDialog.exec("getCenterY");
        dialog.css({ "width": "0px", "height": "0px", "left": (left + Math.floor(width / 2)) + "px", "top": (top + Math.floor(height / 2)) + "px" });
        dialog.animate({
            "width": width,
            "height": height,
            "left": left,
            "top": top
        }, "normal", jQuery.modalDialog.showCallback);
    },
    collapse: function(dialogSelector) {
        jQuery("#" + jQuery.modalDialog.getGrayoutDivID()).fadeOut("normal");
        var dialog = jQuery(dialogSelector);
        var width = parseInt(dialog.css("width"));
        var height = parseInt(dialog.css("height"));
        var left = jQuery.modalDialog.exec("getCenterX");
        var top = jQuery.modalDialog.exec("getCenterY");
        dialog.animate({
            "width": 0,
            "height": 0,
            "left": (left + Math.floor(width / 2)),
            "top": (top + Math.floor(height / 2))
            }, "normal", function() {
                dialog.css({ "width": width + "px", "height": height + "px" });
                jQuery.modalDialog.hideCallback();
            }
        );
    }
}
