$(document).ready(function() {
 mostrar_votos();

 $('.auto-submit-star').rating({
  callback: function(value, link){
  	votar(value);
  }

 });

});


var validator;
function enviar_comentario(){
	usuario_logueado=$("#usuario_logueado").val();
	if (usuario_logueado!=1){
		$("#ComentarioResp").show();
		$("#ComentarioResp").html("Debe estar logueado en el sistema para poder dejar un comentario.");
	}
	else{
		$("#ComentarioResp").hide();
		generarValidador();
		if(validator.form()){
			if (validarUsuarioCaptcha()==true){
				enviar();
			}
		}
	}
	return false;
}

function enviar(){
	$("#accion").val('comentario_enviar');
	$.ajax({
		url: "ajax_noticias_comentarios.php",
		type: "POST",
		data: $("#f1").serialize(),
		success: function(data){
			$("#comentario").val('');
			$("#codigo").val('');
			$("#ComentarioResp").show();
			$("#ComentarioResp").html(data);
			actualizar_comentarios();
		}
	});
}

function actualizar_comentarios(){
	$("#accion").val('actualizar_comentarios');
	$.ajax({
		url: "ajax_noticias_comentarios.php",
		type: "POST",
		data: $("#f1").serialize(),
		success: function(data){
			$("#box_comments").html(data);
		}
	});
}



function generarValidador(){
	// validate signup form on keyup and submit
	validator = $("#f1").validate({
		rules: {
			comentario: "required"
		},
		messages: {
			comentario: "...Requerido"
		}
		});
}


/** change font news **/
var max=19;
var min=10;
var numero= 12;
function increaseFontSize() {
	if(numero!=max) numero += 1;
    else numero=10;
	$("#nota_txt_noticia").css("font-size", numero);
    $("#nota_txt_noticia > *,#nota_txt_noticia > * > *,#nota_txt_noticia > * > * >*,#nota_txt_noticia > * > * > * > * ,#nota_txt_noticia > * > * > * > * > *,#nota_txt_noticia > * > * > * > * > * > *,#nota_txt_noticia > * > * > * > * > * >* >*,#nota_txt_noticia > * > * > * > * > * >* >*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*>*").css("font-size", numero);

}

function decreaseFontSize() {
	if(numero!=min) numero -= 1;
    else numero=19;
	$("#nota_txt_noticia").css("font-size", numero);
    $("#nota_txt_noticia > *,#nota_txt_noticia > * > *,#nota_txt_noticia > * > * >*,#nota_txt_noticia > * > * > * > * ,#nota_txt_noticia > * > * > * > * > *,#nota_txt_noticia > * > * > * > * > * > *,#nota_txt_noticia > * > * > * > * > * >* >*,#nota_txt_noticia > * > * > * > * > * >* >*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*>*,#nota_txt_noticia > * > * > * > * > * >* >*>*>*>*").css("font-size", numero);

}


/** end change font news **/








function validarUsuarioCaptcha(){
	$("#accion").val('ValidarCaptcha');
	valido=false;
	$.ajax({
			type: "POST",
			url: "ajax_noticias_comentarios.php",
			async: false,
			data: $("#f1").serialize(),
			success: function(data){
				if (data==false){
					$("#err_Captha").show();
					document.getElementById("imgCaptcha").src="../common/php/captcha.php?dummy="+Math.random();
				}
				else{
					$("#err_Captha").hide();
					valido= true;
				}
			}
		});
		return valido;
}


/** mail popup **/
function popupMail(id) {
	var url = "mailer_news.php?id="+id;
	newwindow=window.open(url,'TiempoSur_Mail','height=580,width=529');
	if (window.focus) {newwindow.focus()}
}
/** end mail popup **/


/** print popup **/
function popupPrint(id) {
	var url = "print_news.php?id="+id;
	newwindow=window.open(url,'TiempoSur_Imprimir','height=600,width=550,scrollbars=yes');
	if (window.focus) {newwindow.focus()}

}
/** end print popup **/






function mostrar_votos(){
	$.ajax({
		url: "ajax_votar.php",
		type: "POST",
		async:false,
		data: $("#f1").serialize(),
		success: function(data){
			$("#votos").html(data);
		}
	});
}


function votar(value){
	$("#accion").val('votar');
	$("#voto_value").val(value);
	$.ajax({
		url: "ajax_votar.php",
		type: "POST",
		data: $("#f1").serialize(),
		success: function(data){
			$("#votos").html(data);
			$('.auto-submit-star').rating({
			  callback: function(value, link){
			  	votar(value);
			  }

			 });
		}
	});
}

