$(document).ready(function() {
	
	/* Links externos */
	$("a[rel^='externo']").attr("target","_blank");
	
	/* Limpar inputs */	
	$("input:text").val('');
	//$("textarea").html('');
	
	$("textarea").focus(function(){
		$(this).val("");
	});

	$(".nav li ul").hover(function(){
			$(this).prev().toggleClass("sub-nav-ativo");
		});    
   
	 
		/* Destaques */	
	$(".banner-foto").scrollable({size:1, clickable: false, circular:true}).navigator({navi:".controle",naviItem:"a",activeClass:"active"}).autoscroll({ autoplay: true, interval:8000 });
		
		
	/* Vendas */
	var hash = window.location.hash.substring(1);
	if (hash) {
		$("#categorias-vendas ul").hide();
		$("ul","#"+hash).slideToggle("slow");
	}
	else {
		$("#categorias-vendas ul").slideToggle("slow");
	}
	$(".categoria").click(function(){
		$(this).next().slideToggle("300");
		return false;
	});
		
	//Historia
	$(".show").scrollable({size:1, clickable: false}).autoscroll({autoplay: true, interval:7000});
	

	
	//Aeronaves
	//$(".mini-fotos").scrollable({size:1, clickable: false}).autoscroll({autoplay: true, interval:7000});
	$(".mini-fotos").scrollable();

	

	$(".items img").click(function() {

	// see if same thumb is being clicked
	if ($(this).hasClass("active")) { return; }

	// calclulate large image's URL based on the thumbnail URL (flickr specific)
	var url = $(this).attr("src").replace("mini", "ampl");
	
	
	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $("#image_wrap").fadeTo("medium", 0.5);

	// the large image from www.flickr.com
	var img = new Image();


	// call this function after it's loaded
	img.onload = function() {

		// make wrapper fully visible
		wrap.fadeTo("fast", 1);

		// change the image
		wrap.find("img").attr("src", url);

	};

	// begin loading the image from www.flickr.com
	img.src = url;

	// activate item
	$(".items img").removeClass("active");
	$(this).addClass("active");

// when page loads simulate a "click" on the first image
}).filter(":first").click();

// in the above example replace the url assignment with this line
var url = $(this).attr("alt");


/* Contato */
	$("#enviar").click(function(){ $("#formulario").submit(); return false; });
	$("#formulario").submit(function(){
		var error = 0;
		$(".obrigatorio").each(function(){
			if (this.value == "") {
				$(this).addClass("alerta");
				error = 1;
			}
		});
		if (error > 0) {
			alert("Preencha os campos obrigatórios!");
			return false;
		}
		else {
			$.post("ajax/enviar_email.php",
			   {
				'nome': $("#nome").val(),
				'email': $("#email").val(),
				'tel': $("#tel").val(),
				'assunto': $("#assunto").val(),
				'msg': $("#msg").val()
			   },
			   function(data) {
				   if (data == 1) {
					   alert("Mensagem enviada com sucesso!");
					   _gaq.push(['_trackPageview', '/ajax/ajax_email.php']);
					   $("input").removeClass("alerta");
						$("input:text").val('');
						$("textarea").html('');
				   }
				   else if (data == 0) {
					   alert("Não foi possivel enviar a mensagem, tente mais tarde.");
				   }
			   }
			);
			return false;
		}
	});


	
});
