var acesso_postifolio = "0";

$(document).ready(function(){

var tamanho_total = $(document).height();
	
tamanho_total	= tamanho_total - 66;
tamanho_total	= tamanho_total / 2;
tamanho_total	= tamanho_total - 128;

var tamanho_rebate	= tamanho_total + 30;

	$('div#aguarde').slideDown('slow', function(){
		
		atualizaRate(1);
	
		$('div#dados').load("arquivos_1.php", function(){
		
			$('div#dados div img').load(function(){

				$('div#aguarde').slideUp('slow',function(){

					$('div.info_sites').animate({
    					height: 'toggle'
  					}, 500, function(){

  							$('#box_rate').fadeIn('slow');
  					
  						stop();	
  						$('div#info_pessoais').show();
						$('div#info_pessoais').animate({
    						opacity: '1'
  						}, 500, function(){
  						
							$('div#dados_pessoais').show();
							$('div#dados_pessoais').animate({
								top: tamanho_rebate+'px'
							}, 700, function(){
  						
								$('div#dados_pessoais').animate({
									top: tamanho_total+'px'
								}, 400)	
								
								atualizaTamanho(tamanho_total,tamanho_rebate);
							});
  						
  						});
  					
  					});

				});
			
			});
			
		});
		
	});

	
});

var novo_tamanho_total
var novo_tamanho_rebate

function atualizaTamanho(total,rebate){

	novo_tamanho_total	= total;
	novo_tamanho_rebate	= rebate;

}

function stop(){

	clearTimeout(contagem);
	clearTimeout(mostrador);
	$("#tempo").html("10");
	$('a.stop').hide();
	$('a.ativar').show();

}
			
function contador(segundos){
		$('a.stop').show();
		$('a.ativar').hide();
		contagem = setTimeout('portifolio(\'proximo\',\'1\')', segundos*1000);
		atualiza(segundos);
}

function atualiza(segundos){
	if(segundos>=0){
		
		if(segundos < 10){
			$("#tempo").html('0'+segundos);
		}else{
			$("#tempo").html(segundos);
		}
		
		segundos = segundos-1;
		mostrador = setTimeout('atualiza(\''+segundos+'\')', 1000);
	}
}


var contagem
var mostrador

function portifolio(id,ativo){

	if(ativo == 0){
		stop();
	}

	var id_rel = $('a#'+id).attr('rel');
	
	$('div.info_sites').animate({
		height: 'toggle'
	}, 500,function(){

		$('div#aguarde').slideDown('slow', function(){
		
			$('#box_rate').fadeOut('slow', function(){
			
				$('#box_rate div.rate').html("");
				$('<span id="1"></span><span id="2"></span><span id="3"></span><span id="4"></span><span id="5"></span><div class="rate_ok"></div>').appendTo('#box_rate div.rate');
			
				$('div#dados').load("arquivos_"+id_rel+".php", function(){
			
					atualizaRate(id_rel);
					
					$('div#dados div img').load(function(){
			
						$('div#aguarde').slideUp('slow', function(){
	
							$('div.info_sites').animate({
    							height: 'toggle'
  							}, 500, function(){
  						
  								$('#box_rate').fadeIn('slow');

  							});

							if(id_rel >= '11'){
								$('span#pagina').text(id_rel);
							}else{
								id_rel = "0"+id_rel;
								$('span#pagina').text(id_rel);
							}

			
							if(id == "proximo"){
				
								var id_rel_proximo = $('a#proximo').attr('rel');
					
								if(id_rel_proximo == "12"){
					
									$('a#proximo').attr('rel','1');
									$('a#voltar').attr('rel','11');
								
									if(ativo == 1){
										contador(10);
									}
								
								}else{
					
									var mais	= parseFloat(id_rel_proximo) + parseFloat('1');
						
									if(id_rel_proximo == "1"){
										var menos	= "12";
									}else{
										var menos	= parseFloat(id_rel_proximo) - parseFloat('1');
									}
						
									$('a#proximo').attr('rel',mais);
									$('a#voltar').attr('rel',menos);
								
									if(ativo == 1){
										contador(10);
									}
					
								}
					
							}else{
				
								var id_rel_voltar = $('a#voltar').attr('rel');
								var id_rel_proximo = $('a#proximo').attr('rel');
					
								if(id_rel_voltar <= "1"){
					
									$('a#proximo').attr('rel','2');
									$('a#voltar').attr('rel','12');
					
								}else{
					
									if(id_rel_proximo == "1"){
										var mais	= "12";
									}else{
										var mais	= parseFloat(id_rel_proximo) - parseFloat('1');
									}
						
									var menos	= parseFloat(id_rel_voltar) - parseFloat('1');
						
									$('a#proximo').attr('rel',mais);
									$('a#voltar').attr('rel',menos);
					
								}
				
							}
			
						});
			
					});
	
				});
	
			});
	
		});
	
	});

}


function atualizaRate(id){

	$.ajax({
		type: "POST",
		url: "atualiza_rate.php",
		dataType: 'json',
		data: 'id_pagina='+id+'',
		
		success: function(dados_votos){
			
			mostraRate(dados_votos['valor'],id,dados_votos['n_votos']);

		}
	});
	
}

function mostraRate(msg,id,n_votos){
            
    var id_atual		= id;
	var i_atual			= 1;
	var marcacao_atual	= msg;
			
	$('.rate span').removeClass('atual');

	for(i_atual=1; i_atual<=marcacao_atual; i_atual++){
		$('.rate span#'+ i_atual).addClass('atual');
	}
	
	$('.txt_rate span').html(n_votos);
            
	$('.rate span').mouseover(function(){
                
		$(this).css('cursor','pointer');
                
		var valor_estrela = $(this).attr('id');
                
		var i = 1;
		$('.rate span').removeClass('marcacao');
                
		for(i=1; i<=valor_estrela; i++){
			$('.rate span#'+ i).addClass('marcacao');
		}
                
	});
            
	$('.rate span').click(function(){
		var valor_materia = $(this).attr('id');

		$.ajax({
			type: "POST",
			url: "contabiliza_rate.php",
			data: 'id_pagina='+id+'&valor='+valor_materia+'',
		
			success: function(msg){
			
				$('#box_rate div.rate').html("");
				$('<span id="1"></span><span id="2"></span><span id="3"></span><span id="4"></span><span id="5"></span><div class="rate_ok"></div>').appendTo('#box_rate div.rate');

				var novo_valor = valor_materia;
				
				for(i_atual=1; i_atual<=novo_valor; i_atual++){
					$('.rate span#'+ i_atual).addClass('atual');
				}
				
				$('.rate div').html('Ok!');
				$('.txt_rate span').html(msg);

			}
		});
		
	})
            
	$('.rate span').mouseout(function(){
		$('.rate span').removeClass('marcacao');
	});
            
            
}

function acessarPort(tipo_acesso){

	if(tipo_acesso == "acessar_port"){
							
		$('div#dados_pessoais').animate({
			top: novo_tamanho_rebate+'px'
		}, 400, function(){
  						
			$('div#dados_pessoais').animate({
				top: '-265px'
			}, 700, function(){
								
				$('div#dados_pessoais').hide();
				$('div#info_pessoais').animate({
					opacity: '0'
				}, 500,function(){

					$('div#info_pessoais').hide();
					
					if(acesso_postifolio == "0"){
						acesso_postifolio = "1";
						$('a.acessar_dp').fadeIn('slow', function(){
							$('div#navegador').effect("highlight", {color:'#437dc3'}, 1500);
						});
					}
  										
				});
								
			});	
  							
		});
		
	}else{
		
		stop();
		$('div#info_pessoais').show();
		$('div#info_pessoais').animate({
			opacity: '1'
		}, 500, function(){
  						
			$('div#dados_pessoais').show();
			$('div#dados_pessoais').animate({
				top: novo_tamanho_rebate+'px'
			}, 700, function(){
  						
				$('div#dados_pessoais').animate({
					top: novo_tamanho_total+'px'
				}, 400)	
  							
			});
  						
		});

	}

}
