var months = new Array(13);
months[0]  = "January";
months[1]  = "February";
months[2]  = "March";
months[3]  = "April";
months[4]  = "May";
months[5]  = "June";
months[6]  = "July";
months[7]  = "August";
months[8]  = "September";
months[9]  = "October";
months[10] = "November";
months[11] = "December";

var monthDay = new Array(31);
monthDay[0]  = "1st";
monthDay[1]  = "2nd";
monthDay[2]  = "3rd";
monthDay[3]  = "4th";
monthDay[4]  = "5th";
monthDay[5]  = "6th";
monthDay[6]  = "7th";
monthDay[7]  = "8th";
monthDay[8]  = "9th";
monthDay[9]  = "10th";
monthDay[10]  = "11th";
monthDay[11]  = "12th";
monthDay[12]  = "13th";
monthDay[13]  = "14th";
monthDay[14]  = "15th";
monthDay[15]  = "16th";
monthDay[16]  = "17th";
monthDay[17]  = "18th";
monthDay[18]  = "19th";
monthDay[19]  = "20th";
monthDay[20]  = "21st";
monthDay[21]  = "22nd";
monthDay[22]  = "23rd";
monthDay[23]  = "24th";
monthDay[24]  = "25th";
monthDay[25]  = "26th";
monthDay[26]  = "27th";
monthDay[27]  = "28th";
monthDay[28]  = "29th";
monthDay[29]  = "30th";
monthDay[30]  = "31st";

function BlogPreview(container) {
  this.container_ = container;
}

BlogPreview.prototype.show = function(url, opt_noTitle) {
  var feed = new google.feeds.Feed(url);
  feed.setNumEntries(8);
  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");
	linkDiv.className = "green";
    this.createLink_(linkDiv, entry.link, entry.title);
    if (entry.author) {
	  var tempDate = new Date (entry.publishedDate);
	  var author = this.createDiv_(div, "author", months[tempDate.getMonth()] + "  " + monthDay[tempDate.getDate()] + " // by "+entry.author);
		author.className = "date";  
    }
	
// this.createDiv_(div, "body", entry.contentSnippet);
  }
}

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;
}
