$(function(){
	
	/* Inicia funções principais  */
    /* ====================================================================== */
	
	initPNG();
	initMask();
	
	
	/* Verifica o cpf do usuário  */
    /* ====================================================================== */
	
	$("#cpf").bind("blur", function(){
		
		var url = window.location.href;
		var bar = url.split("/");
		var pag = bar[3];
		
		$(this).removeClass("frm_loader");
		$(this).removeClass("frm_error");
		
		if (pag != "meusdados" || $(this).val() == "")
		{
			if (isCpf(this.value)) 
			{
				verificaCPF("#cpf");
			}
		}
		
	});	
	

	/* Limite de texto ao postar no diário  */
    /* ====================================================================== */
	
	$("#diario #postagem").maxlength({
    	'feedback' : '#maxlength'
    });
	
	
	/* Limite de texto ao criar notícia  */
    /* ====================================================================== */
	
	$("#noticia #titulo").maxlength({
    	'feedback' : '.maxtitulo'
    });
	
	$("#noticia #historyline").maxlength({
    	'feedback' : '.maxhistory'
    });
	
	
	/* Limite de texto ao postar comentário  */
    /* ====================================================================== */
	
	$("#comentar #texto").maxlength({
    	'feedback' : '.maxcomment'
    });


	/* Abas  */
    /* ====================================================================== */
	
	$(".aba_noticia").live("click", function(){
		$(this).css({"background-position": "0 0"});
		$(".aba_diario").css({"background-position": "0 0"});
		carregaClip($(this).attr("url")+'?id='+$(this).attr("iduser"));									 
	});
	
	$(".aba_diario").live("click", function(){
		$(this).css({"background-position": "0 -37px"});
		$(".aba_noticia").css({"background-position": "0 -37px"});
		carregaClip($(this).attr("url")+'?id='+$(this).attr("iduser"));									 
	});
	
});

function defineLocal(c)
{
	if ($(c).val() == "Brasil")
	{
		$("#estado_brasil").show().children().removeAttr("disabled").attr("name", "estado");
		$("#estado_outros").hide().children().attr("disabled", "disabled").attr("name", "");
		
		$("#cidade_brasil").show().children().removeAttr("disabled").attr("name", "cidade");
		$("#cidade_outros").hide().children().attr("disabled", "disabled").attr("name", "");
	}
	else if ($(c).val() == "Todos")
	{
		$("#estado_brasil").hide().children().attr("disabled", "disabled").attr("name", "");
		$("#estado_outros").hide().children().attr("disabled", "disabled").attr("name", "");
		
		$("#cidade_brasil").hide().children().attr("disabled", "disabled").attr("name", "");
		$("#cidade_outros").hide().children().attr("disabled", "disabled").attr("name", "");
	}
	else
	{
		$("#estado_brasil").hide().children().attr("disabled", "disabled").attr("name", "");
		$("#estado_outros").show().children().removeAttr("disabled").attr("name", "estado");
		
		$("#cidade_brasil").hide().children().attr("disabled", "disabled").attr("name", "");
		$("#cidade_outros").show().children().removeAttr("disabled").attr("name", "cidade");
	}
}

function initMask()
{
	$(".mask_data").mask("99/99/9999");
	$(".mask_fone").mask("(99) 9999-9999");
	$(".mask_cpf").mask("999.999.999-99");
	$(".mask_cep").mask("99999999");
}

function initCrop()
{
	$('#cropbox').Jcrop({
		minSize: [ 116, 116 ],
		maxSize: [ 200, 200 ],
		allowResize: true,
		allowSelect: true,
		allowMove: true,
		aspectRatio: 1,
		onSelect: updateCoords	
	});	
}

function updateCoords(c)
{
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
}

function initPNG()
{
	$(document).pngFix();
}

function manipulaComentarios(bt, dv)
{
	$(bt).toggle();
	$(dv).slideToggle("slow");	
}

function carregaClip(p)
{
	$("#conteudo").fadeOut("fast", function(){
		
		$("#conteudo_loader").fadeIn("normal");
		
		if (p.indexOf("?") > 0)
		{
			int = p.split("?");
			pag = int[0].substring(0, int[0].length);
			qry = p.substring(int[0].length, p.length);
		}
		else
		{
			int = "";
			pag = p;
			qry = "";
		}
		
		$.ajax({
			url: pag+".php"+qry,
			cache: false,
			dataType: 'html',
			data: { localhost: 0 },
			success: function(html){
				
				$("#conteudo_loader").fadeOut("fast", function(){
					$("#conteudo").fadeIn("normal"); 
					$("#conteudo").empty().append(html);
					$.include('../_js/gampi.site.js');
				}); 
				
			}
		});	
		
	}); 
}