/**
 *
 * @projectDescription change all the elements that have the matched class name to convert to days, hours, minutes ago
 *
 * @author Developed by Adam Coogan
 * @version 1.0
 * @since 1.1
 *
 * @example $(".timestamp").ago();
 *  
 * @type jQuery
 *
 */
  
$.fn.ago = function(o) {               
    return this.each(function() {
    new $ago(this, o);
  });      
};

// Default configuration properties.
var defaults = {
  lang_minute_plural: "[NUM] minutes ago",  
  lang_minute_singular: "[NUM] minute ago",
  lang_hour_plural: "[NUM] hours ago",
  lang_hour_singular: "[NUM] hour ago",
  lang_day_plural: "[NUM] days ago",  
  lang_day_singular: "[NUM] day ago"
};

$.ago = function(e, o){
  
  this.config = $.extend({}, defaults, o || {});
    c = this.config;
  
  //TODO: change this to support server time as well as local machine time
  date = new Date();            
  
  mssec = 1000;
    msmin = 60 * mssec;
  mshrs = 60 * msmin;
  msday = 24 * mshrs;
  
  $("."+$(e).attr("class")).each(function(){                           
        var ts = date.getTime() - new Date($(this).text());
    tsday = (ts/msday);
        tshrs = ((ts/mshrs) % 24);
        tsmin = ((ts/msmin) % (60));                                         
    //TODO: Add months ago support                        
    if(Math.floor(tsday)>0) {                               
      $(this).html(Math.floor(tsday) == 1 ? c.lang_day_singular.replace("[NUM]", Math.floor(tsday)) : c.lang_day_plural.replace("[NUM]", Math.floor(tsday)));
      $(this).addClass("day").addClass("day"+Math.floor(tsday));
    } else if (Math.floor(tshrs)>0) {
      $(this).html(Math.floor(tshrs) == 1 ? c.lang_hour_singular.replace("[NUM]", Math.floor(tshrs)) : c.lang_hour_plural.replace("[NUM]", Math.floor(tshrs)));                  
      $(this).addClass("hour").addClass("hour"+Math.floor(tsday));
    } else if (Math.floor(tsmin)>0) {                            
      $(this).html(Math.floor(tsmin) == 1 ? c.lang_minute_singular.replace("[NUM]", Math.floor(tsmin)) : c.lang_minute_plural.replace("[NUM]", Math.floor(tsmin)));    
      $(this).addClass("minute").addClass("minute"+Math.floor(tsday));
    }
    $(this).css("display","inline");
  });     
      
}

// Create shortcuts for internal use
var $ago = $.ago;

/** end **/

