/* Copyright (c) 2009 Alvaro A. Lima Jr http://alvarojunior.com/jquery/joverlay.html
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Version: 0.3 (Abr 1, 2009)
 * Requires: jQuery 1.2+
 */

(function($) {
	
	var isIE6 = $.browser.msie && parseInt($.browser.version) == 6; // =(
	
	$.fn.jOverlay = function(options) {
		
		// Set Options
		var options = $.extend({}, $.fn.jOverlay.options, options);

		var imgLoading = options.imgLoading ?
						"<img id='jOverlayLoading' src='" + options.imgLoading + "' alt='loading...' title='loading...' border='0' />"
						: "";

		var html = isIE6 ? "<iframe id='jOverlayIframe'></iframe><div id='jOverlay'></div><div id='jOverlayContent'>" + imgLoading + "</div>"
						 : "<div id='jOverlay'></div><div id='jOverlayContent'>" + imgLoading + "</div>";

		var overlayWidth = $(window).width();
		var overlayHeight = $(document).height();

		$('body').prepend(html);

		$("#jOverlayContent").css({
			position: isIE6 ? 'absolute' : 'fixed',
			zIndex: options.zIndex + 1
		});

		// Element
		if ( this.is('*') ) {

			$('#jOverlayContent').css({
				height: this.height(),
				width: this.width()
			}).html( this.hide().clone().addClass('jOverlayClone').show('slow') );
			
		}

		// Element + AJAX LOAD
		if (this.is('*') && options.url) {

			if (imgLoading) {
				$("#jOverlayContent .jOverlayClone").html(imgLoading);
			}

			$("#jOverlayContent .jOverlayClone").load(options.url, options.data);

		} else {

			// AJAX ONLY - GET/POST
			if (options.url) {

				$.ajax({
					type: options.method,
					data: options.data,
					url: options.url,
					success: function(response) {

						$("#jOverlayContent").hide().html(response);

						if (options.center) {
							$.center("#jOverlayContent");
						}

						$("#jOverlayContent").show('show');

					}
				});

			} else if ( !this.is('*') ) {
				$('#jOverlayContent').html("<div style='background:#FFF;'>Element not found!<br/>Press ESC!</div>");
			}

		}

		// "hack" for IE 6
		if (isIE6) {
			$('#jOverlayIframe').css({
				backgroundColor: '#FFF',
				border: 'none',
				filter: 'alpha(opacity=0)', // IE =(
				opacity: 0, // Good Browser =D
				position: 'absolute',
				top: '0px',
				left: '0px',
				zIndex: options.zIndex - 5,
				width: overlayWidth + 'px',
				height: overlayHeight + 'px'
			}).show();
		}

		// Overlay Style
		$('#jOverlay').css({
			backgroundColor : options.color,
			display: 'block',
			position: isIE6 ? 'absolute' : 'fixed',
			top: '0px',
			left: '0px',
			filter : 'alpha(opacity='+ (options.opacity * 100) +')', // IE =(
			opacity : options.opacity, // Good Browser =D
			zIndex: options.zIndex,
			width: overlayWidth + 'px',
			height: !isIE6 ? $(window).height() + 'px' : overlayHeight + 'px'
		});

		// Click to close
		if (options.bgClickToClose) {
			$('#jOverlay').click($.closeOverlay);
		}

		// Window scroll
		if (isIE6) {
			$(window).scroll(function(){
				if (options.center) {
					$.center('#jOverlayContent');
				}
			});
		}

		// Window resize
		$(window).resize(function(){

			var oWidth = $(window).width();
			var oHeight = $(document).height();

			if (isIE6) {
				$('#jOverlayIframe').css({
					width: oWidth + 'px',
					height: oHeight + 'px'
				});
			
			}
			
			$('#jOverlay').css({
				width: oWidth + 'px',
				height: oHeight + 'px'
			});
			
			if (options.center) {
				$.center('#jOverlayContent');
			}
		});

		// Press ESC to close
		$(document).keydown(function(event){
			if (event.keyCode == 27) {
				$.closeOverlay();
			}
		});

		if (options.center) {
			$.center('#jOverlayContent');
		}

	};
	
	$.center = function(element) {

		var element = $(element);

		var elemHeight = element.height();
		var elemWidth = element.width();

		var winWidth = $(window).width();
		var winHeight =  $(window).height();
		var scrollLeft = $(window).scrollLeft();
		var scrollTop = $(window).scrollTop();

		var offLeft = (scrollLeft + Math.floor((winWidth-elemWidth)/2));
		var offTop = (scrollTop + Math.floor((winHeight-elemHeight)/2));

		var t = offTop - scrollTop;
		var	l = offLeft - scrollLeft;

		if (isIE6) {
			t = ((offTop != null && offTop > 0) ? offTop : '0');
			l = ((offLeft != null && offLeft > 0) ? offLeft : '0');
		}

		element.css({top :t + 'px' , left: l + 'px'});
	};

	// Options default
	$.fn.jOverlay.options = {
		method : 'GET',
		data : '',
		url : '',
		color : '#000',
		opacity : '0.6',
		zIndex : 9999,
		center : true,
		imgLoading : '',
		bgClickToClose : false
	};

	// Close
	$.closeOverlay = function() {
		$('#jOverlayContent, #jOverlayIframe, #jOverlay').remove();
	};

})(jQuery);