// JavaScript Document

function Application() {

	this.ativaFormulario = function() {

		if (jQuery("#termo").is(":checked")) {

			jQuery("#frm-cadastro").show();

		} else {

			jQuery("#frm-cadastro").hide();

		}

	}

	this.escreveURL = function(value) {
		
		jQuery("#urlusuario").html(value);

	}

	this.submitCadastro = function() {
		
		if ($("#uploading").val() == "1") {
		
			alert("Aguarde o término do upload!");
			return false;	
		}

		jQuery("#frm-cadastro").ajaxSubmit( {
			success : function(response) {
				try {
					eval(response);
				} catch (e) {
					alert(response);
				}

			}
		});

	}

	this.updateValor = function(value) {

		jQuery("#ACTION_ENVIA").val(value);
		
		
	}

	
	this.ordenarNoticia = function(value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
				
		AppAjax.request('GET_LISTA_ORDENADA',{parameters:"ordenacao="+value,debug:true});
	
	}
	
	this.noticiaporCategoria = function(categoria,value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_NOTICIA_CATEGORIA',{parameters:"cat="+categoria+"&ordenacao="+value,debug:true});	
				
	}
	
	this.noticiaporUsuario= function(usuario,value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_NOTICIA_USUARIO',{parameters:"user="+usuario+"&ordenacao="+value,debug:true});	
					
	}
	
	this.noticiasPublicadas = function(value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_NOTICIA_PUBLICADA',{parameters:"ordenacao="+value,debug:true});	
				
	}
	
	this.noticiaPesquisada = function(value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_NOTICIA_PESQUISA',{parameters:"ordenacao="+value,debug:true});	
				
	}
	
	this.ordenarMinhasNoticiaPublicadas = function(value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_MINHA_NOTICIA_PUBLICADA',{parameters:"ordenacao="+value,debug:true});	
				
	}
	
	this.noticiasRecentes = function(value,obj) {
		
		jQuery(".order_links a").removeClass("operacao-selected");
		
		jQuery(obj).addClass("operacao-selected");
		
		AppAjax.request('GET_NOTICIA_RECENTE',{parameters:"ordenacao="+value,debug:true});	
				
	}
	
	this.adicionarVoto= function(value,value2,obj) {
		
		jQuery(obj).parent().append('<a href="javascript:void(0)" class="botao-vote votado">VOTADO</a>');
		jQuery(obj).remove();
		AppAjax.request('ADD_VOTO',{parameters:"id_noticia="+value+"&obj_link="+obj+"&num_votos="+value2,debug:true});
				
	}	
	
	this.upload = function(obj) {
		
		$(obj).parent().find("img.loading").show();
		
		input_up = document.createElement("input");
		input_up = obj;
		input_up.type = "file";
		input_up.name = "arquivo";
		
		$("#form_upload")[0].appendChild(input_up);
		$("#form_upload").submit();
		
		$("#uploading").val("1");
		
	}
	
	this.discardUploaded = function() {
		
		if (confirm("Tem certeza que deseja descartar esta imagem?")) {
			
			$("#img_preview").remove();
			$("#container_upload").append('<input type="file" name="foto" onchange="App.upload(this)"/>');
			$("#foto_uploaded").val("");
			$("#uploading").val("0");
		
		}
		
	}
	
	this.cancelAlterar = function() {
		
		$("#container_upload").hide();
		$("#container_foto").show();
		$("#button_alterar").show();
		
	}
	
this.alterarFoto = function(obj) {
		
		if (confirm("Tem certeza que deseja descartar esta imagem?")) {
			
			$("#img_preview").remove();
			$("#container_upload").html('<input type="file" name="foto" onchange="App.upload(this)"/><a href="javascript:void(0)" onclick="App.cancelAlterar()" class="link-avatar"> Cancelar alteração</a>').show();
			$("#foto_uploaded").val("");
			$("#uploading").val("0");
			
			$(obj).hide();
			$("#container_foto").hide();
		
		}
		
	}
	
	this.compartilhe= function(value) {
		
		AppAjax.request('COMPARTILHE',{parameters:"id="+value,debug:true});
				
	}	

}

var App = new Application();