/*
 * SimpleModal 1.2 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2008 Eric Martin
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 169 2008-12-04 18:31:40Z emartin24 $
 */
(function($) {
    var c = $.browser.msie && parseInt($.browser.version) == 6 && !window.XMLHttpRequest,
    ieQuirks = $.browser.msie && !$.boxModel,
    w = [];
    $.modal = function(a, b) {
        return $.modal.impl.init(a, b);
    };
    $.modal.close = function() {
        $.modal.impl.close();
    };
    $.fn.modal = function(a) {
        return $.modal.impl.init(this, a);
    };
    $.modal.defaults = {
        iframe: true,
        opacity: 50,
        overlayId: "simplemodal-overlay",
        overlayCss: {},
        containerId: "simplemodal-container",
        containerCss: {},
        dataCss: {},
        zIndex: 1000,
        close: true,
        closeHTML: "<a class=\"modalCloseImg\" title=\"Close\"></a>",
        closeClass: "simplemodal-close",
        position: null,
        persist: false,
        onOpen: null,
        onShow: null,
        onClose: null
    };
    $.modal.impl = {
        opts: null,
        dialog: {},
        init: function(a, b) {
            if (this.dialog.data) {
                return false;
            }
            this.opts = $.extend({},
            $.modal.defaults, b);
            this.zIndex = this.opts.zIndex;
            this.occb = false;
            if (typeof a == "object") {
                a = a instanceof jQuery ? a: $(a);
                if (a.parent().parent().size() > 0) {
                    this.dialog.parentNode = a.parent();
                    if (!this.opts.persist) {
                        this.dialog.orig = a.clone(true);
                    }
                }
            } else if (typeof a == "string" || typeof a == "number") {
                a = $("<div/>").html(a);
            } else {
                alert("SimpleModal Error: Unsupported data type: " + typeof a);
                return false;
            }
            this.dialog.data = a.addClass("simplemodal-data").css(this.opts.dataCss);
            a = null;
            this.create();
            this.open();
            if ($.isFunction(this.opts.onShow)) {
                this.opts.onShow.apply(this, [this.dialog]);
            }
            return this;
        },
        create: function() {
            w = this.getDimensions();
            if (this.opts.iframe || c) {
                this.dialog.iframe = $("<iframe src=\"javascript:false;\"/>").css($.extend(this.opts.iframeCss, {
                    display: "none",
                    opacity: 0,
                    position: "fixed",
                    height: w[0],
                    width: w[1],
                    zIndex: this.opts.zIndex,
                    top: 0,
                    left: 0
                })).appendTo("body");
            }
            this.dialog.overlay = $("<div/>").attr("id", this.opts.overlayId).addClass("simplemodal-overlay").css($.extend(this.opts.overlayCss, {
                display: "none",
                opacity: this.opts.opacity / 100,
                height: w[0],
                width: w[1],
                position: "fixed",
                left: 0,
                top: 0,
                zIndex: this.opts.zIndex + 1
            })).appendTo("body");
            this.dialog.container = $("<div/>").attr("id", this.opts.containerId).addClass("simplemodal-container").css($.extend(this.opts.containerCss, {
                display: "none",
                position: "fixed",
                zIndex: this.opts.zIndex + 2
            })).append(this.opts.close ? $(this.opts.closeHTML).addClass(this.opts.closeClass) : "").appendTo("body");
            this.setPosition();
            if (c || ieQuirks) {
                this.fixIE();
            }
            this.dialog.container.append(this.dialog.data.hide());
        },
        bindEvents: function() {
            var a = this;
            $("." + this.opts.closeClass).bind("click.simplemodal",
            function(e) {
                e.preventDefault();
                a.close();
            });
            $(window).bind("resize.simplemodal",
            function() {
                w = a.getDimensions();
                a.setPosition();
                if (!c) {
                    a.dialog.iframe.css({
                        height: w[0],
                        width: w[1]
                    });
                    a.dialog.overlay.css({
                        height: w[0],
                        width: w[1]
                    });
                }
            });
        },
        unbindEvents: function() {
            $("." + this.opts.closeClass).unbind("click.simplemodal");
            $(window).unbind("resize.simplemodal");
        },
        fixIE: function() {
            $.each([this.dialog.iframe, this.dialog.overlay, this.dialog.container],
            function(i, a) {
                var s = a[0].style;
                s.position = "absolute";
                if (i < 2) {
                    s.setExpression("height", "document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + \"px\"");
                    s.setExpression("width", "jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + \"px\"");
                } else {
                    s.setExpression("top", "(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + \"px\"");
                }
            });
        },
        getDimensions: function() {
            var a = $(window);
            var h = $.browser.opera && $.browser.version > "9.5" && $.fn.jquery <= "1.2.6" ? document.documentElement.clientHeight: a.height();
            return [h, a.width()];
        },
        setPosition: function() {
            var a = 0,
            left = 0,
            hCenter = (w[0] / 2) - this.dialog.container.height() / 2,
            vCenter = (w[1] / 2) - this.dialog.container.width() / 2;
            if (this.opts.position && this.opts.position.constructor == Array) {
                a += this.opts.position[0] || hCenter;
                left += this.opts.position[1] || vCenter;
            } else {
                a += hCenter;
                left += vCenter;
            }
            this.dialog.container.css({
                left: left,
                top: a
            });
        },
        open: function() {
            if (this.dialog.iframe) {
                this.dialog.iframe.show();
            }
            if ($.isFunction(this.opts.onOpen)) {
                this.opts.onOpen.apply(this, [this.dialog]);
            } else {
                this.dialog.overlay.show();
                this.dialog.container.show();
                this.dialog.data.show();
            }
            this.bindEvents();
        },
        close: function() {
            if (!this.dialog.data) {
                return false;
            }
            if ($.isFunction(this.opts.onClose) && !this.occb) {
                this.occb = true;
                this.opts.onClose.apply(this, [this.dialog]);
            } else {
                if (this.dialog.parentNode) {
                    if (this.opts.persist) {
                        this.dialog.data.hide().appendTo(this.dialog.parentNode);
                    } else {
                        this.dialog.data.remove();
                        this.dialog.orig.appendTo(this.dialog.parentNode);
                    }
                } else {
                    this.dialog.data.remove();
                }
                this.dialog.container.remove();
                this.dialog.overlay.remove();
                if (this.dialog.iframe) {
                    this.dialog.iframe.remove();
                }
                this.dialog = {};
            }
            this.unbindEvents();
        }
    };
} (jQuery));