
/* Cookie */
jQuery.cookie=function(B,I,L){if(typeof I!="undefined"){L=L||{};if(I===null){I="";L.expires=-1}var E="";if(L.expires&&(typeof L.expires=="number"||L.expires.toUTCString)){var F;if(typeof L.expires=="number"){F=new Date();F.setTime(F.getTime()+(L.expires*24*60*60*1000))}else{F=L.expires}E="; expires="+F.toUTCString()}var K=L.path?"; path="+(L.path):"";var G=L.domain?"; domain="+(L.domain):"";var A=L.secure?"; secure":"";document.cookie=[B,"=",encodeURIComponent(I),E,K,G,A].join("")}else{var D=null;if(document.cookie&&document.cookie!=""){var J=document.cookie.split(";");for(var H=0;H<J.length;H++){var C=jQuery.trim(J[H]);if(C.substring(0,B.length+1)==(B+"=")){D=decodeURIComponent(C.substring(B.length+1));break}}}return D}};

var votedID=0;

jQuery(function($){
			
	//Mascara
	$("#tiDDD").mask("99");
	$("#tiTelefone").mask("9999-9999");

	$.easing.backout = function(x, t, b, c, d){
		var s=0;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	};
	
	$(".menu_item").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
	$(".rodape_item").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
	$(".rodape_ultimo").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
	$(".imagem_fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(400);});
	
	
	//Fotos
	$('.exibeLightbox').lightBox();
	
	//Enquete
	$("#enquete").submit(formProcess); // setup the submit handler
  
	$("#resultado_ver").click(function() {
		var en = $("#enquete_id").val();
		
		 /*$.getJSON("json/enquete.php?voto=none&enquete="+en, function(json) {
	      alert('');
	      });*/
		
		$("#poll-container").fadeOut("slow",function(){
			$(this).empty();
			$.getJSON("json/enquete.php?voto=none&enquete="+en,loadResults);
		});
	});
	
	if ($("#poll-results").length > 0 ) {
		animateResults();
	}	
	
	if ($.cookie('enquete_id') == $("#enquete_id").val()) {	
		$("#poll-container").empty();
		//$.cookie("enquete_id", null);
		var enquete_id = $.cookie('enquete_id');
		$.getJSON("json/enquete.php?voto=none&enquete="+enquete_id,loadResults);
	}
	

});

function fontSize(action, container){
	
    baseSize = parseInt($(container).css("font-size"))

    $els = $(container)
 
    $els.each(function(){
        $fs = parseInt($(this).css("font-size"))
        if(action=="plus" && baseSize<16 ) $fs+=1
        else if(action=="minus" && baseSize>13) $fs-=1
        $(this).css("font-size", $fs)
    });
}

$(function(){
    $("#liDiminuiFonte").click(function(){ fontSize("minus","#conteudo_corpo .texto") }).blur()
    $("#liAumentaFonte").click(function(){ fontSize("plus","#conteudo_corpo .texto") }).blur()
});

function validaNewsletter()
{ 
	var erro = '';
	var email = document.getElementById('email'); 
	if(!email.value) { erro = erro + '- E-mail\n'; }
	
	if(erro) {
		alert('Preencha o campo E-mail!');
		return false;
	}
	
	//E-mail
	if(!validaemail(email.value)) {
		alert('E-mail inválido.');
		return false;
	}
	
	return true;
}

function validaemail(email)
{
	p=email.indexOf("@");
	z=email.indexOf(".");
	if ((p<1 || p==(email.length-1)) || (z<1 || z==(email.length-1))) 
		return false;
	return true;
}

function abrirUrl(URL){
	window.open('http://'+URL)
}

function textCounter(campo, countcampo, maxlimit) {
	if (campo.value.length > maxlimit) 
		campo.value = campo.value.substring(0, maxlimit); 
	else
		countcampo.value = maxlimit - campo.value.length; 
}

function external(path) { // pass in the correct path to the function so we only need one <A> for infinite amount of calls from  flash                          
// if the lightbox does not exist we will make it                       
	if ($('a#lightbox').length == 0) {                             
		$("body").append("</A><A id='lightbox' style='visibility: hidden; position: absolute; left: -9999px;' href='http://www.thetruetribe.com/+path+'>calling js lightbox from flash</A>");                            
		$('a#lightbox').lightBox();                   
		// if it already exists but the path is different we will set the new path                      
	} else if ($('a#lightbox').attr("href") != path) {                             
		$('a#lightbox').attr("href", path);                   
	}      
	// now we will simulate the click here.                         
	$('a#lightbox').trigger("click");      
}


function fechar_popup(){
    $("#popup").hide();
};



function formProcess(event){
	event.preventDefault();
	var boolValid = false;
	$('#enquete input:radio').each(function() {
		if($(this).is(':checked')) {
			boolValid = true;
		}
	});
	if(boolValid){
		var id      = $("input[@name='options']:checked").attr("value");
		var enquete = $("#enquete_id").val();
		
		$("#poll-container").fadeOut("slow",function(){
			$(this).empty();			
			votedID = id;
			$.getJSON("json/enquete.php?voto="+id+"&enquete="+enquete,loadResults);
			$.cookie('enquete_id', enquete, {expires: 3600});
		});
	} else {
		alert('Selecione uma opção');
	}	 
}

function animateResults(){
  $("#poll-results div").each(function(){
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({width: percentage}, 'slow');
  });
}

function loadResults(data) {

	var total_votes = 0;
	var percent;
	
	
	$.each(data, function(i,opcoes){
		total_votes = total_votes+parseInt(opcoes.OPT_VOTES);
	});	
	
	var results_html = "<div id='poll-results'><h2>Resultado</h2>\n<dl class='graph'>";
	$.each(data, function(i,opcoes){
		percent = Math.round((parseInt(opcoes.OPT_VOTES)/parseInt(total_votes))*100);
		results_html = results_html+"<dt class='bar-title'>"+opcoes.OPT_TITLE+"</dt><dd class='bar-container'><div id='bar"+opcoes.id+"'style='width:0%; background: url(img/enquete_bar.jpg) repeat-x'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
	});
	
	results_html = results_html+"</dl><span class='votos_total'>Total Votos: "+total_votes+"</span></div>\n";
	
	$("#poll-container").append(results_html).fadeIn("slow",function(){animateResults();});
	
	
}




