pages = new Array();

$(function(){
	var iniPage = document.location.href.split("#!")[1];
	if(!iniPage)
		page("pagina-principal");
	else
		page(iniPage);
	
	$(window).resize(function(){
		document.location.reload();
	});
	
	$(".ima-in").css({"width":"auto","height":$("#tpl-images-base").height()+"px"});
	$("#img-bottom").css("margin-top",$("#tpl-images-base").height()+"px");
	$("#img-right").css("margin-left",$("#tpl-images-base").width()+"px");

});

page = function(nome){
	$(".texto-pagina").height($("#tpl-images-base").height()-120);
	$(".texto-pagina").hide();
	$(".tpl-gallery").hide();
	$(".tpl-info").hide();
	$("#tpl-menu li [href!='#!"+nome+"']").removeClass("ativo");
	$("#tpl-menu li [href='#!"+nome+"']").addClass("ativo");
	
	if(!pages[nome]){
		$("#loader").show();
		$.post("page.php",{page:nome},function(dt){
			if(dt.status = 1){
				pages[nome] = dt;
			}
			
			populaInfo(dt);
		},"json");
	} else {
		populaInfo(pages[nome]);
	}
}


direita = function(){
	$(".gallery").append('<li>'+$(".gallery"+" li:first").html()+'</li>');
	$(".gallery"+" li:first").remove();
	$(".gallery").append('<li>'+$(".gallery"+" li:first").html()+'</li>');
	$(".gallery"+" li:first").remove();
	$(".gallery").append('<li>'+$(".gallery"+" li:first").html()+'</li>');
	$(".gallery"+" li:first").remove();
}

esquerda = function(base){
	$(".gallery").prepend('<li>'+$(".gallery"+" li:last").html()+'</li>');
	$(".gallery"+" li:last").remove();
	$(".gallery").prepend('<li>'+$(".gallery"+" li:last").html()+'</li>');
	$(".gallery"+" li:last").remove();
	$(".gallery").prepend('<li>'+$(".gallery"+" li:last").html()+'</li>');
	$(".gallery"+" li:last").remove();
}

populaInfo = function(dt){
	$(".tpl-info").hide();
	$("#loader").hide();
	
	if(dt.galeria && dt.galeria.length > 0) {
		$(".ima-in").css({"width":"auto","height":($("#tpl-images-base").height()-102)+"px"});
		$(".texto-pagina").height($("#tpl-images-base").height()-220);
	} else {
		$(".ima-in").css({"width":"auto","height":$("#tpl-images-base").height()+"px"});
		$(".texto-pagina").height($("#tpl-images-base").height()-120);
	}

	if(dt.principal){
		p = dt.principal;
	} else {
		if(dt.galeria.length > 0)
			p = dt.galeria[0].original;
	}
	
	if($("#img-center").attr("src") == undefined || $("#img-center").attr("src") == ""){
		$("#img-center").attr("src",p);
		if(dt.texto){
			$(".texto-pagina").html(dt.texto).show();
		}
	} else {
		var img = new Image();
  
		$(img).load(function () {
			$("#img-bottom").attr("src",p);
			$("#loader").hide();
			rolaPagina(dt);
		}).error(function () {
		}).attr('src', p);
	}
	
	if(dt.galeria && dt.galeria.length > 0){
		html = '			<div class="esq">';
		html += '				<a onclick="esquerda()" href="javascript:;">';
		html += '					<img src="css/imagens/esq.png">';
		html += '				</a>';
		html += '			</div>';
		html += '			<div class="gallery-base">';
		html += '				<ul class="gallery">';
		
		$.each(dt.galeria,function(i,v){
			html += '					<li>';
			html += '						<a href="javascript:;" onclick="mudaFoto(\''+v.original+'\',\''+v.descricao+'\')"><img src="'+v.thumb+'">';
			
			if(v.linha != "")
				html += '<span class="title-gal">'+v.linha+'</span>';
			
			html += '</a>';
			html += '					</li>';
		});
		
		html += '				</ul>';
		html += '			</div>';
		html += '			<div class="dir">';
		html += '				<a onclick="direita()" href="javascript:;">';
		html += '					<img src="css/imagens/dir.png">';
		html += '				</a>';
		html += '			</div>';
		
		$(".tpl-gallery").html(html).show();
	}
	
}

mudaFoto = function(p,desc){
	$("#loader").show();
	if(desc == "")
		$(".tpl-info").hide();
	
	var img = new Image();
  
	$(img)
	.attr("src",p)
	.attr("id","img-right")
	.attr("class","ima-in")
	.css("margin-left",$("#tpl-images-base").width())
	.width("auto")
	.height($("#tpl-images-base").height()-102)
	.load(function () {
		$("#img-right").remove();
		$("#tpl-images-base").append(this);
		$("#loader").hide();
		rolaGaleria();
		if(desc != ""){
			$(".tpl-info").html(urldecode(desc)).fadeIn();
		}
	}).error(function () {
	})
}

rolaPagina = function(dt){
	$("#img-bottom").animate({"margin-top":"-="+$("#tpl-images-base").height()},500,function(){
		$("#img-center").attr("id","alt");
		$("#img-bottom").attr("id","img-center");
		$("#alt").attr("id","img-bottom");
		$("#img-bottom").css("margin-top",$("#tpl-images-base").height());
		if(dt.texto){
			$(".texto-pagina").html(dt.texto).fadeIn();
		}
	});
}

rolaGaleria = function(){
	
	$("#img-right").animate({"margin-left":"-="+$("#tpl-images-base").width()},500,function(){
		$("#img-center").remove();
		$("#img-right").attr("id","img-center");
		
		if($(".tpl-gallery").is(":visible"))
			$(".ima-in").css({"width":"auto","height":($("#tpl-images-base").height()-102)+"px"});
	});
}

contato = function(){
	$(".stat").html('Aguarde<br/><img src="css/imagens/ajax-loader.gif"/>');
	$.post("contato.php",{
		nome:$(".contatoNome").val(),
		empresa:$(".contatoEmpresa").val(),
		email:$(".contatoEmail").val(),
		telefone:$(".contatoTelefone").val(),
		mensagem:$(".contatoMensagem").val()
	},function(dt){
		$(".stat").html(dt.msg);
		
		if(dt.status == 1){
			$("form").each(function(){
				this.reset();
			});
		}
		
		setTimeout(function(){
			$(".stat").html('<input type="submit" value="ENVIAR"/>');
		},2000);
		
	},"json")
	.error(function(){
		$(".stat").html('Houve um erro ao enviar<br/><a href="javascript:;" onclick="contato()">Tentar novamente</a>');
	});
	return false;
}
 
function urldecode( str ) {

	var histogram = {};
	var ret = str.toString();

	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};

	// The histogram is identical to the one in urlencode.
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram['%20'] = '+';
	histogram['\u20AC'] = '%80';
	histogram['\u0081'] = '%81';
	histogram['\u201A'] = '%82';
	histogram['\u0192'] = '%83';
	histogram['\u201E'] = '%84';
	histogram['\u2026'] = '%85';
	histogram['\u2020'] = '%86';
	histogram['\u2021'] = '%87';
	histogram['\u02C6'] = '%88';
	histogram['\u2030'] = '%89';
	histogram['\u0160'] = '%8A';
	histogram['\u2039'] = '%8B';
	histogram['\u0152'] = '%8C';
	histogram['\u008D'] = '%8D';
	histogram['\u017D'] = '%8E';
	histogram['\u008F'] = '%8F';
	histogram['\u0090'] = '%90';
	histogram['\u2018'] = '%91';
	histogram['\u2019'] = '%92';
	histogram['\u201C'] = '%93';
	histogram['\u201D'] = '%94';
	histogram['\u2022'] = '%95';
	histogram['\u2013'] = '%96';
	histogram['\u2014'] = '%97';
	histogram['\u02DC'] = '%98';
	histogram['\u2122'] = '%99';
	histogram['\u0161'] = '%9A';
	histogram['\u203A'] = '%9B';
	histogram['\u0153'] = '%9C';
	histogram['\u009D'] = '%9D';
	histogram['\u017E'] = '%9E';
	histogram['\u0178'] = '%9F';

	for (replace in histogram) {
		search = histogram[replace]; // Switch order when decoding
		ret = replacer(search, replace, ret) // Custom replace. No regexing   
	}

	// End with decodeURIComponent, which most resembles PHP's encoding functions
	ret = decodeURIComponent(ret);

	return ret;
}
