// JavaScript Document
$(function(){

	var hash = location.search;
	var endereco = location.pathname;
	var array = endereco.split('/');
	var ultimo = array.pop();
	var isola = ultimo.split('.');
	var clicado = isola[0];
	
	if (clicado==='empresa' ){
		$('#slider').nivoSlider({effect:'fold', animSpeed:2000, pauseTime:6000, controlNav:false});
	}
	
	if (clicado==='tecnologia' ){
		$('#tecs').nivoSlider({effect:'fold', animSpeed:2000, pauseTime:6000, controlNav:false});
	}
		if (clicado==='contato' ){
			$('a[rel*=facebox]').facebox(); 
		}
	
	$('.loader').vAlign();
	
	if (jQuery.support.opacity)
	{
		$('h1, #inicial img').fadeIn();
	} else {
		$('h1, #inicial img').show();
	}
	
	$('.menu li').hover(function(){
			$(this).children('div').stop().animate({width:"100%", opacity:1}).css({'z-index':10});
			$(this).css({'z-index':15});	
		}, function(){
			$(this).css({color:'#000'}).children('div').stop().animate({opacity:0});
		})
	
	$('#trabalhos-mini a').not('#seta img').click(function(){
		var nomeimg = $(this).attr('href');
		var caminhoimg = 'img/trabalhos/' + nomeimg;
		var bigimg = 'img/trabalhos/'+nomeimg.substr(0,2) + '_big.jpg';
		var alt = $(this).attr('alt');
		$('h6').fadeOut('fast', function(){

			$('.loader').fadeIn();
			$('#foto-trabalho img').animate({width:'hide'}).attr('src', caminhoimg).load(function(){
				$('.loader').fadeOut();
				$('#foto-trabalho a').attr('width', $(this).width()).attr('height', $(this).height()).attr('alt', alt);
				$(this).animate({ width:'show'});
			});
		});
		return false;
	  })
	
	$('#trabalhos-mini a img').not('#seta img').hover(function(){
		$('#trabalhos-mini img').not(this).stop().fadeTo('slow', 0.5);
	}, function(){
		$('#trabalhos-mini img').stop().fadeTo('fast', 1);});
		
	$('#seta img').hover(function(){
		var arq = $(this).attr('alt');
		$(this).attr('src', 'img/'+arq+'-hover.jpg').stop().animate({opacity:1},500);	
	}, function(){
		var arq = $(this).attr('alt');
		$(this).attr('src', 'img/'+arq +'.jpg').stop().animate({opacity:0.7},500);		;
	})
	
	$('#dir').click(function(){
		$('.loader').hide();
		$('h6').fadeIn();
		$(this).fadeOut();
		$('#trab1').fadeOut('normal', function(){
			$('#trab2').fadeIn();			
		});
		$('#esq').fadeIn();
		return false;
	})
	
	$('#esq').click(function(){
		$(this).fadeOut();
		$('.loader').fadeOut();
		$('h6').fadeIn();

		$('#trab2').fadeOut('normal', function(){
			$('#trab1').fadeIn();			
		});
		$('#dir').fadeIn();
		return false;
	})
		
	$('#enviar_contato').click(function(){
			var nome = $("#nome").val();
			var email = $("#email").val();
			var telefone = $("#telefone").val();
			var msg= $("#msg").val();
			if (nome!='' & email!='' & msg!='')
			{
				$('.loading').show();
				$('form')[0].reset();
				$('#form-contato').fadeTo("slow", 0.5);					
				$.post("envia_contato.php",{nome: nome, email:email, msg:msg, telefone:telefone},
					function(retorno){
						$('.loading').fadeOut();
						$('#resposta').html(retorno);
						$('#form-contato').fadeTo("slow", 1);					
				}) 
			}else{
						alert("Existem campos incompletos no formulário. Favor preencher todos.");
						return false;
			}
			return false;
	})
	
var altura, largura, padrao, razao, novaaltura, recuo;	


$('#slides').find('img').each(function(){
  	altura = $(this).attr('height');
  	largura = $(this).attr('width');	
	padrao = 185;
	razao = 185 / largura;
	novaaltura = Math.ceil(altura * razao);
	$(this).css({'width':185, 'height':novaaltura});
	$(this).fadeTo('fast',.5);
})

	function changeSlide( newSlide){
		
		clearTimeout(slideTimeout);
		
		currSlide = newSlide;	
		
		if (currSlide > maxSlide) currSlide = 0;
		else if (currSlide < 0) currSlide = maxSlide;
		
		$slideReel.animate({
			left : currSlide * - 257
		}, 1000, 'swing', function(){
			
			if (currSlide == 0) $slidePrevNav.fadeOut();
			else $slidePrevNav.fadeIn();
			
			if (currSlide == maxSlide) $slideNextNav.fadeOut();
			else $slideNextNav.fadeIn();
			
			if (activeSlideshow) slideTimeout = setTimeout(nextSlide, 5000);
			
		});
		
		slideAnimado = currSlide + 1;
  	  	altura = $('#'+slideAnimado).find('img').attr('height');
  		largura = $('#'+slideAnimado).find('img').attr('width');	
		$('#slides li').animate({'paddingTop':250},500).removeClass('atual').width('auto').height('auto').find('img').animate({width:padrao ,height:novaaltura, opacity:.5},500,'easeInCubic');
		$('#'+ slideAnimado).css({'width':530, 'height':455}).animate({paddingTop:50},500).find('img').css({'display':'block', 'margin':'auto'}).animate({width:largura,height:altura, opacity:1},500,'easeInCubic');			
	}
	
	function nextSlide(){
		changeSlide(currSlide + 1);	
	}
	
	var activeSlideshow = true,
	currSlide = 0,
	slideAnimado,
	slideTimeout,
	$slideshow = $('#slideshow'),
	$slidePrevNav =$('#slideshow-prev'),
	$slideNextNav = $('#slideshow-next'),	
	$slideReel = $slideshow.find('#slides');
	maxSlide = $slideReel.children().length - 1;
	
	$slidePrevNav.click(function(e){
			e.preventDefault();
			activeSlideshow = false;
			changeSlide(currSlide - 1);
	})
	
	$slideNextNav.click(function(e){
			e.preventDefault();
			activeSlideshow = false;
			changeSlide(currSlide + 1);
	})	
	
slideTimeout = setTimeout(nextSlide,5000);		
	
})


$.fn.vAlign = function() {
	return this.each(function(i){
	var h = $(this).height();
	var oh = $(this).outerHeight();
	var mt = (h + (oh - h)) / 2;	
	$(this).css("margin-top", "-" + mt + "px");	
	$(this).css("top", "50%");
	$(this).css("position", "absolute");	
	});	
};(jQuery);
