var dialog = function(tmp_obj){
	var me = this;
	var msg = '';
	if(!is_object(tmp_obj)){//se o parametro for uma string
		msg = tmp_obj;
	}
	this.option = {message:msg,template:'alert',width:'',height:'',overflow:'',titulo:'Mensagem de aviso'};
	$.extend(me.option,tmp_obj);
	this.b = $('body');
	this.d = $(document);
	this.child;
	this.pre = 'dialog_';

	var html = '';
	switch(me.option.template){
		default:
		case 'alert':
				html += '<div class="'+me.pre+'title">'+me.option.titulo+'</div><div class="'+me.pre+'message">'+me.option.message+'</div><a href="#" id="'+me.pre+'ok" class="'+me.pre+'ok" title="OK"></a>';
				me.child = $('<div id="'+me.pre+'child" class="'+me.pre+'dialog">').html(html);
			break;
	}
	this.bg = $('<div id="'+me.pre+'bg">');
	me.b.prepend(bg);
	me.bg.css({display:'block',opacity:0,width:me.d.width(),height:me.d.height()});
	me.child.css({opacity:0});
	me.b.prepend(child);

	//Fecha ao apertar enter ou esc
	$(document).bind('keydown.'+me.pre,function(e){
		var key = e.keyCode;
		if(me.opened && ((key === 27) || (key === 13))){
			e.preventDefault();
			me.bg.click();
		}
	});
	me.bg.click(function(){
		me.child.fadeOut(200);
		me.bg.fadeOut(400);
		$(document).unbind('keydown.'+me.pre);
	});
	$('#'+me.pre+'ok').click(function(){
		me.bg.click();
	});

	var child_w = me.child.width();
	var child_h = me.child.height();

	var l = ((($(window).width() / 2) - (child_w / 2)) + $(document).scrollLeft());
	var t = ((($(window).height() / 2) - (child_h / 2)) + $(document).scrollTop());

	me.child.css({marginLeft:l,marginTop:t,opacity:0});
	me.bg.animate({opacity:0.7},200);
	me.child.animate({opacity:1},400,function(){
		me.opened = true;
	});
	me.opened = false;
};
var dialogX = function(){
	$('#dialog_bg').click();
};
var openAlert = function(msg){
	dialog(msg);
}
