;
(function ($) {
    var f = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object",
        ieQuirks = null,
        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)
    };
    $.fn.close = function () {
        return $.modal.impl.close()
    };
    $.modal.defaults = {
        appendTo: 'body',
        focus: true,
        opacity: 50,
        overlayId: 'simplemodal-overlay',
        overlayCss: {},
        containerId: 'simplemodal-container',
        containerCss: {},
        dataId: 'simplemodal-data',
        dataCss: {},
        minHeight: 200,
        minWidth: 300,
        maxHeight: null,
        maxWidth: null,
        autoResize: false,
        zIndex: 1000,
        close: true,
        closeHTML: '<a class="modalCloseImg" title="Close"></a>',
        closeClass: 'simplemodal-close',
        escClose: true,
        overlayClose: false,
        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
            }
            ieQuirks = $.browser.msie && !$.boxModel;
            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></div>').html(a)
            } else {
                alert('SimpleModal Error: Unsupported data type: ' + typeof a);
                return false
            }
            this.create(a);
            a = null;
            this.open();
            if ($.isFunction(this.opts.onShow)) {
                this.opts.onShow.apply(this, [this.dialog])
            }
            return this
        },
        create: function (a) {
            w = this.getDimensions();
            if (f) {
                this.dialog.iframe = $('<iframe src="javascript:false;"></iframe>').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(this.opts.appendTo)
            }
            this.dialog.overlay = $('<div></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(this.opts.appendTo);
            this.dialog.container = $('<div></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 ? $(this.opts.closeHTML).addClass(this.opts.closeClass) : '').appendTo(this.opts.appendTo);
            this.dialog.wrap = $('<div></div>').attr('tabIndex', -1).addClass('simplemodal-wrap').css({
                height: '100%',
                outline: 0,
                width: '100%'
            }).appendTo(this.dialog.container);
            this.dialog.data = a.attr('id', a.attr('id') || this.opts.dataId).addClass('simplemodal-data').css($.extend(this.opts.dataCss, {
                display: 'none'
            })).appendTo('body');
            a = null;
            this.setContainerDimensions();
            this.dialog.data.appendTo(this.dialog.wrap);
            if (f || ieQuirks) {
                this.fixIE()
            }
        },
        bindEvents: function () {
            var a = this;
            $('.' + a.opts.closeClass).bind('click.simplemodal', function (e) {
                e.preventDefault();
                a.close()
            });
            if (a.opts.close && a.opts.overlayClose) {
                a.dialog.overlay.bind('click.simplemodal', function (e) {
                    e.preventDefault();
                    a.close()
                })
            }
            $(document).bind('keydown.simplemodal', function (e) {
                if (a.opts.focus && e.keyCode == 9) {
                    a.watchTab(e)
                } else if ((a.opts.close && a.opts.escClose) && e.keyCode == 27) {
                    e.preventDefault();
                    a.close()
                }
            });
            $(window).bind('resize.simplemodal', function () {
                w = a.getDimensions();
                a.opts.autoResize ? a.setContainerDimensions() : a.setPosition();
                if (f || ieQuirks) {
                    a.fixIE()
                } else {
                    a.dialog.iframe && 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');
            $(document).unbind('keydown.simplemodal');
            $(window).unbind('resize.simplemodal');
            this.dialog.overlay.unbind('click.simplemodal')
        },
        fixIE: function () {
            var p = this.opts.position;
            $.each([this.dialog.iframe || null, this.dialog.overlay, this.dialog.container], function (i, a) {
                if (a) {
                    var b = 'document.body.clientHeight',
                        bcw = 'document.body.clientWidth',
                        bsh = 'document.body.scrollHeight',
                        bsl = 'document.body.scrollLeft',
                        bst = 'document.body.scrollTop',
                        bsw = 'document.body.scrollWidth',
                        ch = 'document.documentElement.clientHeight',
                        cw = 'document.documentElement.clientWidth',
                        sl = 'document.documentElement.scrollLeft',
                        st = 'document.documentElement.scrollTop',
                        s = a[0].style;
                    s.position = 'absolute';
                    if (i < 2) {
                        s.removeExpression('height');
                        s.removeExpression('width');
                        s.setExpression('height', '' + bsh + ' > ' + b + ' ? ' + bsh + ' : ' + b + ' + "px"');
                        s.setExpression('width', '' + bsw + ' > ' + bcw + ' ? ' + bsw + ' : ' + bcw + ' + "px"')
                    } else {
                        var c, le;
                        if (p && p.constructor == Array) {
                            var d = p[0] ? typeof p[0] == 'number' ? p[0].toString() : p[0].replace(/px/, '') : a.css('top').replace(/px/, '');
                            c = d.indexOf('%') == -1 ? d + ' + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"' : parseInt(d.replace(/%/, '')) + ' * ((' + ch + ' || ' + b + ') / 100) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';
                            if (p[1]) {
                                var e = typeof p[1] == 'number' ? p[1].toString() : p[1].replace(/px/, '');
                                le = e.indexOf('%') == -1 ? e + ' + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"' : parseInt(e.replace(/%/, '')) + ' * ((' + cw + ' || ' + bcw + ') / 100) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"'
                            }
                        } else {
                            c = '(' + ch + ' || ' + b + ') / 2 - (this.offsetHeight / 2) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';
                            le = '(' + cw + ' || ' + bcw + ') / 2 - (this.offsetWidth / 2) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"'
                        }
                        s.removeExpression('top');
                        s.removeExpression('left');
                        s.setExpression('top', c);
                        s.setExpression('left', le)
                    }
                }
            })
        },
        focus: function (a) {
            var b = this,
                p = a || 'first';
            var c = $(':input:enabled:visible:' + p, b.dialog.wrap);
            c.length > 0 ? c.focus() : b.dialog.wrap.focus()
        },
        getDimensions: function () {
            var a = $(window);
            var h = $.browser.opera && $.browser.version > '9.5' && $.fn.jquery <= '1.2.6' ? document.documentElement['clientHeight'] : $.browser.opera && $.browser.version < '9.5' && $.fn.jquery > '1.2.6' ? window.innerHeight : a.height();
            return [h, a.width()]
        },
        getVal: function (v) {
            return v == 'auto' ? 0 : parseInt(v.replace(/px/, ''))
        },
        setContainerDimensions: function () {
            var a = this.getVal(this.dialog.container.css('height')),
                cw = this.getVal(this.dialog.container.css('width')),
                dh = this.dialog.data.height(),
                dw = this.dialog.data.width();
            var b = this.opts.maxHeight && this.opts.maxHeight < w[0] ? this.opts.maxHeight : w[0],
            mw = this.opts.maxWidth && this.opts.maxWidth < w[1] ? this.opts.maxWidth : w[1];
            if (!a) {
                if (!dh) {
                    a = this.opts.minHeight
                } else {
                    if (dh > b) {
                        a = b
                    } else if (dh < this.opts.minHeight) {
                        a = this.opts.minHeight
                    } else {
                        a = dh
                    }
                }
            } else {
                a = a > b ? b : a
            }
            if (!cw) {
                if (!dw) {
                    cw = this.opts.minWidth
                } else {
                    if (dw > mw) {
                        cw = mw
                    } else if (dw < this.opts.minWidth) {
                        cw = this.opts.minWidth
                    } else {
                        cw = dw
                    }
                }
            } else {
                cw = cw > mw ? mw : cw
            }
            this.dialog.container.css({
                height: a,
                width: cw
            });
            if (dh > a || dw > cw) {
                this.dialog.wrap.css({
                    overflow: 'auto'
                })
            }
            this.setPosition()
        },
        setPosition: function () {
            var a, left, hc = (w[0] / 2) - ((this.dialog.container.height() || this.dialog.data.height()) / 2),
                vc = (w[1] / 2) - ((this.dialog.container.width() || this.dialog.data.width()) / 2);
            if (this.opts.position && Object.prototype.toString.call(this.opts.position) === "[object Array]") {
                a = this.opts.position[0] || hc;
                left = this.opts.position[1] || vc
            } else {
                a = hc;
                left = vc
            }
            this.dialog.container.css({
                left: left,
                top: a
            })
        },
        watchTab: function (e) {
            var a = this;
            if ($(e.target).parents('.simplemodal-container').length > 0) {
                a.inputs = $(':input:enabled:visible:first, :input:enabled:visible:last', a.dialog.data);
                if (!e.shiftKey && e.target == a.inputs[a.inputs.length - 1] || e.shiftKey && e.target == a.inputs[0] || a.inputs.length == 0) {
                    e.preventDefault();
                    var b = e.shiftKey ? 'last' : 'first';
                    setTimeout(function () {
                        a.focus(b)
                    },
                    10)
                }
            } else {
                e.preventDefault();
                setTimeout(function () {
                    a.focus()
                },
                10)
            }
        },
        open: function () {
            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.focus();
            this.bindEvents()
        },
        close: function () {
            if (!this.dialog.data) {
                return false
            }
            this.unbindEvents();
            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.hide().remove();
                        this.dialog.orig.appendTo(this.dialog.parentNode)
                    }
                } else {
                    this.dialog.data.hide().remove()
                }
                this.dialog.container.hide().remove();
                this.dialog.overlay.hide().remove();
                this.dialog.iframe && this.dialog.iframe.hide().remove();
                this.dialog = {}
            }
        }
    }
})(jQuery);