/* 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.6 (Abr 23, 2009)
 * Requires: jQuery 1.3+
 */

(function($) {

	var isIE6 = $.browser.msie && $.browser.version == 6.0; // =(
	var jOverlayTimer = null;

	$.fn.jOverlay = function(options) {

		// Set Options
		var options = $.extend({}, $.fn.jOverlay.options, options);

		// Clear Timer
		if (jOverlayTimer != null) { clearTimeout( jOverlayTimer ); }

		var element = this.is('*') ? this : '#jOverlayContent';
		var position = isIE6 ? 'absolute' : 'fixed';

		var imgLoading = options.imgLoading ? "<img id='jOverlayLoading' src='"+options.imgLoading+"' style='position:"+position+"; z-index:"+(options.zIndex + 9)+";'/>" : '';

		$('body').prepend(imgLoading + "<div id='jOverlay' />"
			+ "<div id='jOverlayContent' style='position:"+position+"; z-index:"+(options.zIndex + 5)+"; display:none;'/>"
		);

		// CSS and Centered
		// FIX
		$('#jOverlayLoading').load(function(){
			if (options.center) {
				$.center(this);
			}
		});

		//IE 6 fix
		if (isIE6) {
			$("select").hide();
			$("#jOverlayContent select").show();
		}

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

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

			$('#jOverlayContent').html( this.addClass('jOverlayChildren').show() ).show();

			if (options.center) {
				$.center('#jOverlayContent');
			}
			
			// Execute callback
			if ( !options.url && $.isFunction( options.success ) ) {
				options.success( this.html() );
			}

		}

		// Ajax
		if (options.url) {

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

					$('#jOverlayLoading').fadeOut(600);

					$( element ).html(responseText).show();

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

					// Execute callback
					if ($.isFunction( options.success )) {
						options.success(responseText);
					}

				}
			});

		}

		// :(
		if (isIE6) {

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

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

				$('#jOverlay').css({
					width: $(window).width() + 'px',
					height: $(document).height() + 'px'
				});

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

			});

		}

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

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

		// Timeout (auto-close)
		if (Number(options.timeout) > 0) {

			// time in millis to wait before auto-close
			// set to 0 to disable
			jOverlayTimer = setTimeout( $.closeOverlay, Number(options.timeout));

		}
		
	};

	$.center = function(element) {

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

		element.css({
			width: elemWidth + 'px',
			marginLeft: '-' + (elemWidth / 2) + 'px',
			marginTop: '-' + elemHeight / 2 + 'px',
		 	height: 'auto',
         	top: !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + "px",
         	left: '50%'
		});

	};

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

	// Close
	$.closeOverlay = function() {

		if (isIE6) { $("select").show(); }

		$('#jOverlayContent .jOverlayChildren').hide().prependTo( $('body') );
		$('#jOverlayLoading, #jOverlayContent, #jOverlay').remove();
	};

})(jQuery);