// Set variables to control which entries in feed are shown
var startPoint = 0;
var endPoint = 4;

google.load("feeds", "1"); // Use Google AJAX Feeds API Version 1
google.setOnLoadCallback(initializeTwitter);

function TwitterFeed(container) {
  this.container_ = container;
}

TwitterFeed.prototype.show = function(url, opt_noTitle) {
  var feed = new google.feeds.Feed(url);
  feed.setNumEntries(endPoint); // Maximum feed entries to show
  var preview = this;
  feed.load(function(result) {
		preview.render_(result, opt_noTitle);
  });
}

TwitterFeed.prototype.render_ = function(result, opt_noTitle) {
  if (!result.feed || !result.feed.entries) return;
  while (this.container_.firstChild) {
    this.container_.removeChild(this.container_.firstChild);
  }

  var tweet = this.createDiv_(this.container_, "tweet-container");
  var count = 0;
  								
  for (var i = startPoint; i < endPoint; i++) {
  
		var entry = result.feed.entries[i];
		var attributes = ["title", "link", "publishedDate", "contentSnippet"];
		
		if (i == 3) {
			var div = this.createDiv_(tweet, "tweet last");
		} else {
			var div = this.createDiv_(tweet, "tweet");
		}
		
		if (result.feed.entries.length < 1) {
			if (count == 0) {
				var titleContainerDiv = this.createDiv_(div, "title-container");				
				this.createDiv_(titleContainerDiv, "title", "Sorry, this feed is unavailable.  Please try again later.");
			}
			
			count += 1;
		}
		
		else {
			if (entry.author) {
				/*
				var itemdate = new Date(entry[attributes[2]]);									
				var itemdate_yr=itemdate.getFullYear();
				var itemdate_mon=itemdate.getMonth()+1;
				var itemdate_day=itemdate.getDate();
				var output= itemdate_mon + "/" + itemdate_day + "/" + itemdate_yr;*/
				//this.createDiv_(div, "author", entry.author + " said on " + output);
				
				var authorNames = entry.author.split(" ");
				this.createDiv_(div, "author", authorNames[0]);
	    	}
			
			var titleContainerDiv = this.createDiv_(div, "title-container");
			var linkDiv = this.createDiv_(titleContainerDiv, "title");
			this.createLink_(linkDiv, entry.link, entry.title);			
		}
  }
  
}

TwitterFeed.prototype.createDiv_ = function(parent, className, opt_text) {
  return this.createElement_("div", parent, className, opt_text);
}

TwitterFeed.prototype.createLink_ = function(parent, href, text) {
  var link = this.createElement_("a", parent, "", text);
  link.href = href;
  return link;
}

TwitterFeed.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;
}
	
function initializeTwitter() {
	//var url = "http://search.twitter.com/search.atom?lang=en&q=+%23weeds";
	// original URL + some bad word filtering is run through a Yahoo Pipe to get MORE filtering:

	//var url = "http://pipes.yahoo.com/pipes/pipe.run?_id=PF7DD_U73hGvHhSYDoSbGg&_render=rss";
	//var url = "http://search.twitter.com/search.atom?lang=en&q=+%23weeds";
	//var url = "http://search.twitter.com/search.atom?lang=en&q=%22%22kevin+nealon%22";
	var url = "http://googleajaxsearchapi.blogspot.com/atom.xml";

	var twitterModule = new TwitterFeed(document.getElementById("tweets"));
	
	twitterModule.show(url);
	
	return false;
}
