
/* CONFIGURAÇÕES
-----------------------------------------------------------------------*/
var imgs_destaque = '.banner img',
	HOME = 'home',
	PAGINA = HOME;
	TITULO = document.title,
	IMGS_QTD = 0,
	IMGS_ARRAY = [],
	IMGS_LOAD = '.home .imagem .load',
	IMGS_NAVEGACAO = '.home #mascara ul.miniaturas',
	IMGS_SETAS = '.home .imagem span',
	IMGS_REF = '.home .imagem .ref';
	IMGS_ATUAL = 1;
	

/* FUNÇÕES NECESSÁRIAS PARA AVISOS
-----------------------------------------------------------------------*/

function alinha_centro(div){
	var posX = Math.round($(div).width() / 2);
	var posY = Math.round($(div).height() / 2);
	
	$(div).css({
		"position" : "fixed",
		"top": '50%',
		"left": '50%',
		"margin-left": - posX,
		"margin-top": - posY,
		"z-index": 1000
	});
}

// gera aviso (como alert);
function aviso(t,m){
	var div = '<div id="div_aviso"></div>';
	$('#div_aviso').remove();
	$('body').append(div);
	$('#div_aviso').html('<h1>' + t + '</h1><p>' + m + '</p><a href="javascript:void(0)" class="f">Fechar</a>');
	
	alinha_centro('#div_aviso');
	
	function fechar(){
		$('#div_aviso').fadeOut('slow',function(){
			$('#div_aviso').remove();
		});
	}
	
	$('#div_aviso').hide();
	$('#div_aviso').fadeIn(500);
	$('#div_aviso a.f').click(function(event){
		event.preventDefault();
		fechar();
	});
}

function debug(msg){
	aviso('Debug',msg);
}

function mostrarCarregando(texto){
	//carregando.show();
	if(texto == undefined){
		texto = 'Carregando...';
	}
	$('#carregando').html(texto);
	$('#carregando').fadeIn(200);
};
	
function ocultarCarregando(){
	$('#carregando').fadeOut(600)
};

function redimensionar(){
	var $hDocumento = $(window).height();
	var $conteudos = $('div.conteudo');
	var $h = $hDocumento - 95;
	$conteudos.height($h);
}

function auto_redimensionar(){
	redimensionar();
	rolar();
}

/* ROLAR O SITE PARA A PAGINA ATUAL
-----------------------------------------------------------------------*/

function rolar(){
	$p = '#cont_' + PAGINA;
	$('html, body').stop().animate({scrollTop: $($p).offset().top}, 1200,'easeInOutExpo');
}

/* AO TROCAR A URL ATUALIZA O SITE
-----------------------------------------------------------------------*/

function atualizar(event){
	// arquivo para ser carregado
	var a = event.path.replace('/','');
	if(a == ''){
		a = HOME;
	}
	var linkA = $('ul.menu li a[href=#/' + a + ']');
	var menu = $('ul.menu');
	PAGINA = a;
	
	rolar();	
	
	SWFAddress.setTitle(TITULO + ' | ' + linkA.attr('title'));
		
}


/* CARREGA IMAGEM CONCEITO
-----------------------------------------------------------------------*/

function carregaImg(n){
	var lista = $(IMGS_NAVEGACAO);
	// numero de fotos por vez
	var nVez = 11;
	// altura deca da imagem
	var tamanhoImg = 75;
	var margem = (nVez - 1) / 2;
	var nFotos = IMGS_QTD;
	
	if(n < 0){
		n = nFotos - 1;
	}else if(n >= nFotos){
		n = 0;
	}
	
	if(n >= margem + 1){
		var pos = ((n * tamanhoImg) - (margem * tamanhoImg)) * -1;
		var Xmax = (nVez * tamanhoImg) - (nFotos * tamanhoImg);
		if(pos < Xmax){
			pos = Xmax;
		}
	}else{
		var pos = 0;
	}
	
	// coloca as miniaturas nas posições corretas
	lista.animate({marginLeft:pos},400);
	
	var l_atual = IMGS_ARRAY[IMGS_ATUAL];
	var l_novo = IMGS_ARRAY[n];
	
	// Diferenciando a imagem atual das outras
	l_atual.removeClass('atual');
	l_novo.addClass('atual');
	
	// carrega a imagem
	
	mostrarCarregando();
	$img = new Image();
	$img.src = l_novo.attr('href');
	$img.alt = l_novo.attr('title');
	$img.onload = function(){
		$(IMGS_LOAD + ' img').fadeOut(400,function(){
			$(IMGS_LOAD).html($img);
			$(IMGS_LOAD + ' img').hide().fadeIn(500);
			$(IMGS_REF).html(l_novo.attr('title')).hide().fadeIn(400);
		})
		
		ocultarCarregando();
	}
	
	// atualiza a imagem atual
	IMGS_ATUAL = n;
}

/* EXECUTA AS FUNÇÕES AO CARREGAR O SITE (CÓDIGO FONTE)
-----------------------------------------------------------------------*/

$(document).ready(function(){
	ocultarCarregando();
	
	//VALIDA FORMULARIO
	var formTarget = "#avisoForm";	
	var optionsForm = {
		target: formTarget,
		success: showResponse,
		resetForm: false
	}; 	
	
	function ocultar(){
		$(formTarget).slideUp(500);
	}
	
	function showResponse(responseText, statusText, xhr, $form)  { 
		//$(formTarget).hide().slideDown(500).html(responseText);
		aviso('Aviso',responseText);
		setTimeout(ocultar,6000);
	}
	
	$('form.ajax_form').ajaxForm(optionsForm);
	
	//APAGA VALUE
	$("form input[type='text'], form textarea").focus( 
		function() {
			if ($(this).val()==$(this).attr("defaultValue")) {
	 			$(this).val('');
		}
	});
	
	$("form input[type='text'], form textarea").blur(
		function() {
			if ($(this).val()=='') {
	 			$(this).val($(this).attr("defaultValue"));
		}
	});
	
	$('.menu li a').click(function(){
		$l = $(this).attr('href').replace('#/','');
		SWFAddress.setValue($l);
		return false;
	})
	
	// adiciona as imagens conceito para o script
	$(IMGS_NAVEGACAO + ' a').each(function(index,domEle){
		$l = $(domEle);
		IMGS_ARRAY[index] = $l;
		$l.click(function(){
			carregaImg(index)
			return false;
		});		
	});	
	
	$(IMGS_SETAS).click(function(){
		if($(this).attr('class') == 'mais'){
			carregaImg(IMGS_ATUAL + 1);
		}else{
			carregaImg(IMGS_ATUAL - 1);
		}
	});
	
	// height grande, pequena e local do banner;
	var $hg = 592, $hp = 345, $lb = '.home .bg, .home .banner, ' + IMGS_LOAD;
	// quando colocar e tirar o mouse de cima da home
	$(IMGS_NAVEGACAO + ', .home .banner').hover(
		function(){
			$($lb).stop().animate({'height':$hg + 'px'},500);
			$hh = $hg + 70;
			$('.home').stop().animate({'height': $hh + 'px'},500);
		},
		function(){
			$($lb).stop().animate({'height':$hp + 'px'},400);
			redimencionar();
		}
	)
	
	IMGS_QTD = IMGS_ARRAY.length;
	carregaImg(0);
	
	// atualiza o tamanho dos conteudos
	redimensionar();
	$(window).resize(auto_redimensionar);
	
	// adiciona evento quando trocar a url
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, atualizar);
			
});

