var url = '';

$(document).ready(function(){
	
	// LINKS EXTERNOS
	$("a[rel=externo]").attr('target','_blank');
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");
	
	// AREAS DE ATIVACAO DO LIGHTBOX
	$('a[rel*=lightbox]').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 	url+'img/loading.gif',
		imageBtnClose: 	url+'img/close.gif',
		imageBtnPrev: 	url+'img/prev.gif',
		imageBtnNext: 	url+'img/next.gif',
		containerResizeSpeed: 300,
		txtImage: 'Imagem',
		txtOf: 'de'
	});

	// MASCARAS 
	$('#contato #fone').mask('(99) 9999-9999');
	// BT BACK 
	$(".bt-back").click(function(){
		history.back();
	});
	// BT TOPO
	$(".bt-topo").click(function(){
		$(document).scrollTo( {top:'0px',left:'0px'}, 200 );
	});
	
	//NAV SEC PRODUTOS
	$('#nav-sec').hover(
		function(){
			$('.nav-sec-produtos').fadeIn(50);
		},function(){
			$('.nav-sec-produtos').fadeOut(50);
		}
	);
	$(".holder").cycle({ 
	    fx: "scrollHorz", 
	    prev: ".bt-left", 
	    next: ".bt-right", 
	    timeout: 0 
	});

	$('.holder a').click(function(){
		var href = $(this).attr('href');
		$('.product-image img').attr('src',href);
		return false;
	});
	
	//GALERIA DE IMAGENS AMBIENTES
	$('.gallery .holder a').click(function(){
		
		$('#imagem-grande span').fadeOut(400);
		$('#imagem-grande img').fadeOut(400);
		
		var imagem 	= $(this).attr('href');
		var legenda = $(this).attr('title');
		
		$('#imagem-grande .legenda').html(legenda);
		$('#imagem-grande img').attr('src', imagem);
		
		$('#imagem-grande span').fadeIn(400);
		$('#imagem-grande img').fadeIn(400);
		
		return false;
	
	});
	
	// FLASH's
	$('#header').flash({
	    src: url+'swf/home.swf',
	    width: '100%',
	    height: 545,
		wmode: 'transparent',
		expressInstall: true
	});
	$('#container-nav').flash({
	    src: url+'swf/menu.swf',
	    width: '100%',
	    height: 164,
		wmode: 'transparent',
		expressInstall: true
	});
	$('#mapa').flash({
	    src: url+'swf/mapa.swf',
	    width: 760,
	    height: 320,
		wmode: 'transparent',
		expressInstall: true
	});
	$('#mapa-reduzido').flash({
	    src: url+'swf/mapa.swf',
	    width: 330,
	    height: 320,
		wmode: 'transparent',
		expressInstall: true
	});

	
});

/**
  * Função Validar Formulário
  * @param 
  * @return Boolean
  **/
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")) {
			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 = "#117da5";
				document.getElementById(form).elements[i].style.color = "#FFFFFF";
				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 = "#117da5";
				document.getElementById(form).elements[i].style.color = "#FFFFFF";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

/**
  * Função Validar Contato
  * @param 
  * @return jQuery
  **/
function validarContato() {
	if(validaForm("form-contato")) {	
		$.ajax( {
			type: "POST",
			url: "lib/acao_contato.php",
			data: "con_nome=" + $("#nome").val() + "&con_email=" + $("#email").val() + "&con_fone=" + $("#fone").val() + "&con_msg=" + $("#msg").val(),
			beforeSend: function() {				
			},
			success: function(txt) {
				alert("Mensagem enviada com sucesso, em breve entraremos em contato! Obrigado!");
				document.getElementById("form-contato").reset();
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	}
}

/**
  * Função Validar News
  * @param 
  * @return jQuery
  **/
function validarNews() {
	if(validaForm("form-news")) {	
		$.ajax( {
			type: "POST",
			url: "lib/acao_news.php",
			data: "email=" + $("#news_email").val(),
			beforeSend: function() {				
			},
			success: function(txt) {
				alert(txt);
				document.getElementById("form-news").reset();
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	}
}
