var Fnc = {
	$w: $(window),
	$d: $(document),
	
	init: function() {
		_this = Fnc;

		_this.badbox();
		_this.fechar();
	},
	
	carregando: function() {
		$("#carregando").ajaxStart(function(){
			$(this).show();
		}).ajaxStop(function(){
			$(this).hide();
		});
	},
	
	fechar: function() {
		$('a.fechar').livequery('click',function() {
			$("#lightbox").remove();
			$("#BGoverlay").remove();
			$("select").css("visibility","visible");
			//($("link.print").get(0)) ? alert('yes') : alert('no');
			$("link.print").remove();
			return false;
		});
	},
	
	badbox: function() {
		$('.badbox').livequery('click',function(e) {
			// <![CDATA[
			e.preventDefault(); //como se fosse o return false
			$("body").prepend("<div id='BGoverlay'><\/div><div id='lightbox'><p id='carregando'>Loading...<br /><img src='../pics/loading.gif' alt='' /><\/p><\/div>");
			// ]]>
			
			var heightWindow = _this.$w.height();
			var scrollY = _this.$d.scrollTop();

			if (scrollY == 0) {
				$("#lightbox").css({top:'20px'});
			} else {
				var top = heightWindow/2 + scrollY - 150;
				$("#lightbox").css({top:top});
			}
			
			Fnc.carregando();
						  
			var edificio = $("#select_edificio").val();
			var etapa = $("#select_etapa").val();
			$("head").append("<link class='print' rel='stylesheet' type='text/css' href='css/print.css' media='print' />");
			$("#lightbox").fadeIn("normal").empty().load("availability.php", { edificio: edificio, etapa: etapa }, function() { 
				$("table#dados tr:odd td").css("backgroundColor","#e2e2e2"); 
				$("select").css("visibility","hidden"); 
			});
		});
	}

}

$(document).ready(function(){
	Fnc.init();
});