var stock_quote = function() {
  
  var container = $('div.stock_quote'),
      markup = {},
      that = {};
      
  
  var getData = function() {
    $.getJSON('/stock_price.json', function(data) {
      that.stock_price = data.lastprice;
      that.change = data.change;
      if(parseFloat(that.stock_price) > 0.0) {
        container.addClass('module active');
        createMarkup();
      }
    });
    
  };
  
  var createMarkup = function() {
    var header = $('<h3></h3>', { text: 'Baker Hughes Stock' }),
        price = $('<h4></h4>', { text: 'Last price' })
                   .after($('<span></span>', { 'class': 'price', text: "$" + that.stock_price })),
        day_change = $('<p></p>', { text: 'Day Change' })
                        .append($('<span></span>', { 'class': 'change', text: that.change + "%" })),
        links = $('<a></a>', { text: 'Share', href: '/share-stock'}).after(
                $('<a></a>', { text: 'View Full Stock Report', href: '/stock-full-report'}));
                        
    container.append(header)
             .append($('<div></div>')
               .append(price)
               .append(day_change))
             .append(links);
  };
  
  if(container.length > 0) {
    getData();
  }
  
};

$(function() {
  stock_quote();
});
