if(jQuery)(
	function($){
		$.fn.extend({
			message: function(options){
				return this.each(function(){
					var settings = $.extend({
						html: '',
						type: 'error',	//result|confirm|error
						onComplete: function(){},
						okText: 'Aceptar',
						cancelText: 'Cancelar',
						auto: true
					}, options);
					var me = this;
					if(!this.msgdiv){
						$(this).css({
							position: 'fixed',
							top: '20px',
							left: 0,
							width: '100%',
							zIndex: 10000
						}).attr('align', 'center');
						this.msgdiv = $('<div style="width: 700px; display: none;"></div>').appendTo(this);
						this.st = 0;//0=>hidden, 1=>showing, 2=>shown, 3=>hidding
						this.confirmSt = 0;
						$(this).bind('hideMsg', function(e, callback){
							me.st = 3;
							me.confirmSt = 0;
							me.msgdiv.slideUp('fast', function(){me.st = 0; callback && callback();});
						});
						$(this).bind('confirmRet', function(e, result){
							settings.onComplete(result);
							$(me).trigger('hideMsg');
							me.confirmSt = 0;
						});
						$(this).bind('showMsg', _showMsg);
					}
					if(this.st == 1 || this.st == 2)
						return $(me).trigger('hideMsg', [function(){$(me).message(settings);}]);
					
					this.msgdiv.attr('class', settings.type+'Msg').html(settings.html);
					if(settings.type == 'confirm'){
						me.msgdiv.append('<br/>');
						$('<a href="javascript:;">' + settings.okText + '</a>').appendTo(me.msgdiv).one('click', function(e){$(me).trigger('confirmRet', [true]);}).focus();
						me.msgdiv.append(' | ');
						$('<a href="javascript:;">' + settings.cancelText + '</a>').appendTo(me.msgdiv).one('click', function(){$(me).trigger('confirmRet', [false]);});
						$(window).one('keypress', function(e){
							//if(e.keyCode == 13) ret(true);
							if(e.keyCode == 27) $(me).trigger('confirmRet', [false]);;
						});
					}
					function _showMsg(){
						me.msgdiv.slideDown('fast', function(){
							me.st = 2;
							if(settings.type != 'confirm' && !me.bEvent){
								me.bEvent = 1;
								$(window).one('click keypress', function(){
									if(settings.auto){
										me.bEvent = 0;
										me.st == 2 && $(me).trigger('hideMsg');
									}
								});
							}
						});
					}
					_showMsg();
					return this;
				});
			}
		});
		$(function(){
			var $msg = $('<div></div>').appendTo('body');
			window.msg = function(html, type, callback){
				return $msg.message({html: html, type: type, onComplete: callback});
			}
		});
			
	}
)(jQuery);
