/* CONFIGURAÇÕES
-----------------------------------------------------------------------*/

var PAGINA = 'home',
	ATUAL = PAGINA,
	//CARREGADOS = [],
	// Introdução
	_intro_nome = '#introducao h2.nome-colecao span',
	_intro_colecao = '#introducao h1.colecao';


/* 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)
	$('#carregando').hide();
};



/* GERA OS TÍTULOS DE ACORDO COM A PÁGINA ATUAL
-----------------------------------------------------------------------*/

function gera_titulo(){
	var titulos = [];
	titulos['home'] = 'Coleção Verão 2012';
	titulos['empresa'] = 'Conheça nossa História';
	titulos['catalogo'] = 'Coleção Verão 2012';
	titulos['wallpaper'] = 'Personalize seu Computador';
	titulos['representantes'] = 'Confira nossos representantes';
	titulos['contato'] = 'Fale Conosco';
	
	if(titulos[PAGINA] != undefined)
		return 'Alvo da Moda | ' + titulos[PAGINA];
	else
		return 'Alvo da Moda | Coleção Verão 2012';
}



/* REDIMENSIONAR IMAGENS DO FUNDO
-----------------------------------------------------------------------*/

function redimensionar(){
	var wDocumento = $(window).width()+20;
	var hDocumento = $(window).height();
	var img = $('div.fundo img');
	var fundo = $('div.fundo');
	
	var wImg = img.attr('width');
	var hImg = img.attr('height');
	
	//fundo.height($(window).height());
	
	var _rw = parseInt(wDocumento);
	var _rh = parseInt((wDocumento * hImg) / wImg);
	
	if(_rh < hDocumento){
		_rw = parseInt((hDocumento * wImg) / hImg);
		_rh = parseInt(hDocumento);
	}
	
	img.width(_rw);
	img.height(_rh);
	
	//debug('wDocumento:' + wDocumento + '<br>hDocumento:' + hDocumento + '<br>wImg:' + wImg + '<br>hImg:' + hImg + ' - ' + img.height() + ' - ' + _rh)
	
	// alinha a imagem ao meio
	var left = (wDocumento / 2) - (_rw / 2);
	left = left > 0 ? (0) : (parseInt(left)); 
	img.css({marginLeft:left});
	
}


/* ADICIONA AS FUNÇÕES NECESSÁRIAS PARA A PÁGINA ATUAL
-----------------------------------------------------------------------*/

//function add_functions(n_local){
//	
//	if(n_local == undefined){
//		var local = '#conteudo #' + PAGINA + ' ';
//	}else{
//		var local = n_local + ' ';
//	}
//	
//	// colorbox
//	$(local + "a.colorbox").colorbox({maxWidth:'95%',maxHeight:'95%'});
//	
//	// barra de rolagem
//	$(local + '.scroll-pane').jScrollPane();
//	
//	/*
//	// efeito alpha ao carregar as imagens
//	if(!$.browser.msie){
//		$(local + ' img').css('opacity',0);
//		$(local + ' img').onImagesLoaded(function(_this){
//			$(_this).fadeTo(1000,1,function(){ $(this).removeAttr('style') });
//		});
//	}
//	*/
//}

// carrega / exibi o arquivo
function mostrar_conteudo(){
	
	//debug('ATUAL: ' + ATUAL + '<br>PAGINA: ' + PAGINA)
	var _l = $(window).width() + 'px';
	
	if(ATUAL != PAGINA){
		
		$('#conteudo #' + ATUAL).animate({
				marginLeft: '-' + _l,
				opacity: .4
			},
			800,
			function(){
				$(this).removeAttr('style').hide();
			}
		);
		
	}
	
	// efeitos para abrir o conteudo
	var l_ef = '#conteudo #' + PAGINA;
	// mostra o conteudo
	$(l_ef).show();
	// oculta os itens do efeito x
	$(l_ef + ' .ef-x').css({opacity:0,marginLeft:'-300px'})
	
	// faz fade no fundo
	$(l_ef + ' .fundo').hide().fadeIn(800,function(){
		
		// faz efeito x
		$(l_ef + ' .ef-x').each(function(index,domEle){
			
			// tempo do efeito
			var _t = (200 * index) + 500;
			
			$(domEle).animate({
					opacity : 1,
					marginLeft : 0	
				},
				_t,
				function(){
					$(this).removeAttr('style');
					// adiciona as funções necessárias
					//add_functions();
				}
			);
			
		});		
	});
	
	SWFAddress.setTitle(gera_titulo());
	ocultarCarregando();
	
}



/* EFEITOS INTRODUÇÃO
-----------------------------------------------------------------------*/

// abre
function _intro_abre(){
	// oculta allure
	$(_intro_nome)
	.css({
		opacity : 0,
		marginLeft : '500px'
	})
	
	// efeito colecao
	$(_intro_colecao).hide().fadeIn(1000,function(){
		
		// efeito allure
		$(_intro_nome).each(function(index,domEle){
			
			var _intro_t = (100 * index) + 800;
			
			$(domEle).animate({
					opacity : 1,
					marginLeft : 0	
				},
				_intro_t,
				function(){
					$(this).removeAttr('style');				
				}
			);
			
		});
	})
}

// fecha
function _intro_fecha(){
	
	// oculta allure
	$(_intro_colecao).fadeOut(700,function(){
		// efeito allure
		$(_intro_nome).each(function(index,domEle){
			var _intro_t = (100 * index) + 800;
			$(domEle).fadeOut(_intro_t);
		});
	})
	
}


/* NAVEGAÇÃO DO SITE ATUALIZA OS CONTEÚDOS DE ACORDO COM A URL
-----------------------------------------------------------------------*/

function atualizar(event){
	
	// arquivo para ser carregado
	ATUAL = PAGINA;
	PAGINA = event.path.replace('/','');
	
	if(PAGINA == ''){PAGINA = 'home';}
	
	$('.menu a.selecionado').removeClass('selecionado');
	$('.menu a[href=#/' + PAGINA + ']').addClass('selecionado');
	
	SWFAddress.setTitle('Carregando ' + PAGINA + '...');
	mostrarCarregando('Carregando ' + PAGINA + '...');
	mostrar_conteudo();
}



/* ABRIR O SITE (EFEITOS)
-----------------------------------------------------------------------*/

function abre_site(){
	$('#introducao').hide();
	$('.topo').fadeIn(700,function(){
		// abre o conteudo
		$('#conteudo').show();
		$('#conteudo .div-cont').hide();
		
		mostrar_conteudo();	
		
	})	
}

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

// atualiza ao trocar a url
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, atualizar);

$(document).ready(function(){
	
	if(!$.browser.msie){
		redimensionar();
		$(window).resize(redimensionar);
	}
	
	// adiciona as funções ao menu
	$('.menu a').click(function(event){
		event.preventDefault();
		SWFAddress.setValue($(this).attr('href').replace('#/',''));
	})
	
	// efeito allure
	_intro_abre();
	setTimeout(_intro_fecha,3000);
	setTimeout(abre_site,4500);
	
	// oculta o carregando
	ocultarCarregando();
	
	// adiciona as funções necessárias
	//add_functions();
	
	// mp3 player
	var flashvars = {};
	var params = {wmode:"transparent",salign:'lt'};
	var attributes = {id: "mp3Player"};
	swfobject.embedSWF("swf/player.swf", "mp3Player", "15px", "15px", "10", "", flashvars, params, attributes);
	
	
});

