(function($) {
	$.dialogs = {
		
		settings : {
			buttons : true,
			buttonOK : 'Aceptar',
			buttonCancel : 'Cancelar',
			formEnctype : '',
			formAction : '',
			formSend : false
			
		},
		
		alert : function(html, title, callback){
			if(html == null || html == ''){ return false; }
			options = {'title' : title }
			$.extend($.dialogs.settings, options);
			
			$('#dialog_html_transparent').remove();
			
			$.dialogs._create(html, options, 'alert', null);	
			$.dialogs._show();
			
			try{
			$.dialogs._callback(function(result){
				if(callback){ callback(result); callback = null; }
			});
			}catch(e){}
			
		},
		
		dialog : function(html, callback,  options){
			
			if(html == null || html == ''){ return false; }
			
			if(options){ $.extend($.dialogs.settings, options); }
			
			$.dialogs._create(html, options, 'dialog');
			
			$.dialogs._reposition();
			
			$.dialogs._show();
			
			$.dialogs._callback(function(result){
				if(callback){ callback(result); callback = null; }
			});
		},
		
		_create : function(html, options, type){
			
			if($("#dialog").length == 1){ $("#dialog").remove(); }
			
			switch(type){
				case 'alert':
					$("#head_outer").before(
						'<div id="dialog" class="dialog_alert" style="display:none;">' + 
							'<div id="dialog_true">'+
								'<div class="dialog title">' + (($.dialogs.settings.title) ?  + $.dialogs.settings.title : 'Mensaje de Karaoteca.com') + '</div>' +
								'<div class="dialog display" id="">' +
									html +
								'</div>' +
							'</div>' +
						'</div>' 
					);
					break;
				case 'dialog':
					$("#head_outer").before(
						'<div id="dialog" class="dialog" style="display:none;">' + 
							'<div class="dialog sup"></div>' + 
							'<div class="dialog content">' +
								'<form id="dialog_form" method="post" enctype="' + (($.dialogs.settings.formEnctype) ? $.dialogs.settings.formEnctype : '') + '" action="' + (($.dialogs.settings.formAction) ? $.dialogs.settings.formAction : '') + '">' +
									'<div class="dialog display">' +
										'<div class="dialog information">' +
											html +
										'</div>' +
									'</div>' +
									(($.dialogs.settings.buttons == true) ?  '<div class="dialog buttons"><input value="' +$.dialogs.settings.buttonOK + '" id="dialog_true" type="submit" /> <input type="button" value="' + $.dialogs.settings.buttonCancel + '" id="dialog_false" /></div>' : '') +
								'</form>' + 
							'</div>' + 
							'<div class="dialog inf"></div>'+
						'</div>' 
					);
					break;
			}
		},
		
		_show : function(){
			try{
				$("#dialog").show(0);	
				return true;
			}catch(e){}
		},
		
		_hide : function(){
			try{
				$("#dialog").remove(0);	
			}catch(e){}
		},
		
		_callback : function(callback){
			
			$("#dialog_true").click(function(event) {
				
				if($.dialogs.settings.formSend == false){
					event.preventDefault();
					var data = $("form#dialog_form").serialize();
					$.dialogs._hide();
					if(data == ''){ data = true; }
					$('#dialog_html_transparent').remove();
					if(callback){  callback(data); data = null, callback = null;}
				}
			});
			
			$("#dialog_false").click( function() {
				$.dialogs._hide();
				$('#dialog_html_transparent').remove();
				if( callback ) callback(false);
			});
		},
		
		_reposition : function(){
			var w = $(window), d = $("#dialog"), width = w.width() / 2 - d.width() / 2, height = w.height() / 2 - d.height() / 2;
			d.css({'top' : height, 'left' : width, 'position' : 'fixed'});
			var dPosition = d.position(); d.css({position : 'absolute', top : dPosition.top + height});
		},
		
		_sleep : function(milliseconds) {
		  var start = new Date().getTime();
		  for (var i = 0; i < 1e7; i++) {
			if ((new Date().getTime() - start) > milliseconds){
			  break;
			}
		  }
		}
	}
	
	kAlert = function(html, title){
		$.dialogs._hide();
		$.dialogs.alert(html,title);
	}
	
	kDialog = function(html, callback, title, options){
		$.dialogs.dialog(html,callback, title, options);
	}
	
})(jQuery);
