var Format = {
  // helper method that converts \n to <br/> & <p>
  // used when rendering comments
  text: function(text) {
    text = text.replace(/\r\n?/g, "\n");                    // \r\n and \r -> \n
    text = text.replace(/\n\n+/g, "</p>\n\n<p>");           // 2+ newline  -> paragraph
    text = text.replace(/([^\n]\n)(?=[^\n])/g, '$1<br />'); // 1 newline   -> br
    return "<p>" + text + "</p>";
  },

  quantity_class_name: function(count) {
    return count > 0 ? "not_empty" : "empty";
  },

  iso_date: function(iso_date) {
    var date = new Date.from_iso(iso_date);
    return date.strftime('%a %m/%d/%y %I:%M %p'); // e.g.: Wed 04/22/09 8:53 AM
  },
  
  colors: [ [255,0,0] ],
  color_index: 0,
  get_color: function(percentage) {
    if(Format.colors.length > 1) {
      var color = Format.colors[Format.color_index];
      Format.color_index = (Format.color_index + 1 == Format.colors.length) ? 0 : Format.color_index + 1;
    } else {
      var base_color = Format.colors[0];
      var color = [];
      var inverse_percentage = 100 - percentage;
      var max_value = 225;
      for(var i in base_color) {
        color_diff = base_color[i] < max_value ? max_value - base_color[i] : 0;
        color[i] = base_color[i] + Math.floor(inverse_percentage * color_diff / 100);
      }
    }
    return "rgb(" + color.join(",") + ")";
  },
  
  truncate_length: 30,
  
  truncate: function(text) {
    if(text.length <= Format.truncate_length) {
      return text;
    } else {
      var last_ix = Format.truncate_length - 1;
      return text.substring(0, last_ix) + '...'
    }
  }
}
