﻿(function($) {

    $.modalPopUp = function (data, options) {
        return $.modalPopUp.impl.init(data, options);
    };
    
    $.fn.modalPopUp = function (options) {
        return $.modalPopUp.impl.init(this, options);
    };
    
    $.modalPopUp.defaults = {
        overlay: 50,
        overlayClass: 'modalOverlay',
        containerId: 'modalContainer',
        closeClass: 'modalClose'
    };    
    
    $.modalPopUp.impl = {
        opts: null,
        dialog: {},
        init: function (data, options) {
            this.opts = $.extend({},$.modalPopUp.defaults, options);
            // determine how to handle the data based on its type
			if (typeof data != 'object') return;
			
			// convert DOM object to a jQuery object
			data = data instanceof jQuery ? data : $(data);

			this.dialog.data = data;
			
			this.open();
			
			return this;
        },
        close: function () {
            // prevent close when dialog does not exist			
			if (!this.dialog.data) {
				return false;
			}
			
			// remove the remaining elements
			this.dialog.data.hide();
			this.dialog.overlay.remove();
			if (this.dialog.iframe) {
				this.dialog.iframe.remove();
			}

			// reset the dialog object
			this.dialog = {};
        },
        open: function () {
            // create the overlay
            this.dialog.overlay = $('<div>')
                .addClass(this.opts.overlayClass)
                .css({
                    opacity: this.opts.overlay / 100,
                    height: '100%',
					width: '100%',
					position: 'fixed',
					left: 0,
					top: 0,
					zIndex: 3000
				})
				.hide()
				.appendTo('body');
			
			this.dialog.data.css({
			        position: 'fixed',
			        zIndex: 3100,
			        left:'50%',
			        'margin-left' : -this.dialog.data.width() / 2 + 'px'
			    })
			    .hide();
		    
		    
		    var o = this;
		    $('.' + this.opts.closeClass).click(function() {
		        o.close();
		    });
		    
		    // fix issues with IE and create an iframe
			if ($.browser.msie && ($.browser.version < 7)) {
				this.fixIE();
			}
			
			// display the iframe
			if (this.dialog.iframe) {
				this.dialog.iframe.show();
			}
			
			this.dialog.overlay.show();
			this.dialog.data.show();
        },
        /*
		 * Fix issues in IE 6
		 */
		fixIE: function () {
			var wHeight = $(document.body).height() + 'px';
			var wWidth = $(document.body).width() + 'px';

			// position hacks
			var obj
			this.dialog.overlay.css({
			    position: 'absolute', 
			    height: document.documentElement.offsetHeight, 
			    width: document.documentElement.offsetWidth
			});			
			
			this.dialog.data.css({position: 'absolute'});

			// add an iframe to prevent select options from bleeding through
			this.dialog.iframe = $('<iframe src="javascript:false;">')
				.css($.extend(this.opts.iframeCss, {
					opacity: 0, 
					position: 'absolute',
					height: wHeight,
					width: wWidth,
					zIndex: 1000,
					width: '100%',
					top: 0,
					left: 0
				}))
				.hide()
				.appendTo('body');
		}
    };
})(jQuery);