bh.new_at_bh = function() {

  var that = {},
      container;


  var init = function(element_id) {

    container = $('#' + element_id);

    if(container.length > 0) {
      bh.loader.add(element_id);

      $.getJSON('/new_at_baker_hughes.json', function(data) {
        bh.loader.remove(element_id);
        create_list(data.value.items);
        navigation().init();
      });
    }

  },

  create_list = function(items) {
    var list_length = 0,
        wrap = $('<div class="wrap"></div>'),
        list = $('<ul></ul>');

    list_length = items.length < 21 ? items.length - 1 : 21;

    for(var i = 0; i < list_length - 1; i++) {
      item(items[i]).appendTo(list);
    }

    list.appendTo(wrap);

    wrap.appendTo(container).hide().fadeIn();
  },

  item = function(item) {
    var li = $('<li></li>'),
        title = item.title,
        link = item.link,
        pubDate = item.pubDate,
        source = {link: item["source:link"], title: item["source:title"]};

    $('<a></a>', {
      text: title.truncate_at_word(32),
      href: link,
      title: title
    }).appendTo(li);

    $('<span></span>', {
      "class": "from",
      text: "From: "
    }).append(
    $('<a></a>', {
      text: source.title,
      href: source.link
    })).appendTo(li);

    $('<span></span>', {
      text: getDate(pubDate) + ':',
      "class": "date"
    }).appendTo(li);

    $('<span></span>', {
      text: time_ago(pubDate),
      "class": time_ago
    }).appendTo(li);

    return li;
  },

  navigation = function() {
    var up    = {},
        down  = {},
        that  = {},

    init = function() {

      var nav   = $('<div class="nav"></div>'),
          arrow = "M0,0l5,0l-2.5,5z";

      up.arrow = $('<span></span>');
      down.arrow = $('<span></span>');

      up.link = $('<a></a>', {
        text: "newer",
        'class': "newer",
        click: function() { shift("up", {up: up, down: down}); }
      }).prepend(up.arrow);

      down.link = $('<a></a>', {
        text: "older",
        'class': "older",
        click: function() { shift("down", {up: up, down: down}); }
      }).prepend(down.arrow);

      down.link.appendTo(nav);
      up.link.appendTo(nav);

      nav.appendTo(container);

      up.paper = Raphael(up.arrow[0], 5, 5);
      down.paper = Raphael(down.arrow[0], 5, 5);

      up.vector = up.paper.path(arrow).attr({"stroke-width":0, rotation:180});
      down.vector = down.paper.path(arrow).attr({"stroke-width":0});

      enable(down);
      disable(up);
    },

    enable = function(item) {
      item.link.css({cursor:"pointer", color: ""});
      item.vector.attr({fill:"#2573b4"});
    },

    disable = function(item) {
      item.link.css({cursor:"default", color:"#ccc"});
      item.vector.attr({fill:"#ccc"});
    };

    that.init = init;
    that.enable = enable;
    that.disable = disable;

    return that;

  },

  shift = function(direction, nav) {
    var list = container.find('ul'),
        wrap = container.find('div.wrap'),
        multiplier = list.find('li:first-child').height(),
        bottom = list.height() - (multiplier * 3),
        list_top = parseInt(list.position().top, 10);

    list.css({position: "relative"});

    if(wrap.find('>:animated').length === 0) {
      if(direction === 'down' && bottom >= (-1 * list_top)) {
        list.animate({
          top: list_top - (multiplier * 3)
        }, 500, function() { check_nav(); });
      }
      else if(direction === 'up' && list_top !== 0) {
        list.animate({
          top: list_top + (multiplier * 3)
        }, 500, function() { check_nav(); });
      }
    }


    var check_nav = function() {
      var list_top = parseInt(list.position().top, 10);

      if(list_top !== 0) {
        navigation().enable(nav.up);
      }
      else {
        navigation().disable(nav.up);
      }
      if(bottom > (-1 * list_top)) {
        navigation().enable(nav.down);
      }
      else {
        navigation().disable(nav.down);
      }
    };

  },


  getDate = function(d) {
    var pubdate, today, month, day, year;

    if(d) {
      pubdate  = new Date(d);
      month    = pubdate.getMonth() + 1;
      day      = pubdate.getDate();
      year     = pubdate.getFullYear();
    }
    else {
      today = new Date();
      month = today.getMonth();
      day   = today.getDay();
      year  = today.getFullYear();
    }

    return month + '.' + day + '.' + year;
  },

  time_ago = function(d) {
    if(!d) return 'Today';

    else {
      var today     = new Date().getTime(),
          other_day = Date.parse(d),
          offset    = parseInt((today - other_day) / 86400000, 10);

      if(offset === 0) return 'Today';
      else return pluralize('day', offset) + ' ago';
    }
  },

  pluralize = function(word, num) {
    if(num === 1) return '1 ' + word;
    else return num + ' ' + word + 's';
  },
  
  truncate = function(word, num) {
    if(word.length > num - 3) {
      var regex = new RegExp('(.{0,' + (num - 3) + '}).*');
      return word.replace(regex,'$1...');
    }
    else return word;
  };

  that.init = init;

  return that;

}();

