var timerinbox;
var delayinbox=40;
function updateInbox() {
   $.getJSON("/dump.php?source=inbox",
      function(data){
         old = $("#inbox").html(); $("#inbox").html(data.inbox);
         if(old != data.inbox) { delayinbox=30; abajo("#inbox"); } 
         else { delayinbox=120;  }
         delete old;
         if(data.msg) { $("#mensaje").html(data.msg); }
         if(data.last) { $("#lastmsg").html(data.last); }
   });
   clearTimeout(timerinbox);
   timerinbox = setTimeout("updateInbox()",1000*delayinbox);
}
var timerposts;
var delayposts=17;
var lastposts = 0;
var updatepostsno=0;
function updatePosts() {
   clearTimeout(timerposts);
   if(updatepostsno>=150) {
      $("#mensaje").html('<h3>Ha pasado mucho tiempo inactivo, por favor refresque la página para obtener actualizaciones.</h3>');
      clearTimeout(timerinbox);
      return false;
   }
   if($('#estado')) { $('#estado').html('<img src="/load.gif" alt="..." />'); }  
   $.getJSON("/dump.php?source=posts"+(pub && pub==1?'&pub=1':''),
   function(data){
      if(data.last == lastposts) { delayposts=60; } 
      else { delayposts=17; }
      $("#latest").html(data.me);
      $.each(data.items, function(i,item){
         $("#upi"+i).attr({src:item.upi,alt:item.ulo});
         $("#ulo"+i).html('<a href="'+item.ulo+'" class="luser">'+item.ulo+'</a>');
         var tmp = item.bod;
         if(item.pro == '1') { tmp = '<img src="/il.gif" /> ' +tmp; }
         $("#bod"+i).html(tmp);
         $("#pos"+i).text(item.pos);
         var tmp = (item.rto !== '') ? ' en <a href="/'+item.rto+';m;'+item.rep+'">respuesta a '+item.rto : '</a>';
         $("#met"+i).html('<em><a href="/'+item.ulo+';m;'+item.pos+'" title="Detalles">'+item.fec+'</a>, '+item.app+tmp+'</em>');
         if(item.del == '1') { $("#del"+i).show(); }
         else { $("#del"+i).hide(); }
         if(item.pos>lastposts) { abajo2("#i"+i); }
      });
      lastposts = data.last;
      if($('#estado')) { $('#estado').html('<img src="/white.gif" alt="..." />'); }
      updatepostsno++;
   });
   timerposts = setTimeout("updatePosts()",1000*delayposts);
   $('#irt').val('');
}
/* oculta o muestra la galería */
function hideGal() {
   var c = $.cookie('mdx_gal'); $('#uimages').toggle();
   if(c == "0" || c === null) { $.cookie('mdx_gal','1',{ expires: 100 });} 
   else { $.cookie('mdx_gal','0',{ expires: 100 }); }
   return false;
}
/* twitter */
/* permite enviar o no los posts a twitter */
$(document).ready(function(){
   $("#twitter").change(function(){
    if ($("#twitter:checked").val() == '1') {
       $("#mensaje").html("Desde ahora serán enviados los mensajes a twitter.");
       $.cookie('mdx_twit','1',{ expires:100 });
    } else {
       $("#mensaje").html("Ya no se enviarán los mensajes a twitter.");
       $.cookie('mdx_twit','0',{ expires:100 });
    }
   });
});


/* envia privados */
$(document).ready(function(){    
   $(".user-picture").click(function () { 
      var x = $(this).attr("id").substring(3);x = $("#ulo"+x).html();x = x.replace(/<\/?[^>]+>/gi, '');
      $("#sendmdto").text(x);
      $("#sendmd").fadeIn();
      $("#sendmdtext").focus();
      $("#sendmdtox").val(x);
      //enviaPriv(x);
   });
});
/* sy */
$(document).ready(function() {
   $(".sy").click(function () {
      $('#postit').val($('#postit').val()+' '+$(this).text()); 
   });
});
function follow(u,t) {
   $.ajax({ type:'POST',url:'/dump.php',data:'follow='+u+'&t='+t, success: 
          function(msg) { $("#fll"+t).fadeOut(); $("#umensaje").html(msg); $("#umensaje").fadeIn(); } 
   });
}
/* envia mensajes directos */
function enviaPriv() {
   var to = $("#sendmdtox").val();
   var ms  = $("#sendmdtext").val();
   if (ms==''||ms === null||ms.length<3) {alert("Su mensaje para "+to+" es muy corto, no será enviado.");return false;}
   $("#mensaje").html("Enviando mensaje a <strong>"+to+"</strong>...");
   $.ajax({
      type:"POST",url:'/dump.php',data: 'msgto='+to+'&msg='+ms,
      success: function(msg) {$("#sendmdtox").val('');$("#sendmdtext").val('');$('#mensaje').html(msg);$("#sendmd").fadeOut();}
   });   
   $("#postit").focus();
   return false;
}
function soemd(e) {
   var keycode;
   if (window.event) keycode = window.event.keyCode;
   else if (e) keycode = e.which;
   else return true;
   if (keycode == 13) { return enviaPriv(); } 
   else return true;
}
/* fade */
function abajo(arg) { $(arg).hide().fadeIn('slow'); }
function abajo2(arg) { $(arg).hide().slideDown(); }
/* Dynamic Version by: Nannette Thacker */ 
function textCounter(field,cntfield,maxlimit) {
   if (field.value.length<1) $("#postb").attr("disabled","disabled");
   else { $("#postb").removeAttr("disabled"); }
   if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
   else $("#countfield").text(maxlimit - field.value.length);
}
function enviaPost() {
   if($('#postit').val() == '') {
      $('#mensaje').html('Eso no dice mucho :-(');
      return false;
   } else if($('#img').val() === '') {
      //normal;
      clearTimeout(timerposts);
      if($('#estado')) { $('#estado').html('<img src="/load.gif" alt="..." />'); }
      $("#postb").attr("disabled","disabled");
      $.ajax({
         type: "POST",url: '/dump.php',data: $('#formitas').serialize(),
         success: function(msg) {
            $('#mensaje').html(msg);$("#postit").val('');$("#vid").val('');$("#video").hide();$("#postit").focus();
            if($('#estado')) { $('#estado').html('<img src="/white.gif" alt="..." />'); }
            updatePosts();
         }
      });
      return false;
   } else if(!_cft($('#img').val())) {
      alert('Solo puede elegir archivos del tipo .jpg .gif o .png');
      $('#img').val(null); 
      cuantos = 254; $('#kount').val(cuantos);
      return false;
   } else {
      if($('#estado')) { $('#estado').html('<img src="/load.gif" alt="..." />'); }
      clearTimeout(timerposts);
      document.forms.formita.submit();
   }
}
/* eliminar foto de galería */
function gidel(id) {
   $('#gi'+id).html('<br />Espere, eliminando foto...');
   $.ajax({ type: "POST",url: '/dump.php',data: 'delpic='+id,success: function(msg) { $('#gi'+id).html(msg); } });
}
function _cft(filename) { var re = /\.(jpg|jpeg|gif|png)$/gi; return (re.exec(filename) !== null) ? true : false;; }
var cuantos = 254; //caracteres por escribir
function _adjust(arg) {
   if (arg == 'img' && $('#img').val() != '') { cuantos = 100; } 
   else if(arg == 'vid') { cuantos = 100; } 
   else { cuantos = 254; }
   $("#countfield").text(cuantos);
   $("#postit").focus();
}
function _pr(i) {
   $('#postit').val($('#postit').val()+' @'+$('#ulo'+i).text()+' '); 
   $('#postit').focus(); 
   $('#irt').val($('#pos'+i).text()+';'+$('#ulo'+i).text()); 
   return false; 
}
function delminipost(i) {
   var post = $("#pos"+i).html();$("#mensaje").html("Eliminando mensaje #"+post+"... ");$("#del"+i).hide();
   $.ajax({
          type: 'POST',url:'/dump.php',data: 'delpost='+post,
          success: function (msg) {
             if(msg == 'true') {updatePosts();$("#mensaje").html("Mensaje eliminado.");             }
             else { alert('el mensaje no es suyo.'); }
          }
   });
   return false;
}
        function _(url,nombre,ancho,alto) {
           ancho = ancho ? ancho : 600; alto = alto ? alto : 480; nombre = nombre ? nombre : 'aux';
           aux = window.open(url,nombre,"width="+ancho+",height="+alto+",toolbar=no,directories=no,location=no,copyhistory=no,scrollbars=yes,resizable=yes,status=no");
           aux.focus();
           return false;
        }

function tinythis() {
   var msg=window.prompt("Escriba la dirección:");
   if(msg == '' || msg === null) { $('#postit').focus();return false; }
   if(msg.length<25) { $('#postit').val($('#postit').val()+' '+msg+' '); 
   $('#postit').focus();  return false;   }
   $('#mensaje').html("Intentando obtener tinyurl de "+msg+" ...");
   $.ajax({
          type:'POST',url:'/dump.php',data:'tiny='+msg,
          success: function(m) {
             $('#postit').val($('#postit').val()+' '+m+' '); 
             $('#postit').focus();
             $('#mensaje').html('Se convirtió '+msg+' a '+m);
          }
   });
   return false;
}

function submitenter(myfield,e) {
   var keycode;
   if (window.event) keycode = window.event.keyCode;
   else if (e) keycode = e.which;
   else return true;
   if (keycode == 13) { return enviaPost(); }
   else return true;
}


