// JavaScript Document
function BlogPreview(container) {
  this.container_ = container;
}

BlogPreview.prototype.show = function(url, opt_noTitle) {
  var feed = new google.feeds.Feed(url);
  var preview = this;
  feed.load(function(result) {
    preview.render_(result, opt_noTitle);
  });
}

BlogPreview.prototype.render_ = function(result, opt_noTitle) {
  if (!result.feed || !result.feed.entries) return;
  while (this.container_.firstChild) {
    this.container_.removeChild(this.container_.firstChild);
  }

  var blog = this.createDiv_(this.container_, "blog");
  if (!opt_noTitle) {
    var header = this.createElement_("h2", blog, "");
    this.createLink_(header, result.feed.link, result.feed.title);
  }

  for (var i = 0; i < result.feed.entries.length; i++) {
    var entry = result.feed.entries[i];
    var div = this.createDiv_(blog, "entry");
    var linkDiv = this.createDiv_(div, "title");
    this.createLink_(linkDiv, entry.link, entry.title);
    if (entry.author) {
      this.createDiv_(div, "author", "Posted by " + entry.author);
    }
    this.createDiv_(div, "body", entry.contentSnippet);
	var date = this.createDiv_(div, "date");
	date.innerHTML  = entry.publishedDate.toDate().formatted();
  }
}
//DATE
Date.shortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Date.daysOfWeek = ['Sunday','Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturday']
Date.prototype.formatted = function(){
  return Date.daysOfWeek[this.getDay()]+', '+this.getDate()+' '+Date.shortMonths[this.getMonth()]+' '+this.getFullYear();
};

//STRING
String.prototype.toDate = function(){
  var d = new Date();
  d.setTime(Date.parse(this));
  return d;
};


BlogPreview.prototype.createDiv_ = function(parent, className, opt_text) {
  return this.createElement_("div", parent, className, opt_text);
}

BlogPreview.prototype.createLink_ = function(parent, href, text) {
  var link = this.createElement_("a", parent, "", text);
  link.href = href;
  return link;
}

BlogPreview.prototype.createElement_ = function(tagName, parent, className,
                                                opt_text) {
  var div = document.createElement(tagName);
  div.className = className;
  parent.appendChild(div);
  if (opt_text) {
    div.appendChild(document.createTextNode(opt_text));
  }
  return div;
}

