jQuery.extend(
{
	alert: function(m,o)
	{
		o = o || {};
		o.prefix = o.prefix || 'jqa';
		o.id = o.id || 'jqa';
		o.buttons = o.buttons || {Aceptar : true};
		o.title = o.title || "";
		o.undertitle = o.undertitle || "";
		o.required = o.required || false;
		o.callback = o.callback || function(){};
		o.container =o.container || 'body';
		o.opacity = o.opacity || 0.6;
		o.height = o.height || "100%";
		o.width = o.width || "100%";
		o.widthAlert=o.widthAlert || false;
		o.textColor=o.textColor || false;
		o.zIndex = o.zIndex || 996;
		o.script=o.script || "";
		o.top=o.top || "30%";
		o.left=o.left || "50%";
		
		var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);
		var b = jQuery(o.container);
		if (o.script!="")
		{
			o.script='<script type="text/javascript">'+o.script+'</script>';
		}
		var fade = '<div class="'+ o.prefix +'fade" id="'+ o.id +'fade"></div>';
		widthAlert="";
		if(o.widthAlert)
		{
			widthAlert='width:'+o.widthAlert+'px;';
		}
		textColor="";
		var msgbox = '<div class="'+ o.prefix +'" id="'+ o.id +'" style="display:none;'+widthAlert+'"><div class="'+ o.prefix +'title">'+ o.title +'</div><div class="'+ o.prefix +'container"><div class="'+ o.prefix +'message"'+textColor+'>'+ m + o.script +'</div><div class="'+ o.prefix +'buttons" id="'+ o.id +'buttons">';
		for(var i in o.buttons) msgbox += '<button class="boton" name="'+ o.id +'button'+ i +'" id="'+ o.id +'button'+ i +'" value="'+ o.buttons[i] +'">'+ i +'</button>';
		msgbox += '</div><div class="'+ o.prefix +'undertitle">'+ o.undertitle +'</div></div></div>';
		var jqa = b.prepend(msgbox).children('#'+ o.id);
		var jqaf = b.prepend(fade).children('#'+ o.id +'fade');
		if(ie6) b.css({ overflow: "hidden" }).find("select").css({ visibility: "hidden" });
		if (o.container=='body')
		{
			jqaf.css({ height: o.height, width: o.width, position: "absolute", top: 0, left: 0, right: 0, bottom: 0, zIndex: o.zIndex, display: "none", opacity: o.opacity });
		}
		else
		{
			jqaf.css({ height: b.height(), width: b.width(), position: "absolute", top: 0, left: 0, right: 0, bottom: 0, zIndex: o.zIndex, display: "none", opacity: o.opacity });
		}
		thetop=((window.innerHeight-$("#"+ o.prefix).height())/2).toFixed(0);
		thetop=(thetop<0)?0:thetop;
		o.top=(thetop*100/window.innerHeight).toFixed(0)+"%";
		theleft=((window.innerWidth-$("#"+ o.prefix).width())/2).toFixed(0);
		o.left=theleft+"px";
		if(o.textColor)
		{
			if($("."+ o.prefix +"message>*").size()>0)
			{
				$("."+ o.prefix +"message>*").css("color",o.textColor+" !important");
			}
			else
			{
				$("."+ o.prefix +"message").css("color",o.textColor+" !important");
			}
			$("."+ o.prefix +"message").find(".label").css("color",o.textColor+" !important");
		}
		if(!o.widthAlert)
		{
			jqa.css({ position: (ie6)? "absolute" : "fixed", top: -50000});
			jqa.show();
			if($("."+ o.prefix +"message>*").size()>0)
			{
				if ($("."+ o.prefix +"message>*").width()>0)
				{
					$("."+ o.prefix).width($("."+ o.prefix +"message>*").width()+28);
				}
				else
				{
					$("."+ o.prefix).width($("."+ o.prefix +"message").width()+28);
				}
			}
			else
			{
				$("."+ o.prefix).width($("."+ o.prefix +"message").width()+28);
			}
			jqa.hide();
		}
		jqa.css({ position: (ie6)? "absolute" : "fixed", top: o.top, left: o.left, zIndex: o.zIndex+1 });
		var msg = jqa.children('.'+ o.prefix +'container').children('.'+ o.prefix +'message');
		jQuery('#'+ o.id +'buttons').children('button').click(function()
		{
			var clicked = o.buttons[jQuery(this).text()];
			if ((clicked==true && !o.required)||(clicked!=true))
			{
				jqa.remove(); 
				jqaf.fadeOut("slow",function()
				{
					jqaf.remove();
					if(ie6)
					{
						b.css({ overflow: "auto" }).find("select").css({visibility: "visible"});
					}
					o.callback(clicked,msg);
				});
			}
			else
			{
				inputs=$('.'+ o.prefix +'message').find('input:text').filter(function(){return $(this).val()=="";})
				if(inputs.size()>0)
				{
					$.alert("Es obligatorio rellenar todos los campos",{zIndex:998,callback:function(){inputs.get(0).focus()},title:"Atenci&oacute;n",id:"innerjqa"});
				}
				else
				{
					jqa.remove();
					jqaf.fadeOut("slow",function()
					{
						jqaf.remove();
						if(ie6)
						{
							b.css({ overflow: "auto" }).find("select").css({ visibility: "visible"});
						}
						o.callback(clicked,msg);
					});
				}
			}
		});
		jqaf.fadeIn("slow");
		jqa.fadeIn("fast");
		return jqa;
	}
});