function updateChat() {
    if(ref) { clearTimeout(ref); }
    last = $("#last").val();
    $("#estado").html("buscando...");
    $.ajax({
        type: 'POST',
        url: 'index.php',
        data: "md="+last,
        success: function(m) {
            //clausuramos #info
            $("#info").html('').hide();
            $("#posts").show();
            var data = jQuery.parseJSON(m);
            odata = $("#chatito LI");
            if(data.length >0 && data.length <= odata.length) {
               $("#chatito LI").slice(-data.length).remove();
               $("#chatito LI").removeClass("i2");
            } else if(data.length > 0 && data.length >= odata.length) {
               ////$("#chatito LI").slideDown();
            }
            var x = 0;
            $.each(data,function(i,item) {
               if(x == 0) { x = item.i; }
               if(me != '' && me == item.n) {
                  $("#m"+$("#last").val()).before('<li id="m'+item.i+'" class="i3"><img src="'+item.p+'" align="left" class="av" alt="'+item.n+'" /> '+item.m+'<br /><small><span class="rr" alt="'+item.n+'">responder a '+item.n+'</span></small></li>');
               } else {
                  $("#m"+$("#last").val()).before('<li id="m'+item.i+'"><img src="'+item.p+'" alt="'+item.n+'" class="av" align="left" /></a> '+item.m+'<br /><small><span class="rr" alt="'+item.n+'">responder a '+item.n+'</span> '+item.f+'</small></li>');
               }
               ////abajo("#m"+item.i);
            });
            if(x > 0) {
               $("#m"+$("#last").val()).addClass('i2');
               $("#last").val(x);
            }
            //$("#estado").html(data.length);
            ref = setTimeout("updateChat()",1000*30);
            $("#estado").html("Actualizado.");
        }
    });
   return false;
}
function user(uu) {
   $("#estado").html("obteniendo info...");
   if(ref) { clearTimeout(ref); }
   $.ajax({
      type:'GET',url:'index.php',data: 'u='+uu,
      success: function(m) {
         $("#posts").hide();
         $("#info").show();
         $("#info").html(m);
         $("#estado").html('');
      }
   });
   return false;
}
function upChat() {
   if(ref) { clearTimeout(ref); }
   $("#estado").html("enviando...");
   $.ajax({
      type: 'POST', url: 'index.php', data: $("#f").serialize()+'&d=1',
      success: function(m) {
         $("#touch").val('');
			$("#estado").text(m);         
			ref = setTimeout("updateChat()",1000*3);
      }
   });
   return false;
}
$(document).ready(function(){
   $(".rr").live('click',function() {
      var x = $(this).attr('alt');
      $("#touch").val("@"+x+" "+$("#touch").val()).focus();
   });
   $(".av").live('click',function() {
      $("#estado").html("obteniendo info..."); var x = $(this).attr('alt'); if(ref) { clearTimeout(ref); }
      $.ajax({ type:'GET',url:'index.php',data: 'u='+x, success: function(m) { $("#posts").hide(); $("#info").show(); $("#info").html(m); $("#estado").html(''); } });
   });
});


