/*
 * http://maquinar.us
 * Nishio
 * 
 */

(function(jQuery){
	
	jQuery.maqAlert = function(type, messageOrUrl){
		if($('#maqAlert-main').length==0){
			html = "<div id='maqAlert-main' style='position:absolute; z-index:1000; top:0; left:0; width:100%; height:100%; display:block'>";
			html += "<div class='background' style='position:absolute; top:0; left:0; width:100%; height:100%; background:#000000; overflow:hidden; opacity:0.65; -moz-opacity: 0.65; filter: alpha(opacity=65)'></div>";
			html += "<div class='main' style='position:absolute; display:block; left:50%; top:30px; width:600px; margin-left:-300px'><div class='content' ></div>";
			html += "<a href='javascript' class='ok'>Ok</a><a href='javascript' class='yes'>Sim</a><a href='javascript' class='no'>Não</a></div></div>";
			$('body').prepend(html);
			$("#maqAlert-main div.main a.ok").click(function(){
				jQuery.maqAlert('close');
				return false;
			});
			$("#maqAlert-main div.main a.yes").click(function(){
				jQuery.maqAlert('close');
				return false;
			})
			$("#maqAlert-main div.main a.no").click(function(){
				jQuery.maqAlert('close');
				return false;
			});
			// TODO: Desabilitar TAB
			/*
			$("#maqAlert-main").keypress(function(event) {
				alert(event.which);
				return false;
			});
			*/					
			$(document).resize(function(){

				//jQuery.maqAlert_resize(); 
			});
		}
		$("#maqAlert-main").show();
		alertDiv = $("#maqAlert-main div.main");
		alertDiv.find('a').hide();
		alertDiv.show();
		$('body').css('overflow', 'hidden');
		switch(type){
			case "loading":
				alertDiv.children('div.content').html("Carregando...");
			break;
			case "message":
				alertDiv.find('a.ok').show().focus();
				alertDiv.children('div.content').html(messageOrUrl);
			break;
			case "url":
				alertDiv.find('a.ok').show().focus();
				$.get(messageOrUrl, function(data) {
					$("#maqAlert-main div.main div.content").html(data);
					jQuery.maqAlert_resize();
				});
			break;
			case "confirm":
				alertDiv.find('a.yes').show().focus();
				alertDiv.find('a.no').show();
			break;
			case "close":
				$("#maqAlert-main").hide();
				$('body').css('overflow', 'auto');	
			break;
		}
		jQuery.maqAlert_resize();
	};	
	
	jQuery.maqAlert_resize = function(){
		//$("#maqAlert-main").width($('body').width());
		//$("#maqAlert-main").height($('body').height());
		$("#maqAlert-main div.main").css("margin-left", "-" + Math.round($("#maqAlert-main div.main").width() * .5) + "px");
		$("#maqAlert-main div.main").css("margin-top", "-" + Math.round($("#maqAlert-main div.main").height() * .5) + "px");
	}
	

})(jQuery);



