/*
	Modal Dialog v2.1.1.
	Written by All Web Promotion, Inc. 2008-2009.
*/

;(function($) {

	/* SETUP */

	$.modal = function (options) {

			var defaults = {
			'overlayId'				: 'dialog-overlay',
			'overlayCss'			: {},
			'overlayFadeIn'			: null,
			'overlayOpacity'		: 50,
			'overlayClose'			: true,
			'containerId'			: 'dialog-container',
			'containerFadeIn'		: 300,
			'containerHorizontal'	: 'center',
			'containerVertical'		: 'center',
			'closeIds'				: '.close',
			'onClose'				: null
		};

		this.settings = $.extend({}, defaults, options);
		this.open = false;
		var opacity = this.settings.overlayOpacity / 100;

		this.isie6 = function () { 
			return ($.browser.msie 
        				&& /MSIE 6\.0/i.test(window.navigator.userAgent) 
        				&& !/MSIE 7\.0/i.test(window.navigator.userAgent));
		};

		this.isie6or7 = function () {
			return $.browser.msie && ((parseInt($.browser.version) == 6) || (parseInt($.browser.version) == 7));
		};

		this.overlay = $('<div>')
			.attr('id', this.settings.overlayId)
			.css($.extend({},{
				'opacity'			: opacity,
				'height'			: '100%',
				'width'				: '100%',
				'position'			: 'fixed',
				'left'				: '0',
				'top'				: '0',
				'zIndex'			: '3000',
				'backgroundColor'	: '#000',
				'overflow'			: 'auto'  /* covers flash */
			},this.settings.overlayCss))
			.hide()
			.appendTo('body');

		if (this.isie6or7()) {
			$('body','html').css({height: '100%', width: '100%'});
			$('html').css('overflow','hidden');
			var wHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) + 'px';
			var wWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) + 'px';
			$(this.overlay).css({
				'position'	: 'absolute',
				'height'	: wHeight,
				'width'		: wWidth
			});
			this.iframe = $('<iframe src="javascript:false;">')
				.css({
					'opacity'	: '0', 
					'position'	: 'absolute',
					'height'	: wHeight,
					'width'		: wWidth,
					'zIndex'	: '1000',
					'width'		: '100%',
					'top'		: '0',
					'left'		: '0'
				})
				.hide()
				.appendTo('body');
		}

		/* end SETUP */

		/* functions */

		this.initWindow = function (windowHtml) {
			this.container = $(windowHtml)
				.css({
					'position'	: 'fixed', 
					'zIndex'	: '3100'
				})
				.css('visibility', 'hidden')
				/*.hide()*/
				.appendTo('body');
		}

		this.show = function () {
			var me = this;
			if (!this.open) {
				if (this.iframe) {
					this.iframe.show();
				}
				this.open = true;
				if (this.settings.overlayFadeIn) {
					this.overlay.fadeIn(this.settings.overlayFadeIn);
				} else {
					this.overlay.show();
				}
				if (this.settings.overlayClose) {
					this.overlay.click(function(e){
						me.close();
						return false;
					});
				}
			}
			this.showWindow();
			return this;
		};

		this.showWindow = function () {
			var me = this;
			/*var containerHeight = this.container.height();
			var windowHeight = $(window).height();
			if (containerHeight > windowHeight) {
				$(this.container).height(windowHeight - 14);
			}*/
			if (this.settings.containerHorizontal == 'center') {
				this.container.css({
					'left'			: '50%',
					'marginLeft'	: (0 - (this.container.width() / 2)) + 'px'
				});
			}
			if (this.settings.containerVertical == 'center') {
				this.container.css({
					'top'			: '49%',
					'marginTop'		: (0 - (this.container.height() / 2)) + 'px'
				});
			} else if (this.settings.containerVertical == 'top') {
				this.container.css({
					'top'			: '5px',
					'marginTop'		: '0'
				});
			}
			if (this.isie6or7()) {
				this.container.css({
					'position'	: 'absolute'
				});
			}
			this.container.hide().css('visibility', 'visible');
			if ($.browser.msie && ($.browser.version < 8)) {
				this.container.fadeIn(this.settings.containerFadeIn);
			} else {
				this.container.slideDown(this.settings.containerFadeIn);
			}
			$(this.settings.closeIds).click(function(e){
				me.close();
				return false;
			});		
		};

		this.closeWindow = function () {
			if (this.container) {
				$(this.settings.closeIds).unbind('click');
				this.container.remove();
				this.container = null;
			}		
		};

		this.close = function () {
			$('#' + this.settings.overlayId).unbind('click');
			this.closeWindow();
			if ($.isFunction(this.settings.onClose)) {
				this.settings.onClose();
			}
			if (this.overlay) {
				this.overlay.unbind('click');
				this.overlay.remove();
			}
			if (this.iframe) {
				this.iframe.remove();
			}
			if (this.isie6or7() && (this.container || this.overlay)) {
				$('body','html').css({height: 'auto', width: 'auto'});
				$('html').css('overflow','');
			}
			this.overlay = null;
			this.container = null;
			this.iframe = null;
			this.open = false;
			return this;
		};

		this.makePx = function (dimension) {
			var units = 'px';
			if (!(dimension.substring(dimension.length, units.length) == units)) {
				dimension = dimension + units;
			}
			return dimension;
		};

		this.getId = function (idOrClass) {
			return {
				'kind'	: 'id',
				'id'	: idOrClass
			};
		};

		this.makeId = function (idName) {
			var units = '#';
			if (!(idName.substr(0, units.length) == units)) {
				idName = units + idName;
			}
			return idName;
		};

		this.makeClass = function (className) {
			var units = '.';
			if (!(className.substr(0, units.length) == units)) {
				className = units + className;
			}
			return className;
		};

		return this;

	};

	$.fn.setIdentifier = function (identifier) {
		var idName = '';
		var className = '';
		//$(this).attr('id', idName);
		//$(this).attr('class', className);
		return $(this);
	};

})(jQuery);
