
$(document).ready(function() {
	// INICIO
	switch(pagina){
		case "home":
			init_twitter_tts();
			home_menu();
			$("#painel_doadores").thumbsPanelHome({jsonPath:"benfeitores_lista.php", rootPath:path});
		break
		case "doacao":
			
			imagePreview();
			
			$("input[type=file]").filestyle({ 
				 image: path+"img/btn_anexar.png",
				 imageheight : 27,
				 imagewidth : 94,
				 width : 94
			 });
		break
		case "diretoria":
			$(".diretoria").mouseover(function(){
				$(this).find(".descricao .area").unbind("mouseout");
				$(this).find(".descricao").show();
				$(this).find(".descricao .area").animate({opacity:0.1},50);
				$(this).find(".descricao .area").mouseout(function(){
					$(this).parents(".descricao").hide();
				});
			})
		break
	}
	
	init_imgToBackground(".parceiros .foto");
	
	/**********************************/
	/*  banner do rodape - parceiros  */
	/**********************************/
			
	$('ul#img-parceiros').innerfade({
		speed: 1000,
		timeout: 2500,
		type: 'random',
		containerheight: '140px'
	});
	
});


function home_menu(){
	
	$(".menu a").each(function(){
		$(this).click(function(){
			if(!$(this).hasClass("atual")){
				$(".menu a[class=atual]").removeClass("atual");
				$(this).addClass("atual");
				var rel = $(this).attr("rel");
				$(".banner").slideUp(function(){
					$(".banner").load(path+"banner.php?id="+rel, function(){
						$(".banner").slideDown();
					});
				});
			}
		})
	});
	
}



// TROCA IMAGENS NO HTML E ACIONA NO BACKGROUND

function init_imgToBackground(element){
	$(element+" img").each(function(i){
		$(this).parent().css("background","url("+$(this).attr("src")+") no-repeat center center");
		$(this).hide();
	})
}



// TWITTER //

function init_twitter_tts(){
	
	var box1Search = "from:vscottolengo";
	
	$.getJSON("http://search.twitter.com/search.json?q="+box1Search+"&rpp=3&callback=?", function(data) {
		if(data.results.length > 0){
			var parsedURL;
			for(i=0;i<data.results.length;i++){
				parsedURL = data.results[i].text.parseURL().parseUsername().parseHashtag();
				$("<div class=\"twit\"><div class=\"msg\"><a href=\"http://twitter.com/"+data.results[i].from_user+"\" target=\"_blank\" class=\"user\">@"+data.results[i].from_user+"</a> "+parsedURL+"<br /></div><div class=\"clear\">&nbsp;</div></div>").appendTo(".tts");
			}
			$(".twit a").attr("target", "_blank");
		}
		
	});
	
}



String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
		return url.link(url);
	});
};
String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};
String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

//---------------------PRÓXIMO CAMPO---------------------------------------------------------
function jrs_Valida(campo, prox, total)
{
	if(campo.value.length>total-1)document.getElementById(prox).focus();
}

//MÁSCARA DE VALORES
function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8 || nTecla == 0) {
			return true;
		}
	}
    sValue = objeto.value;
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++;
	  }else{
        sCod += sValue.charAt(nCount);
        nCount++;
      }
      i++;
    }
    objeto.value = sCod;

	if (nTecla != 8) { // backspace ou tab
		if(sMask.charAt(i-1) == "9"){ // apenas números...
			return ((nTecla > 47) && (nTecla < 58));
		}else{ // qualquer caracter...
			return true;
		} 
	}else{
		return true;
	}
}

function validaEmail(email){
	ER = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64})*\.[a-z]{2,4}$");
	if (ER.test(email)){
		return true;
	}else{
		return false;
	}
}

function carregaPaginacao(pagina){
	$("#atualiza_pag").load(pagina);
}

/******************************************************************************/
// ENVIA CONTATO
/******************************************************************************/
function Envia_Contato(form){
var nome=form.nome.value;
var email=form.email.value;
var cidade=form.cidade.value;
var estado=form.estado.value;
var comentario=form.comentario.value;
var lingua=form.lingua.value;
	if(lingua == "pt") {
		if (nome==""){
			$(".form_resposta").html("Digite o Nome.");											
			form.nome.focus();
		} else if (email==""){
			$(".form_resposta").html("Digite o Email.");													
			form.email.focus();
		} else if(!validaEmail(email)){
			$(".form_resposta").html("Formato do Email Incorreto.");													
			form.email.focus();
		} else if (cidade==""){
			$(".form_resposta").html("Digite a Cidade.");											
			form.cidade.focus();
		}else if (estado==""){
			$(".form_resposta").html("Digite o Estado.");											
			form.estado.focus();
		}else if (comentario==""){
			$(".form_resposta").html("Digite o Coment&aacute;rio.");											
			form.comentario.focus();
		} else {	
			$.post('scripts/envia_contato.php',$(form).serialize(),function(data){
				$(".form_resposta").html(data);
				form.reset();
			});
		}		
	}else if(lingua == "en") {
		if (nome==""){
			$(".form_resposta").html("Enter Name.");											
			form.nome.focus();
		} else if (email==""){
			$(".form_resposta").html("Enter your Email.");													
			form.email.focus();
		} else if(!validaEmail(email)){
			$(".form_resposta").html("Wrong Email Format.");													
			form.email.focus();
		} else if (cidade==""){
			$(".form_resposta").html("Enter the City.");											
			form.cidade.focus();
		}else if (estado==""){
			$(".form_resposta").html("Enter the State.");											
			form.estado.focus();
		}else if (comentario==""){
			$(".form_resposta").html("Enter the Comment.");											
			form.comentario.focus();
		} else {	
			$.post('scripts/envia_contato.php',$(form).serialize(),function(data){
				$(".form_resposta").html(data);
				form.reset();
			});
		}				
	}		
}

