var scrollCase = 0;

$(document).ready(function() {
	if(paginaAtual == 'contato'){
		ajustarAltura('#contatos > #formulario');
		ajustarAltura('#contatos > #mapa-porto-alegre');
		ajustarAltura('#contatos > #mapa-sao-paulo');	
	}
	
	ajustarAltura = function(str, pagina){
		var i = $(str).attr('offsetHeight');
		if(i % 50 != 0){
			i += 49 - i % 50;
		}
		
		if(pagina == 'interna'){
			$(str).css('height', (i)+'px');
		}else{
			pad = $(str).css('padding-top').split('px').join('');
			$(str).css('height', (i-pad)+'px');
		}
	}
						   
	var caseClienteAberto = false;					   
	$('.cliente').each(function(){
		var id = $(this).attr('id');
		if(whichBrs() == "Internet Explorer"){
			$(this).hover(function(){
				$('.cliente').each(function(){
					if($(this).attr('id') != id){
						//$(this).stop().css({opacity:.4});
						$('.fundo-bg', this).stop().css({opacity:0});
					}
				});

				$(this).css('z-index', 300);
				$('#'+id+' > .informacoes').css({display:'block'});
				$("#bg-escuro").stop().css({display:'block'});
				ajustarAltura('#'+id+' > .informacoes');				
			}, function(){
				$('.cliente').each(function(){
					if($(this).attr('id') != id){
						//$(this).stop().css({opacity:1});
						$('.fundo-bg', this).stop().css({opacity:1});
					}
				});			

				$(this).css('z-index', 1);
				$('#'+id+' > .informacoes').css({display:'none'});
				$("#bg-escuro").stop().css({ display:'none' });
			});			 
		}else{
			$(this).hover(function(){
				$('.cliente').each(function(){
					if($(this).attr('id') != id){
						$(this).stop().animate({opacity:.4}, 500);
						$('.fundo-bg', this).stop().animate({opacity:0}, 500);
					}
				});
				
								   
				$(this).css('z-index', 300);
				$('#'+id+' > .informacoes').css({display:'block', opacity:0}).animate({opacity:1}, 500);
				$("#bg-escuro").stop().css({display:'block', opacity:0}).animate({ opacity: 1 }, 500, function() {  } );			
				ajustarAltura('#'+id+' > .informacoes');
			}, function(){
				$('.cliente').each(function(){
					if($(this).attr('id') != id){
						$(this).stop().animate({opacity:1}, 500);
						$('.fundo-bg', this).stop().animate({opacity:1}, 500);
					}
				});			
		
				$(this).css('z-index', 1);
				$('#'+id+' > .informacoes').fadeOut(1, function(){ });
				if(!caseClienteAberto){
					$("#bg-escuro").stop().animate({ opacity: 0 }, 500, function() { $("#bg-escuro").css({display:'none'}); } );
				}
				
			});			
		}
		

	});
						   
						   
						   
						   
	var browserName=navigator.appName;	
	if (browserName == "Microsoft Internet Explorer"){
		var opacity = 0.5;
	}else{
		var opacity = 1;
	}
	
	function whichBrs() {
		var agt=navigator.userAgent.toLowerCase();
		if (agt.indexOf("opera") != -1) return 'Opera';
		if (agt.indexOf("staroffice") != -1) return 'Star Office';
		if (agt.indexOf("webtv") != -1) return 'WebTV';
		if (agt.indexOf("beonex") != -1) return 'Beonex';
		if (agt.indexOf("chimera") != -1) return 'Chimera';
		if (agt.indexOf("netpositive") != -1) return 'NetPositive';
		if (agt.indexOf("phoenix") != -1) return 'Phoenix';
		if (agt.indexOf("firefox") != -1) return 'Firefox';
		if (agt.indexOf("safari") != -1) return 'Safari';
		if (agt.indexOf("skipstone") != -1) return 'SkipStone';
		if (agt.indexOf("msie") != -1) return 'Internet Explorer';
		if (agt.indexOf("netscape") != -1) return 'Netscape';
		if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
		if (agt.indexOf('\/') != -1) {
		if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
		return navigator.userAgent.substr(0,agt.indexOf('\/'));}
		else return 'Netscape';} else if (agt.indexOf(' ') != -1)
		return navigator.userAgent.substr(0,agt.indexOf(' '));
		else return navigator.userAgent;
	}			
	
	for(var i=1; i<12; i++){
		
		if(i>11){
			$("#case-home-"+i).css({opacity:0});
		}
		
		$("#case-home-"+i).hover( function(){
			caseHover($(this).attr('id'), true);
		}, function(){
			caseHover($(this).attr('id'), false);
		});
		
		if(i != 2){			
			$("#case-home-"+i).click( function(){
				$('#case-1').empty().html('<div class="conteudo-load"> loader </div>').load("article-home.php?id="+$(this).attr('data')+"&origem=home");
				//window.location = 'http://localhost/dez/#!/portfolio/'+$(this).attr('data');
			
				paginaAtual = 'case-1';
				abreCase();
			});
		}
	}
	
	function abreCase(){
		if(whichBrs() == 'Opera' || whichBrs() == "Internet Explorer" || whichBrs() == 'Firefox' ){
			scrollCase = $('html').attr('scrollTop');//salva a posição do scroll na home
			if(scrollCase > 100){
				$('html').animate({scrollTop:0}, 1000, 'easeInCubic', function(){ trocaPagina('case', paginaAtual, 'd'); } );
			}else{
				trocaPagina('case', paginaAtual, 'd');
			}
		}else{
			scrollCase = $('body').attr('scrollTop');//salva a posição do scroll na home
			if(scrollCase > 100){			
				$('body').animate({scrollTop:0}, 1000, 'easeInCubic', function(){ trocaPagina('case', paginaAtual, 'd'); } );						
			}else{
				trocaPagina('case', paginaAtual, 'd');
			}
		}
		return false;		
	}

	caseVoltar = function(){
		//window.location = 'http://localhost/dez/#!/home/';
		trocaPagina('case', paginaAtual, 'e');
		paginaAtual = 'case';
		trocaBgBody(false);		
	}


	abreCaseCliente = function(id){
		if(whichBrs() == 'Opera' || whichBrs() == "Internet Explorer" || whichBrs() == 'Firefox' ){
			scrollId = 'html';
		}else{
			scrollId = 'body';
		}		
		
		scrollCase = $(scrollId).attr('scrollTop');//salva a posição do scroll na home
		if(scrollCase > 100){
			$(scrollId).animate({scrollTop:0}, 1000, 'easeInCubic', function(){ 
				caseClienteAberto = true;
				trocaPagina('clientes', 'case-cliente', 'd');
				paginaAtual = 'case-cliente';
				$('#case-cliente').empty().html('<div class="conteudo-load"> loader </div>').load("article-home.php?id="+id);																		   
			});
		}else{
			caseClienteAberto = true;
			trocaPagina('clientes', 'case-cliente', 'd');
			paginaAtual = 'case-cliente';
			$('#case-cliente').empty().html('<div class="conteudo-load"> loader </div>').load("article-home.php?id="+id);
		}
	}
	
	caseClienteVoltar = function(){
		caseClienteAberto = false;
		$("#bg-escuro").stop().animate({ opacity: 0 }, 700, function() { $("#bg-escuro").css({display:'none'}); } );
		trocaPagina('clientes', 'case-cliente', 'e');
		paginaAtual = 'clientes';
	}

	trocaPagina = function(origem, id, direcao){
		if(direcao == 'd'){
			$('#'+origem).animate({ opacity: 0, marginLeft: '-=100' }, 500, 'easeInCubic', function() { 
				$('#'+origem).css({ display:'none' });
				$('#'+origem).animate({ marginLeft: '+=100' }, 0, 'easeOutCubic');	
			});
			
			window.setTimeout(function() {
				$('#'+id).animate({ marginLeft: '+=100' }, 0, 'easeOutCubic');
				$('#'+id).css({display:'block'}).animate({ opacity: 1, marginLeft: '-=100' }, 500, 'easeOutCubic', function() {	
					setaAlturaContainerCentro();
					ajustarAltura('.case-interna-informacoes');
				});
				scrollFunction(); //le a posição do botão voltar assim que o case é colocado em display block				
			}, 500);
		}else{
			$('#'+id).animate({ opacity: 0, marginLeft: '+=100' }, 500, 'easeInCubic', function() {
				$('#'+id).css({ display:'none' });																								
				$('#'+id).animate({ marginLeft: '-=100' }, 0, 'easeOutCubic');	
			});
			
			window.setTimeout(function() {
				$('#'+origem).animate({ marginLeft: '-=100' }, 0, 'easeOutCubic');
				$('#'+origem).css({display:'block'}).animate({ opacity: 1, marginLeft: '+=100' }, 500, 'easeOutCubic', function() {	
					setaAlturaContainerCentro();
					if(whichBrs() == 'Opera' || whichBrs() == "Internet Explorer" || whichBrs() == 'Firefox' ){						
						$('html').animate({scrollTop:scrollCase}, 1000, 'easeInCubic' ); //volta o scroll para a altura que estava na home
					}else{
						$('body').animate({scrollTop:scrollCase}, 1000, 'easeInCubic' ); //volta o scroll para a altura que estava na home
					}
				});
			}, 500);
		}
	}
	
	caseHover = function(id, flag){
		s = id.split('-');
		
		for(var i=1; i<12; i++){
			if(i != s[2]){
				if(flag){
					//$("#case-home-"+i+"-figcaption").css({ backgroundColor: '#FFFFFF' });
					$("#case-home-"+i+"-bg-escuro").stop(true, false).css({display:'block', opacity: 0}).animate({ opacity: opacity }, 500, function() {  } );
				}else{
					//$("#case-home-"+i+"-figcaption").css({ backgroundColor:'transparent' });
					$("#case-home-"+i+"-bg-escuro").stop(true, false).animate({ opacity: 0 }, 500, function() {  $("#case-home-"+i+"-bg-escuro").css({display:'none'}); } );
				}
			}
		}
		
		if(flag){
			$("#"+id+"-bg-escuro").stop(true, false).animate({ opacity: 0 }, 500, function(){ $("#"+id+"-bg-escuro").css({display:'none'}); } );			
			$("#"+id+"-bg-claro").stop(true, false).animate({ opacity: 0 }, 500, function() { $("#"+id+"-bg-claro").css({display:'none'}); } );
			$("#"+id+"-btn").stop(true, false).css({opacity:0, display:'block'}).animate({ opacity: 1 }, 500, function() {  } );
			//$("#"+id+"-figcaption").css({ background:'#ffffff' });
			trocaBgBody(true);
		}else{
			$("#"+id+"-bg-claro").stop(true, false).css({opacity:0, display:'block'}).animate({ opacity: 1 }, 500, function() {  } );
			$("#"+id+"-btn").stop(true, false).animate({ opacity: 0 }, 500, function() {  $("#"+id+"-btn").css({display:'none'}); } );
			//$("#"+id+"-figcaption").css({ backgroundColor:'transparent' });
			trocaBgBody(false);			
		}

	}

	trocaBgBody  = function(flag){
		if(flag){
			$("#bg-escuro").stop(true, false).css({display:'block', opacity:0}).animate({ opacity: 1 }, 500, function() {  } );
		}else{
			if(paginaAtual == 'case'){
				$("#bg-escuro").stop(true, false).animate({ opacity: 0 }, 500, function() { $("#bg-escuro").css({display:'none'}); } );
			}
		}
	}

	function scrollFunction(){ //mostra a div caso ele esteja visivel no navegador
		var top  = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
		var left = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;				

		for(var i=1; i<12; i++){
			if(top + $(window).height() - 200 >= $("#case-home-"+i).attr('offsetTop')){
				if(mostraDivInicioA == mostraDivInicioB){
					$("#case-home-"+i).animate({ opacity: 1 }, 1200);
				}
			}			
		}

		if(paginaAtual != 'case'){
			//document.getElementById('teste').value = top +'<'+ $('#'+paginaAtual+'-btn-voltar').attr('offsetTop');
			if(top < 200){
				$('#case-1-btn-voltar').removeClass('case-interna-voltar-fixed');
				$('#case-1-btn-voltar').addClass('case-interna-voltar-absolute');				
			}else{
				$('#case-1-btn-voltar').removeClass('case-interna-voltar-absolute');
				$('#case-1-btn-voltar').addClass('case-interna-voltar-fixed');
			}
		}
	}

	function setaAlturaContainerCentro(){
		if(paginaAtual == 'case'){ //home
			/*
			$('#container-centro').css('height', document.getElementById('case').offsetHeight);
			$('#bg-escuro').css('height', document.getElementById('case').offsetHeight + 100);
			$('#bg-claro').css('height', document.getElementById('case').offsetHeight + 100);
			*/
			/*
			$('#container-centro').css('height', document.getElementById('interna-tudo').offsetHeight);
			$('#bg-escuro').css('height', document.getElementById('interna-tudo').offsetHeight + 100);
			$('#bg-claro').css('height', document.getElementById('interna-tudo').offsetHeight + 100);
			*/
			
			$('#container-centro').css('height', 2050);
			$('#bg-escuro').css('height', 2150);
			$('#bg-claro').css('height', 2150);
			
		}else if(paginaAtual == 'a-dez'){
			//$('#dez').css('height', document.getElementById(paginaAtual).offsetHeight + 99);
			$('#bg-escuro').css('height', document.getElementById(paginaAtual).offsetHeight + 200);
			$('#bg-claro').css('height', document.getElementById(paginaAtual).offsetHeight + 200);
		}else if(paginaAtual == 'contatos'){
			$('#contato').css('height', document.getElementById(paginaAtual).offsetHeight + 50);
			$('#bg-escuro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);
			$('#bg-claro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);						
		}else if(paginaAtual == 'case-cliente' || paginaAtual == 'clientes'){
			$('#cliente').css('height', document.getElementById(paginaAtual).offsetHeight + 50);
			$('#bg-escuro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);
			$('#bg-claro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);						
		}else{
			$('#container-centro').css('height', document.getElementById(paginaAtual).offsetHeight + 50);
			$('#bg-escuro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);
			$('#bg-claro').css('height', document.getElementById(paginaAtual).offsetHeight + 150);
		}		
	}
	
	var mostraDivInicioA = -1;
	var mostraDivInicioB = 0;	
	/*
	function mostraUmPorVez(){
		var top  = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
		if(top + $(window).height() - 200 > 300){
			mostraDivInicioA = 2;
			window.setTimeout(function() {
				$("#case-home-1").animate({ opacity: 1 }, 1200);
				mostraDivInicioB++;
			}, 0);
			window.setTimeout(function() {
				$("#case-home-2").animate({ opacity: 1 }, 1200);
				mostraDivInicioB++;				
			}, 500);			
		}
		
		if(top + $(window).height() - 200 > 500){
			mostraDivInicioA = 3;			
			window.setTimeout(function() {
				$("#case-home-3").animate({ opacity: 1 }, 1200);
				mostraDivInicioB++;				
			}, 1000);			
		}
		
		if(top + $(window).height() - 200 > 700){
			mostraDivInicioA = 5;
			window.setTimeout(function() {
				$("#case-home-4").animate({ opacity: 1 }, 1200);
				mostraDivInicioB++;				
			}, 1500);			
			
			window.setTimeout(function() {
				$("#case-home-5").animate({ opacity: 1 }, 1200);
				mostraDivInicioB++;				
			}, 2000);
		}		
		
		if(top + $(window).height() - 200 > 750){
			mostraDivInicioA = 6;
			window.setTimeout(function() {			
				mostraDivInicioB++
				scrollFunction();			
			}, 2500);
		}
	}
	*/
	
	resizeFunction = function(){
		scrollFunction();

		if($(this).width() < 1150){
			$(".caseImagem").each(function(){

				var largura = $(this).attr('tamanhosOriginais').split(',')[0];
				var altura  = $(this).attr('tamanhosOriginais').split(',')[1];

				if(largura > 600){
					divisor  = largura / 600;
					altura   = Math.round(altura / divisor);
					largura  = Math.round(largura / divisor);					
		
					$(this).css('width', largura+'px');
					$(this).css('height', altura+'px');
				}
			});	
		}else{
			$(".caseImagem").each(function(){
				var largura = $(this).attr('tamanhosOriginais').split(',')[0];
				var altura  = $(this).attr('tamanhosOriginais').split(',')[1];

				$(this).css('width', largura+'px');
				$(this).css('height', altura+'px');
			});	
		}
		

		
	}	
	
	
	validarFormulario = function(){
		erro = '';

		if(document.formContato.mensagem.value.length < 5 ){
			erro = 'Por favor, escreva uma mensagem';
		}		
		
		if(document.formContato.assunto.value.length < 5 ){
			erro = 'Por favor, preencha o assunto';
		}
	
		if(document.formContato.email.value.length < 5){
			erro = 'Por favor, preencha seu e-mail';
		}	

		if(document.formContato.nome.value.length < 5 ){
			erro = 'Por favor, preencha seu nome';
		}
		
		if(erro.length > 0){
			$('#erro-email').html(erro);
			$('#contato-mensagem-sucesso').fadeOut(250, function(){
				$('#contato-mensagem-erro').fadeIn(250);
			});
		}else{
			$.post("ajax_contato.php", $("#formContato").serialize(), function(data){
				if(data == 'true'){
					$('#contato-mensagem-erro').fadeOut(250, function(){
						$('#contato-mensagem-sucesso').fadeIn(250);
					});
					document.formContato.nome.value     = '';
					document.formContato.email.value    = '';
					document.formContato.assunto.value  = '';
					document.formContato.mensagem.value = '';								
				}else{
					//$('#feedback-form').html('Erro ao enviar e-mail!').fadeIn(500);
				}
			});
		}		
		
	}


	window.onscroll = function(event) {
		scrollFunction();
	}	
	
	window.onresize = function(event) {
		resizeFunction();
		setaAlturaContainerCentro();
	}

	scrollFunction();
	setaAlturaContainerCentro();
	
	
	$("#footer").css({display:'block'});
});
