function monta_menu(){
	var subTimer;
	$("#menu a").mouseover(function(){
		clearTimeout(subTimer);
		var iten = $(this).attr("name");
		if ( $("#submenu #"+ iten ).length > 0 ){
			$("#submenu").slideDown('slow');
			$("#submenu ul").hide();
			$("#submenu #"+ iten ).fadeIn();
		} else {
			$("#submenu").slideUp();
			$("#submenu ul").fadeOut();
		}
	});
	
	$("#menu a").mouseout(function(){
		subTimer = setTimeout('fechar()', 1000);		
	});
	$("#submenu, #submenu li, #submenu li a").mouseover(function(){
		clearTimeout(subTimer);
	});
	$("#submenu").mouseout(function(){
		subTimer = setTimeout('fechar()', 1000);						
	});
	
	$("#twitter").find("a").click(function(){
		$(this).attr("target","_blank");								
		return true;
	});
	
	//*************************************************************************
	//FUNÇÕES AJAX DO MENU
	$("#carregando").bind("ajaxSend", function(){
		$(this).fadeIn();
	}).bind("ajaxComplete", function(){
		$(this).fadeOut();
	}).bind("error", function(){
		alert("Erro");
	});
	$('#carregando').hide();
	$('#centro').hide();
	//função para converter os links do menu para javascript
	$("#menu, #submenu").find("a").click(function(){
		var pagina = "ajax/" + $(this).attr("href");
		//CARREGA A PÁGINA VIA AJAX
		if ( $(this).attr("href") != "" && $(this).attr("href") != "#" && $(this).attr("target") != "_blank" ){
			$('#centro').stop().slideUp();
			$.get(pagina,function(response){
				// resposta
				$('#banner_mascara').css("background","url(imagens/mascara2.png) 0 0 no-repeat");
				$('#centro').slideDown()
							.html(response);							
			})
			return false;
		}
	});
}//function monta_menu(){

function fechar(){
		$("#submenu").slideUp();
		$("#submenu ul").fadeOut();
}


//FUNCOES PARA AS GALERIAS
function makeScrollable(wrapper, scrollable){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);
	
	// Hide images until they are not loaded
	scrollable.hide();
	var loading = $('<div class="loading">Carregando...</div>').appendTo(wrapper);
	
	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('img');
		var completed = 0;
		
		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;	
		});
		
		if (completed == images.length){
			clearInterval(interval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				
				loading.hide();
				// Remove scrollbars	
				wrapper.css({overflow: 'hidden'});						
				
				scrollable.slideDown('slow', function(){
					enable();	
				});					
			}, 1000);	
		}
	}, 100);
	
	function enable(){			
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = scrollable.find("li").height() * scrollable.find("li").length;
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		//When user move mouse over menu			
		wrapper.mousemove(function(e){
			var wrapperOffset = wrapper.offset();
			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;	
			
			if (top < 0){
				top = 0;
			}
			
			wrapper.scrollTop(top);
		});		
	}
}
