 /*
 * Basado en el ejemplo de AbhiTech pero con grandes reformas
 * http://ad1987.blogspot.com/2009/02/reddit-style-voting-with-php-mysql-and.html
 */
$(function(){
// *****************************************************************************    
// *****************************************************************************





// *****************************************************************************    
// *****************************************************************************    
// *****************************************************************************
// *****************************************************************************



// *****************************************************************************
// contador de caracteres
$('#descripcion').each(function()
{
    // Toma el valor actual
    var length = $(this).val().length;

    $('#counter').html(length + ' caracteres');

    }).keyup(function()
    {
    // actualiza el valor
    var new_length = $(this).val().length;
    $('#counter').html( new_length + ' caracteres');
});
// *****************************************************************************
// disabled para cuando uso texto en vez de link
$("#id_tipo_link").change(function () {
          if($("#id_tipo_link option:selected").val() == 1)
               {
                    $("#urlxx").removeAttr("disabled");
               }
               else
               {
                     $("#urlxx").attr("disabled","disabled");
                
               }

        })
        .trigger('change');
// *****************************************************************************
// powerups!
   $("a.powerup").click(function(){
   the_id = $(this).attr('id');

  //  $("div#puntaje"+the_id).fadeOut("fast");
   $("div#botones_voto"+the_id).remove(); // elimino los botones de voto
   //the main ajax request
    $.ajax({
     type: "POST",
     data: "accion=voto_arriba&accion=powerup&id_link="+$(this).attr("id"),
     url: "actions/voto.php",
     success: function(msg)
     {
         if (isNumber(msg))
         {
              $("div#puntajevisible"+the_id).html(msg); // actualizo el puntaje
         }
         else
         {
             $("span#error"+the_id).html("<br>"+msg); // imprimo error cuando no llega un resultado numérico
         }
     }
    });
   });
// *****************************************************************************
// cancelar
   $("#cancelar").click(function(){

    $("#accion").attr("value", "cancelar");
    $("#formulario").submit();
   });
// *****************************************************************************
// borrar link
    $("#borrarlink").click(function(){

    $("#accion").attr("value", "borrarlink");
    $("#formulario").submit();
   });
// *****************************************************************************
// confirma borrar definitivo
   $("#borrarfinal").click(function(){

    $("#accion").attr("value", "borrarfinal");
    $("#formulario").submit();
   });
// *****************************************************************************
// corregir
   $("#corregir").click(function(){

    $("#accion").attr("value", "agregar");
    $("#formulario").submit();
   });
// *****************************************************************************
// enviar final
    $("#enviarfinal").click(function(){

    $("#accion").attr("value", "agregando");
    $("#formulario").submit();
   });
// *****************************************************************************
// Voto arriba
$("a.voto_arriba").click(function(){
     //get the id
     the_id = $(this).attr('id');
    // alert(the_id);
     // show the spinner
     //$(this).parent().html("<img src='images/spinner.gif'/>");
   
     //fadeout the vote-count
    //$("div#puntaje"+the_id).fadeOut("fast");
    //$("div#botones_voto"+the_id).remove(); // elimino los botones de voto
    //
    //$("div#botones_voto"+the_id).$(".voto_arriba").remove();
    //alert ($("div#botones_voto"+the_id).$(".voto_arriba").html());
    
    
    $(".voto_arriba", $("span#botones_voto"+the_id)).remove();
    $(".voto_abajo", $("span#botones_voto"+the_id)).remove();
    
   //the main ajax request
    $.ajax({
     type: "POST",
     data: "accion=voto_arriba&id_link="+$(this).attr("id"),
     url: "actions/voto.php",
     success: function(msg)
     {
         //alert (msg);
         //alert ($("span#puntaje"+the_id).html());
         if (isNumber(msg))
         {
              //$("span#error"+the_id).html(""); // limpio el cuadro de error
              //$("div#puntaje"+the_id).fadeOut(); // hago fadeout del puntaje
              $("span#puntaje"+the_id).html(msg); // actualizo el puntaje
             // $("span#puntaje"+the_id).toggle(); // hago el fadein del puntaje
         }
         else
         {
             alert(msg); // imprimo error cuando no llega un resultado numérico
         }
     }
    });
   });
// *****************************************************************************
// Voto Abajo
$("a.voto_abajo").click(function(){
 //get the id
 
 the_id = $(this).attr('id');

 // show the spinner
 //$(this).parent().html("<img src='images/spinner.gif'/>");
 //$("div#puntaje"+the_id).fadeOut("fast");
 //$("div#botones_voto"+the_id).remove(); // elimino los botones de voto
 //
     
    $(".voto_arriba", $("span#botones_voto"+the_id)).remove();
    $(".voto_abajo", $("span#botones_voto"+the_id)).remove();
 //the main ajax request
  $.ajax({
   type: "POST",
   data: "accion=voto_abajo&id_link="+$(this).attr("id"),
   url: "actions/voto.php",
   success: function(msg)
   {
         if (isNumber(msg))
         {
              $("span#puntaje"+the_id).html(msg); // actualizo el puntaje
              //$("span#puntaje"+the_id).toggle(); // hago el fadein del puntaje
         }
         else
         {
          alert(msg); // imprimo error cuando no llega un resultado numérico
          }
   }
  });
 });
// *****************************************************************************
// Follow
    $("a.follow").click(function(){
    // seguir
    the_id = $(this).attr('id');
    $("span#seguimiento"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=agregar&id_usuario="+$(this).attr("id"),
       url: "actions/adduser.php",
       success: function(msg)
       {
           // imprimo el mensaje de vuelta, sea el que sea
            $("span#seguimiento"+the_id).html(msg);
            $("span#seguimiento"+the_id).fadeIn();
       }
      });

 });
 // *****************************************************************************
 // Un follow
    $("a.unfollow").click(function(){
    // dejar de seguir
    the_id = $(this).attr('id');
    $("span#seguimiento"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=remover&id_usuario="+$(this).attr("id"),
       url: "actions/adduser.php",
       success: function(msg)
       {
           // imprimo el mensaje de vuelta, sea el que sea
            $("span#seguimiento"+the_id).html(msg);
            $("span#seguimiento"+the_id).fadeIn();
       }
      });
});
// *****************************************************************************
// Revocar
    $("a.revoke").click(function(){
    // dejar de seguir
    the_id = $(this).attr('id');
    $("span#seguidor"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=revocar&id_usuario="+$(this).attr("id"),
       url: "actions/adduser.php",
       success: function(msg)
       {
           // imprimo el mensaje de vuelta, sea el que sea
            $("span#seguidor"+the_id).html(msg);
            $("span#seguidor"+the_id).fadeIn();
       }
      });
      });

    $("a.admit").click(function(){
    // dejar de seguir
    the_id = $(this).attr('id');
    $("span#seguidor"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=autorizar&id_usuario="+$(this).attr("id"),
       url: "actions/adduser.php",
       success: function(msg)
       {
           // imprimo el mensaje de vuelta, sea el que sea
            $("span#seguidor"+the_id).html(msg);
            $("span#seguidor"+the_id).fadeIn();
       }
      });
  });
// *****************************************************************************
// Negar
    $("a.negate").click(function(){
    // dejar de seguir
    the_id = $(this).attr('id');
    $("span#seguidor"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=rechazar&id_usuario="+$(this).attr("id"),
       url: "actions/adduser.php",
       success: function(msg)
       {
           // imprimo el mensaje de vuelta, sea el que sea
            $("span#seguidor"+the_id).html(msg);
            $("span#seguidor"+the_id).fadeIn();
       }
      });
  });
// *****************************************************************************
// Lupitad de más opciones
   $("a.abrir").click(function(){
      
    the_id = $(this).attr('id');
    
    $("div#masopciones"+the_id).toggle();
    cargatags(the_id);
    
   });
// Lupitad de más opciones
   $("a.abriropciones").click(function(){
       the_id = $(this).attr('id');
       //alert(the_id);
    $("div#detallesusuario"+the_id).toggle();
   });

   $("a.abrirdatosusuariohead").click(function(){
       id_usuario = $(this).attr('id');
    $("div.datosusuariohead").toggle();
    cargarelaciones(id_usuario);
   });

// *****************************************************************************
// Favorito
    $("a.botonfavorito").click(function(){
    // seguir
    
    the_id = $(this).attr('id');
    //$("span#seguimiento"+the_id).fadeOut();
      $.ajax({
       type: "POST",
       data: "accion=favorito&id_link="+$(this).attr("id"),
       url: "actions/favorito.php",
       success: function(msg)
       {
           if (msg == 1)
               {
                   $("img#fav"+the_id).attr('src', 'template/images/favon.png');
               }
               else
               {
                    $("img#fav"+the_id).attr('src', 'template/images/favoff.png');
               }
       }
      });

 });
// *****************************************************************************
});

// *****************************************************************************
function isNumber (valor)
{
	return (/^-?((\d+\.?\d*)|(\.\d+))$/).test(valor);
}
// *****************************************************************************
function cargatags(valor)
{
   // alert("mas!");
   $.ajax({
   type: "POST",
   data: "accion=tags&id_link="+valor,
   url: "actions/query.php",
   success: function(msg)
   {
       $("span#tagsextra"+valor).html(msg);
   }
  });
}
function cargarelaciones(valor)
{
   $.ajax({
   type: "POST",
   data: "accion=relaciones&id_usuario="+valor,
   url: "actions/query.php",
   success: function(msg)
   {
       $("div.cromaniones").html(msg);
   }
  });
}
