// Genera horas amigables (e.g. "Hace 15 min") que se actualizan
// automágicamente. Por Jaime G. Wong (http://jgwong.org/)
// 
// Llamar a set_friendly_timers() con un resultado de jQuery selectors.
// Hay que pasarle objetos del DOM que tengan los siguientes atributos:
// - time: Que es el timestamp de PHP.
// - normal: El estilo CSS cuando el texto es normal.
// - highlight: El estilo CSS cuando el texto es iluminado por reciente.
// 
// e.g.: <span class="time" time="1318439220" normal="info_noticia"
//        highlight="info_noticia_high">
// 
// Y entonces:
// 
// set_friendly_timer($('span.time'));
// 

function set_friendly_time(obj) {
  $.each(obj, function(i, o) {
    var oo = $(o);
    var od  = oo.attr('time');
    var bt  = oo.attr('base');
    var cd  = Math.round((new Date(bt*1000)).getTime() / 1000);
    
    var period = cd - od;
    var fecha  = 'Hace unos mins';
    var high   = true;
    
    if(period > (60 * 4)) {
      fecha = 'Hace ' + Math.round(period / 60) + ' min';
      high  = true;
    }
    if(period > (60 * 59)) {
      fecha = 'Hace una hora';
      high  = false;
    }
    if(period > (60 * 64)) {
      high  = false;
      d     = new Date(od * 1000);
      h     = d.getHours();
      m     = d.getMinutes();
      
      if(h == 0) {
        h    = 12;
        ampm = 'AM';
      }
      else if(h >= 13) {
        h    = h - 12;
        ampm = 'PM';
      }
      else {
        ampm = 'AM';
      }
      
      fecha = h + ':' + (m < 10 ? '0' : '') + m + ' ' + ampm;
    }
    
    oo.text(fecha);
    
    oo.removeClass();
    
    if(high) {
      oo.addClass(oo.attr('highlight'));
    }
    else {
      oo.addClass(oo.attr('normal'));
    }
  });
}


function set_friendly_timers(obj) {
  set_friendly_time(obj);
  
  setInterval(function() {
    set_friendly_time(obj)
  }, 30000);
}

/* CRV 20111122 */
function get_friendly_time(od, bt) {
  var tdy = new Date(bt*1000);
  var yst = new Date(tdy.getTime() - (24 * 60 * 60 * 1000));
  var cd  = Math.round(tdy.getTime() / 1000);
  var period = cd - od;
  var fecha  = 'Hace unos mins';

  if(period > (60 * 4)) {
    fecha = 'Hace ' + Math.round(period / 60) + ' min';
  }
  if(period > (60 * 59)) {
    fecha = 'Hace una hora';
  }
  if(period > (60 * 64)) {
    d     = new Date(od * 1000);
    h     = d.getHours();
    m     = d.getMinutes();
    dd    = d.getDate();
    mm    = d.getMonth() + 1;
    yyyy  = d.getFullYear();

    if ((h >= 0) && (h < 12)) {
      ampm = 'AM';
    } else {
      ampm = 'PM';
    }

    if(h == 0) {
      h    = 12;
    } else if (h >= 13) {
      h    = h - 12;
    }

    if ((tdy.getDate() == dd) && ((tdy.getMonth() + 1) == mm) && (tdy.getFullYear() == yyyy)) {
      dia = 'Hoy';
    } else if ((yst.getDate() == dd) && ((yst.getMonth() + 1) == mm) && (yst.getFullYear() == yyyy)) {
      dia = 'Ayer';
    } else {
      dia = dd + '/' + (mm < 10 ? '0' : '') + mm + '/' + yyyy;
    }

    fecha = dia + ' | ' + h + ':' + (m < 10 ? '0' : '') + m + ' ' + ampm;
  }
  return fecha;
}

