/*
 * SimpleModal 1.1.1 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://plugins.jquery.com/project/SimpleModal
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Revision: $Id: jquery.simplemodal.js 93 2008-01-15 16:14:20Z emartin24 $
 *
 */
(function($){
    $.modal = function(data, options){
        return $.modal.impl.init(data, options);
    };
    $.modal.close = function(refresh){
        if(typeof(refresh)=="undefined") 
            refresh = true;
        $.modal.impl.close(true, refresh);
    };
    $.fn.modal = function(options){
        return $.modal.impl.init(this, options);
    };
    $.modal.defaults = {
        //overlay: 100,
        overlayId: 'modalOverlay',
        overlayCss: {},
        containerId: 'modalContainer',
        containerCss: {},
        close: true,
        closeTitle: 'Close',
        closeClass: 'modalCloseImg',
        dataClass: 'modalData',
        closeCss: null,
        persist: false,
        onOpen: null,
        onShow: null,
        onClose: null,
        windowPos: null
    };
    $.modal.impl = {
        opts: null,
        dialog: {},
        init: function(data, options){
            if (this.dialog.data) {
                return false;
            }
            this.opts = $.extend({}, $.modal.defaults, options);
            if (typeof data == 'object') {
                data = data instanceof jQuery ? data : $(data);
                if (data.parent().parent().size() > 0) {
                    this.dialog.parentNode = data.parent();
                    if (!this.opts.persist) {
                        this.dialog.original = data.clone(true);
                    }
                }
            }
            else 
                if (typeof data == 'string' || typeof data == 'number') {
                    data = $('<div>').html(data);
                }
                else {
                    if (console) {
                        console.log('SimpleModal Error: Unsupported data type: ' + typeof data);
                    }
                    return false;
                }
            this.dialog.data = data.addClass(this.opts.dataClass);
            data = null;
            this.create();
            this.open();
            if ($.isFunction(this.opts.onShow)) {
                this.opts.onShow.apply(this, [this.dialog]);
            }
            return this;
        },
        create: function(){
            this.dialog.overlay = $('<div>').attr('id', this.opts.overlayId).addClass(this.opts.overlayId).css($.extend(this.opts.overlayCss, {
                //opacity: this.opts.overlay / 100,
                height: '100%',
                width: '100%',
                position: 'fixed',
                left: 0,
                top: 0,
                zIndex: 1000000
            })).hide().appendTo('body');
            this.dialog.container = $('<div>').attr('id', this.opts.containerId).addClass(this.opts.containerId).css($.extend(this.opts.containerCss, {
                position: 'absolute',
                top: '50%',
                zIndex: 1000100
            })).append(this.opts.close ? '<a class="'+ this.opts.closeClass + '" title="' + this.opts.closeTitle + '"></a>' : '').hide().appendTo('body');
            
            if (this.opts.closeCss) {
                $('.'+this.opts.closeClass).css(this.opts.closeCss);
            }
            if ($.browser.msie && ($.browser.version < 7)) {
                this.fixIE();
            }
            this.dialog.container.append(this.dialog.data.hide());
        },
        bindEvents: function(){
            var modal = this;
            $('.' + this.opts.closeClass).click(function(e){
                e.preventDefault();
                modal.close(true,false);
            });
            
            if (this.opts.close === true) {
                $('#modalOverlay').click(function(e) {
                    e.preventDefault();
                    modal.close(false,false);
                });
            }
            
            jQuery('#'+this.opts.containerId).keydown(function(e) {
                // Escape
                if (e.keyCode == 27) {
                    jQuery.modal.close(false);
                }
            });
        },
        unbindEvents: function(){
            $('.' + this.opts.closeClass).unbind('click');
        },
        fixIE: function(){
            var wHeight = $(document.body).height() + 'px';
            var wWidth = $(document.body).width() + 'px';
            this.dialog.overlay.css({
                position: 'absolute',
                height: wHeight,
                width: wWidth
            });
            this.dialog.container.css({
                position: 'absolute'
            });
            this.dialog.iframe = $('<iframe src="javascript:false;">').css($.extend(this.opts.iframeCss, {
                //opacity: 0,
                position: 'absolute',
                height: wHeight,
                width: wWidth,
                zIndex: 1000,
                top: 0,
                left: 0
            })).hide().appendTo('body');
        },
        open: function(){
            this.opts.windowPos = $(document).scrollTop();
            $(document).scrollTop(0);
            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();
            this.dialog.container.css({
                marginLeft: '-' + this.dialog.container.width() / 2 + 'px',
                marginTop: '-' + (this.dialog.container.height() + 14) / 2 + 'px'
            });
            
            this.dialog.container.css({
                
            });
        },
        close: function(external, refresh) {
            if (!this.dialog.data) {
                return false;
            }
            if ($.isFunction(this.opts.onClose) && !external) {
                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.original.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 = {};
                if(typeof(refresh)=="undefined") 
                    refresh = true;
                if(refresh)
                    location.reload();
            }
            if (this.opts.windowPos !== null) {
                $(document).scrollTop(this.opts.windowPos);
            }
            this.unbindEvents();
        }
    };
})(jQuery);
