var GET = {};

function executa(elemento, css, animado, tempo) {
	if (typeof(animado) != 'boolean')
		animado = true;
	if (typeof(tempo) != 'number')
		tempo   = 600;
	
	if (animado)
		elemento.stop().animate(css, tempo);
	else
		elemento.css(css);
}

function movimenta_menu(menu, animado) {
	executa($('#menu_ativo'), {marginLeft: $(menu).position().left + 'px',	width: $(menu).css('width')}, animado);
}

function finaliza(uri, tempo) {
	if (typeof(tempo) != 'number')
		tempo = 1200;
	var opcoes = {};
	GET = carrega_get(uri);
	if (GET.pagina)
		opcoes.pagina = GET.pagina;
	else
		opcoes.pagina = 'home';
	
	if (GET.sub)
		opcoes.sub = GET.sub;
	
	$.get('includes/conteudo.php', opcoes, function (resposta) {
		$('#boxPrincipal').html(resposta);
		mLBlock('boxPrincipal');
	});
}

function desce_do_ceu(posicao, animado) {
	executa($('#ceu .object-layer1-ceu'), {
		top: '50px'
	}, animado);

	executa($('#ceu .object-layer2'), {
		top: '-800px'
	}, animado);
	
	executa($('#ceu .object-layer3, #ceu .object-layer4, #ceu .object-layer5, #ceu .object-layer6, #ceu .object-layer7'), {
		top: '-500px'
	}, animado);
	
	executa($('#terra'), {
		bottom: '0'
	}, animado);
	
	executa($('#fundo'), {
		top: '-500px'
	}, animado);
	
	executa($('#ceu'), {
		left: posicao
	}, animado, 1200);
}

function vai_para_cenario1(animado) {
	if (typeof(animado) != 'boolean')
		animado = true;
	
	desce_do_ceu(0, animado);
	
	executa($('#terra .object-layer5, #terra .object-layer4, #terra .object-layer3, #terra .object-layer2, #terra .object-layer1'), {
		marginLeft: '0'
	}, animado, 1200);
	
	movimenta_menu('#btnCenario1', animado);
	
	if ($(this).attr('href'))
		finaliza($(this).attr('href'));
	
	return false;
}

function vai_para_cenario2(animado) {
	if (typeof(animado) != 'boolean')
		animado = true;

	desce_do_ceu('-300px', animado);
	
	executa($('#terra .object-layer1 .parte3'), {
		marginLeft: '3600px'
	}, animado);
	
	executa($('#terra .object-layer5, #terra .object-layer4, #terra .object-layer3, #terra .object-layer2, #terra .object-layer1'), {
		marginLeft: '-2200px'
	}, animado, 1200);
	
	movimenta_menu('#btnCenario2', animado);
	
	if ($(this).attr('href'))
		finaliza($(this).attr('href'));
	
	return false;
}

function vai_para_cenario3(animado) {
	if (typeof(animado) != 'boolean')
		animado = true;

	desce_do_ceu('-600px', animado);
	
	executa($('#terra .object-layer5, #terra .object-layer4'), {
		marginLeft: '-4100px'
	}, animado, 1000);
	
	executa($('#terra .object-layer3, #terra .object-layer2, #terra .object-layer1'), {
		marginLeft: '-3500px'
	}, animado, 1200);
	
	movimenta_menu('#btnCenario3', animado);
	
	if ($(this).attr('href'))
		finaliza($(this).attr('href'));
	
	return false;
}

function vai_para_ceu(animado) {
	if (typeof(animado) != 'boolean')
		animado = true;
		
	executa($('#ceu .object-layer2'), {
		top: '150px'
	}, animado);
	
	executa($('#ceu .object-layer3'), {
		top: '50px'
	}, animado);
	
	executa($('#ceu .object-layer4'), {
		top: '500px'
	}, animado);
	
	executa($('#ceu .object-layer5'), {
		top: '300px'
	}, animado);
	
	executa($('#ceu .object-layer6'), {
		top: '400px'
	}, animado);
	
	executa($('#ceu .object-layer7'), {
		top: '600px'
	}, animado);
	
	executa($('#terra'), {
		bottom: '-650px'
	}, animado);
	
	executa($('#fundo'), {
		top: '0'
	}, animado);
	
	executa($('#ceu .object-layer1-ceu'), {
		top: '450px'
	}, animado);
	
	executa($('#ceu, #terra'), {
		left: '0'
	}, animado, 1200);
	
	executa($('#terra'), {
		bottom: '-700px'
	}, animado, 1200);
	
	executa($('#terra .object-layer1, #terra .object-layer2, #terra .object-layer3, #terra .object-layer4, #terra .object-layer5'), {
		marginLeft: '0'
	}, animado, 1200);

	executa($('#ceu-objetos'), {
		top: '70px'
	}, animado);
	
	movimenta_menu('#btnCeu', animado);
	
	if ($(this).attr('href'))
		finaliza($(this).attr('href'));
	
	return false;
}

function carrega_get(uri) {
	if (typeof(uri) != 'string')
		uri = location.search;
		
	var vars_tmp = uri.replace('?', '').split('&');
	var vars_get = {};
	
	for (var i = 0; i < vars_tmp.length; i++) {
		var tmp = vars_tmp[i].split('=');
		vars_get[tmp[0]] = tmp[1];
	}
	
	return vars_get;
}

function mLBlockNone(id) { // OCULTA E MOSTRA ITENS (ID_DO_ITEM)
	if (document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.display='block';
	} else {
		document.getElementById(id).style.display='none';
	}
}

function mLBlock(id){
	document.getElementById(id).style.display='block';
}

function mLNome(id){

	document.getElementById(id).style.display='none';
}

function abrirURL() {
	var nome = $(this).attr("name");
	var url
	
	switch (nome) {
		case 'fotos':
			url = '?pagina=editora&sub=galeria';
			break;
		case 'noticias':
			url = '?pagina=editora&sub=noticia';
			break;
		case 'dicas':
			url = '?pagina=editora&sub=dica';
			break;
		case 'videos':
			url = '?pagina=editora&sub=videos&p=video';
			break;    
		case 'personagens':
			url = '?pagina=livros&sub=personagens';
			break;
		case 'livros':
			url = '?pagina=livros&sub=livro';
			break;
	}
	$("#banner").hide();
	location.href = url+"&id="+$(this).val();
}

function enviarEmail() {

	$.post('paginas/contato/enviar.php', {
		nome:     $('#nome').val(),
		email:    $('#email').val(),
		mensagem: $('#mensagem').val()
	}, function (resposta) {
		alert(resposta);
	});
}

function arrumaAltura(){
	alturaTotal = document.body.clientHeight;
	if(document.getElementById('boxDoConteudo') != null)
		document.getElementById('boxDoConteudo').style.height = (alturaTotal-260)+"px";
}


$(document).ready(function(){
	
	$.preloadCssImages();
	$('.elemento').jparallax({
		frameDuration: 25,
		yparallax: true,
		ytravel: '100px'
	});
	
	arrumaAltura();
	
	GET = carrega_get();
	
	switch (GET.pagina) {
		case 'editora' : vai_para_cenario2(false); break;
		case 'contato' : vai_para_cenario3(false); break;
		case 'livros'  : vai_para_ceu(false);      break;
		case 'home': 
		default:
		vai_para_cenario1(false);
		GET.pagina = 'home';
	}
	
	$('#btnCenario1').click( vai_para_cenario1 );
	$('#btnCenario2').click( vai_para_cenario2 );
	$('#btnCenario3').click( vai_para_cenario3 );
	$('#btnCeu').click(      vai_para_ceu      );
		
	$(".sl_url").change(abrirURL);
	
	$(window).resize(function () {
		var local = location.href;
		location.href = local;
	});
});
