$(document).ready(function()  {
	$('.textoBasico')
		.fck({
			path: '/lib/fckeditor/',
			height: '150',
			toolbar:'Basic'
	});
	$('.textoCompleto')
		.fck({
			path: '/lib/fckeditor/',
			height: '400',
			toolbar:'Default'
	});
    xOffset=10; yOffset=20;
    $(".tooltip").hover(function(e){
        this.t=this.title;
        this.title="";
        $("body").append("<p id='tooltip'>"+ this.t +"</p>");
        $("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("normal");
    },function(){
        this.title = this.t;
        $("#tooltip")
        	.remove();
    });
    $(".tooltip").mousemove(function(e){
        $("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });
});
function toggleComentario(id){
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "";
	}
	else {
		document.getElementById(id).style.display = "none";
	}
}
/**
 *
 * @access public
 * @return void
 **/
function trocarImagem(){
	$('#imagemExibir').hide();
	$('#imagemTrocar').show();
}
/**
 *
 * @access public
 * @return void
 **/
function cancelarTroca(){
	$('#imagemExibir').show();
	$('#imagemTrocar').hide();
	$('#imagem_exibicao').val("");
}

/**
 * Validação do formulário de cadastro do usuário
 * @access public
 * @return true ou false
 **/
function validaCadastroUsuario(){
	$('#campo').hide();
	$('#aguarde').show();
	if ($('#nome').val() == "") {
		alert("Preencha o campo nome corretamente!");
		$('#nome').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#email').val() == "") {
		alert("Preencha o campo e-mail corretamente!");
		$('#email').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	/*
	else if ($('#telefone').val() == "") {
		alert("Preencha o campo telefone corretamente!");
		$('#telefone').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	*/
	else if ($('#cidade').val() == "") {
		alert("Preencha o campo cidade corretamente!");
		$('#cidade').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#estado').val() == "") {
		alert("Preencha o campo estado corretamente!");
		$('#estado').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#pais').val() == "") {
		alert("Preencha o campo país corretamente!");
		$('#pais').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#idioma').val() == "") {
		alert("Preencha o campo idioma corretamente!");
		$('#idioma').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#usuario').val() == "") {
		alert("Preencha o campo usuario corretamente!");
		$('#usuario').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	else if ($('#senha').val() == "") {
		alert("Preencha o campo senha corretamente!");
		$('#senha').focus();
		$('#campo').show(); $('#aguarde').hide();
		return false;
	}
	return true;
}


/**
 * Ativa e Desativa as categorias do usuário
 * @access public
 * @return true
 **/
function categoriasUsuarios(id){
	if (id) {
		var opcao = $('#campo_' + id).val();
		if (opcao == "desativar") {
			$('#categoria_' + id).slideToggle();
			$('#campo_' + id).val("ativar")
			$('#opcao_' + id).html("desativar")
		}
		else if (opcao == "ativar") {
			$('#categoria_' + id).slideToggle();
			$('#campo_' + id).val("desativar")
			$('#opcao_' + id).html("ativar")
		}
	}
}
/**
 * Ativa e Desativa as fontes do usuário
 * @access public
 * @return true
 **/
function fontesUsuarios(id){
	if (id) {
		var opcao = $('#fonte_' + id).val();
		if (opcao == "desativar") {
			$('#fonte_' + id).val("ativar");
			$('#fonte_opcao_' + id).html("desativar");
			$('#fonte_cor_' + id).css({'color' : 'rgb(108, 142, 27)'});
		}
		else if (opcao == "ativar") {
			$('#fonte_' + id).val("desativar");
			$('#fonte_opcao_' + id).html("ativar");
			$('#fonte_cor_' + id).css({'color' : 'rgb(197, 37, 37)'});
		}
	}
}

/**
 *
 * @access public
 * @return void
 **/
function classifica(voto, baseUrl){
	if (voto != "") {
		var retorno = "";
		for( var i=1 ; i <= 10 ; i++ ) {
			if (voto >= i) {
				retorno += "<img title=\"" + i + "\" alt=\"" + i + "\" id=\"classificacao_" + i + "\" onclick=\"return classifica('" + i + "', '" + baseUrl + "');\" src=\"" + baseUrl + "images/estrela_positivo.png\"/>";
			}
			else {
				retorno += "<img title=\"" + i + "\" alt=\"" + i + "\" id=\"classificacao_" + i + "\" onclick=\"return classifica('" + i + "', '" + baseUrl + "');\" src=\"" + baseUrl + "images/estrela_negativo.png\"/>";
			}
		}
		$('#classificacao').val(voto);
		$('#classificacao_div').html(retorno);
	}
}
/**
 *
 * @access public
 * @return void
 **/
function classificaCanal(voto, baseUrl){
	if (voto != "") {
		var retorno = "";
		for( var i=1 ; i <= 10 ; i++ ) {
			if (voto >= i) {
				retorno += "<img title=\"" + i + "\" alt=\"" + i + "\" id=\"classificacaoCanal_" + i + "\" onclick=\"return classificaCanal('" + i + "', '" + baseUrl + "');\" src=\"" + baseUrl + "images/estrela_positivo.png\"/>";
			}
			else {
				retorno += "<img title=\"" + i + "\" alt=\"" + i + "\" id=\"classificacaoCanal_" + i + "\" onclick=\"return classificaCanal('" + i + "', '" + baseUrl + "');\" src=\"" + baseUrl + "images/estrela_negativo.png\"/>";
			}
		}
		retorno += "&nbsp;&nbsp;&nbsp;<a href=\"javascript:;\" onclick=\"formularioVota.submit();\">salvar voto</a>";
		$('#classificacaoCanal').val(voto);
		$('#classificacaoCanal_div').html(retorno);
	}
}
/**
 *
 * @access public
 * @return void
 **/
function corListagemCategorias(registro, canal){
	if (!canal) {
		var canal = "";
	}
	if ($("#campo_" + registro).is(':checked') == true) {
		$('#div_' + registro).css("background", "#F9F9F9");
		mostraFontesListagem(canal);
	}
	else {
		$('#div_' + registro).css("background", "#FFFFFF");
		mostraFontesListagem(canal);
	}
}
/**
 *
 * @access public
 * @return void
 **/
function corListagemFontes(registro){
	if ($("#fonte_" + registro).is(':checked') == true) {
		$('#div_fonte_' + registro).css("background", "#F9F9F9");
	}
	else {
		$('#div_fonte_' + registro).css("background", "#FFFFFF");
	}
}
/**
 *
 * @access public
 * @return void
 **/
function mostraFontesListagem(canal){
	if (!canal) {
		var canal = "";
	}
	var camposAjax = "";
	var contador = 1;
	$("input:checked[name*=campo]").each(function() {
		camposAjax += "categoria" + contador + "=" + $(this).val() + "&";
		contador++;
	});
	camposAjax += "canal=" + canal;
	$.ajax({
	   type: "POST",
	   url: "/usuarios/fontes-listagem-ajax/",
	   data: camposAjax,
	   success: function(msg){
			$('.fontes_listagem').html(msg);
		}
	});
}
/**
 *
 * @access public
 * @return void
 **/
function votaNoticiaPositivo(noticia){
	if (!noticia) {
		var noticia = "";
	}
	var camposAjax = "noticia="+noticia;
	$('#votacao-'+noticia).fadeOut("slow", function() {
		$.ajax({
		   type: "POST",
		   url: "/vota-noticia/positivo/",
		   data: camposAjax,
		   success: function(msg){
				$('#votacao-'+noticia).html(msg);
				$('#votacao-'+noticia).fadeIn();
			}
		});
	});
}
/**
 *
 * @access public
 * @return void
 **/
function votaNoticiaNegativo(noticia){
	if (!noticia) {
		var noticia = "";
	}
	var camposAjax = "noticia="+noticia;
	$('#votacao-'+noticia).fadeOut("slow", function() {
		$.ajax({
		   type: "POST",
		   url: "/vota-noticia/negativo/",
		   data: camposAjax,
		   success: function(msg){
				$('#votacao-'+noticia).html(msg);
				$('#votacao-'+noticia).fadeIn();
			}
		});
	});
}
/**
 *
 * @access public
 * @return void
 **/
function retratioMenuAbre(id) {
	$("#" + id).show("slow");
}
/**
 *
 * @access public
 * @return void
 **/
function retratioMenuFecha(id) {
	$("#" + id).hide("slow");
}