$(function() {

	// LINKS EXTERNOS
	$("a[rel=external]").attr('target','_blank');
	$("a[rel=external nofollow]").attr('target','_blank');
	$("a[rel=nofollow external]").attr('target','_blank');
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");


	$('#nav li').hover(
		function(){
			$('#nav span').addClass('bug');
			$(this).find('span').removeClass('bug');
			$(this).addClass('active');
			$(this).find('span').addClass('label-active');
		},
		function(){
			$('#nav span').removeClass('bug');
			$(this).removeClass('active');
			$(this).find('span').removeClass('label-active');
		}
		
	);
	
	$('#banner').flash({ 
		src: 'swf/preloader.swf',
		width: '100%',
		height: 324,
		wmode: 'transparent'
	})
	
	$('#bee').flash({ 
		src: 'swf/abelhinha.swf',
		width: 85,
		height: 85,
		wmode: 'transparent'
	})
	
	$('.aba-01').addClass('aba-01-active');
	$('.abas li a').click(function(){
		
		var href = '#'+$(this).attr('title');
		var classe = $(this).attr('class');
		
		if($(this).hasClass('.active')){
		} else {
			$('.panels').hide();
			$('.abas li a').removeClass('active');
			$('.abas li a').removeClass('aba-01-active').removeClass('aba-02-active').removeClass('aba-03-active').removeClass('aba-04-active').removeClass('aba-05-active');
			$(this).addClass('active');
			$(this).addClass(classe+'-active');
			$(href).show();		
		}
	
	});
	
	$('.aba-03').click(function(){
		
		$.ajax( {
			type: "POST",
			url: "incs/form-agendar-contato.php",
			beforeSend: function() {
				// enquanto a função esta sendo processada...
				$('#fale-conosco form').html('<img src="img/load.gif" alt="Carregando" width="16" />');
			},
			success: function(txt) {
				// troca o conteúdo
				$('#fale-conosco form').html(txt);
			},		
			error: function(txt) {
				// em caso de erro pode dar um alert('erro');
				alert('Erro: '+txt);
			}
		});
		
	});
	
	$('.aba-01').click(function(){
		
		$.ajax( {
			type: "POST",
			url: "incs/form-contato.php",
			beforeSend: function() {
				// enquanto a função esta sendo processada...
				$('#fale-conosco form').html('<img src="img/load.gif" alt="Carregando" width="16" />');
			},
			success: function(txt) {
				// troca o conteúdo
				$('#fale-conosco form').html(txt);
			},		
			error: function(txt) {
				// em caso de erro pode dar um alert('erro');
				alert('Erro: '+txt);
			}
		});
		
	});

	$('#passo-01 .bt-submit').click(function(){
		if (($('#nome_responsavel').val() != "") && 
			($('#nome_empresa').val() != "") && 
			($('#telefone').val() != "") && 
			($('#email_orcamento').val() != "") && 
			($('#endereco_site').val() != "") && 
			($('#tipo_trabalho').val() != ""))
		{
			$('#passo-01').slideUp(100, function(){
				$('#passo-02').slideDown(300);
			});
		} else {
			alert('Todos os campos devem ser preenchidos.');
		}
		return false;
	});
	$('#passo-02 .bt-submit').click(function(){
		if (($('#referencias').val() != "") && ($('#site_temp').val() != "")){
			$('#passo-02').slideUp(100,function(){
				$('#passo-03').slideDown(300);
			});
		} else {
			alert('Todos os campos devem ser preenchidos.');
		}
		return false;
	});
	
	$('#passo-02 .bt-voltar').click(function(){
		$('#passo-02').slideUp(100, function(){
			$('#passo-01').slideDown(300);
		});
		return false;
	});
	$('#passo-03 .bt-voltar').click(function(){
		$('#passo-03').slideUp(100,function(){
			$('#passo-02').slideDown(300);
		});
		return false;
	});
	
	$('.btn-sitemap-footer').click(function(){
		
		if($(this).hasClass('.ajaxed')){
		
		}else{
			$(this).addClass('ajaxed');
			$.ajax( {
				type: "POST",
				url: "incs/sitemap.php",
				beforeSend: function() {
					// enquanto a função esta sendo processada...
					$('#sitemap-alvo').html('<img src="img/load.gif" alt="Carregando" width="16" />');
				},
				success: function(txt) {
					// troca o conteúdo
					$('#sitemap-alvo').html(txt);
				},		
				error: function(txt) {
					// em caso de erro pode dar um alert('erro');
					alert('Erro: '+txt);
				}
			});
		}
		if($(this).hasClass('.active-bt')){
			
			if($.browser.msie){
				$('#footer').animate({ height: 110 }, 300);
			}else{
				$('#footer').animate({ height: 110 }, 0);
			}
			$(this).removeClass('active-bt')
		
		}else{
		
			$(this).addClass('active-bt')
			if($.browser.msie){
				$('#footer').animate({ height: 400 }, 300);
			}else{
				$('#footer').animate({ height: 400 }, 0);
			}
		
		}
	
	});


});

/**
   * Função para validação do formulário
   * @param string form : id do formulário
   * @return bool true/false
   **/
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj 	= document.getElementById(form).elements[i].id;
		var relObj 	= document.getElementById(form).elements[i].getAttribute('rel');
				
		if ((relObj == "email-required")) {
			expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
			if(!expressao.test(document.getElementById(form).elements[i].value)){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].value = "";
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}else if (relObj == "required") {
			if((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

/**
   * Contato
   * @param bool true/false
   * @return AJAX
   **/
function validarContato(flag){
	if(flag){
		var str = $('#formContato').serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao_contato.php",
			data: str,
			beforeSend: function() {
			},
			success: function(txt) {
				//alert(txt);
				$.getSucesso(txt);
				$('#formContato').each(function(){
					this.reset();
				});
			},
			error: function(txt) {
				//alert('Erro: '+txt);
				$.getErro(txt);
			}
		});
	}
}

/**
   * Orcamento
   * @param bool true/false
   * @return AJAX
   **/
function validarOrcamento(flag){
	if(flag){
		var str = $('#formOrcamento').serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao_orcamento.php",
			data: str,
			beforeSend: function() {
			},
			success: function(txt) {
				//alert(txt);
				$.getSucesso(txt);
				$('#formOrcamento').each(function(){
					this.reset();
				});
			},
			error: function(txt) {
				//alert('Erro: '+txt);
				$.getErro(txt);
			}
		});
	}
}

/**
   * Agendar
   * @param bool true/false
   * @return AJAX
   **/
function validarAgendar(flag){
	if(flag){
		var str = $('#formContato').serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao_agendar.php",
			data: str,
			beforeSend: function() {
			},
			success: function(txt) {
				//alert(txt);
				$.getSucesso(txt);
				$('#formContato').each(function(){
					this.reset();
				});
			},
			error: function(txt) {
				//alert('Erro: '+txt);
				$.getErro(txt);
			}
		});
	}
}


/*
 * hivecode-JS ~ Now with iPhone support!
 * Code: http://hivecode-js.googlecode.com/
 * Examples: http://www.snaptortoise.com/hivecode-js
 * Copyright (c) 2009 George Mandis (georgemandis.com)
 * Version: 1.0.8 (05/08/2009)
 * Licensed under the Artistic License/GPL
 * http://dev.perl.org/licenses/
 * Tested in: Safari 4, Firefox 3, IE7 and Mobile Safari 2.2.1
 */

var hivecode = {
	input:"",
	pattern:"3838404037393739666513",
	clear:setTimeout('hivecode.clear_input()',2000),
	load: function(link) {
		window.document.onkeydown = function(e) {
			hivecode.input+= e ? e.keyCode : event.keyCode;
			if (hivecode.input == hivecode.pattern) {
				hivecode.code(link);
				clearTimeout(hivecode.clear);
				return;
				}
			clearTimeout(hivecode.clear);
			hivecode.clear = setTimeout("hivecode.clear_input()",2000);
			}
			this.iphone.load(link)
		},
	code: function(link) { window.location=link},
	clear_input: function() {
		hivecode.input="";
		clearTimeout(hivecode.clear);
		},
	iphone:{
		start_x:0,
		start_y:0,
		stop_x:0,
		stop_y:0,
		tap:false,
		capture:false,
		keys:["38","38","40","40","37","39","37","39","66","65","13"],
		code: function(link) { window.location=link},
		load: function(link){
			document.ontouchmove = function(e){
			  if(e.touches.length == 1 && hivecode.iphone.capture==true){ // Only deal with one finger
			    var touch = e.touches[0]; // Get info for finger #1
				hivecode.iphone.stop_x = touch.pageX;
				hivecode.iphone.stop_y = touch.pageY;
				hivecode.iphone.tap = false; 
				hivecode.iphone.capture=false;
				hivecode.iphone.check_direction();
			  	}
				}		
			document.ontouchend = function(evt){
				if (hivecode.iphone.tap==true) hivecode.iphone.check_direction();		
				}
			document.ontouchstart = function(evt){
				hivecode.iphone.start_x = evt.changedTouches[0].pageX
				hivecode.iphone.start_y = evt.changedTouches[0].pageY
				hivecode.iphone.tap = true
				hivecode.iphone.capture = true
				}		
				},
		check_direction: function(){
			x_magnitude = Math.abs(this.start_x-this.stop_x)
			y_magnitude = Math.abs(this.start_y-this.stop_y)
			x = ((this.start_x-this.stop_x) < 0) ? "RIGHT" : "LEFT";
			y = ((this.start_y-this.stop_y) < 0) ? "DOWN" : "UP";
			result = (x_magnitude > y_magnitude) ? x : y;
			result = (this.tap==true) ? "TAP" : result;			
			if (result==this.keys[0]) this.keys = this.keys.slice(1,this.keys.length)
			if (this.keys.length==0) this.code(this.link)
			}
		}
}