/* 
 * 
 * $.ventana - jQuery Plugin
 * 
 * 	Copyright (c) 2009 Antonio Javier Martinez
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * 	
 * Requires:
 * 		jQuery.dialog Plugin and Core Ui.
 * 
 * Optional:
 * 
 * 		jQuery.metadata
 * 		jQuery.shadow
 * 
 * Usage:
 * 
 * 		
 * 		<div id="loading" class="{style:'border 1px solid', message:'hola'}"/>
 * 		$('#loading').ventana();
 * 
 * 		<div id="loading"/>
 * 		$('#loading').ventana({style:'border 1px solid', message:'hola'});
 * 
 * 
 * 
 * 
 * 
 * Review:
 * 
 * 		- Si se pasa id:	 Creará un div con ese id
 * 		-
 */

(function($) {

	$.fn.ventana = function(options) {
	
		$this = $(this);
		
		var opts = $.extend({}, $.fn.ventana.defaults, options);	
		o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
		
		var rand = o.id + rand();
		var rand_id = "#" + rand;

		if( $(rand_id).length > 0 ){
			$(rand_id).dialog('destroy');
			$(rand_id).remove();
		}
		
		$this.append('<div id="' + rand + '" style="display:none"/>');
		
		if( o.html != '' ) { $(rand_id).html(o.html); };
		if( o.url  != '' ) { $(rand_id).load(o.url);  };
		
		$(rand_id).dialog({
				autoOpen: false,
				show: o.effect,
				modal: o.modal,
				resizable: o.resizable,
				draggable: o.draggable,
				width: o.width,
				height: o.height, 
				title: o.title,
				close: function(ev, ui) { $(this).remove(); },
				buttons: {
					'Cerrar': function() { $(this).dialog('close').remove();}
				},
				overlay: { 
					opacity: 0.2, 
					background: "black" 
				} 
		}).dialog('open').show('slow');
		
		return $this;
		
		function rand(){
			return	Math.round( Math.random() * 10000 );
		}
		
	}
	
	$.fn.ventana.defaults = {
		modal: false,
		resizable: false,
		draggable: true,
		effect: 'explode',
		id: 'ventana',
		url: '',
		html: '',
		width: 300,
		height: 100,
		delay:0,
		title: 'Titulo'
	};

})(jQuery);