// JavaScript Document
function FeedReader(container) {
  this.container_ = container;
	this.params_ = null;
	this.header_ = null;
	this.feeds_ = null;
	this.debug_ = document.getElementById("elpais-debug");
}
FeedReader.prototype.show = function(url, params) {
	this.params_ = params;
	this.adjustContainerSize_(params["width"], params["height"]);
  var feed = new google.feeds.Feed(url);
  feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
  if (!params["numEntries"]) params["numEntries"] = 20;
	feed.setNumEntries(params["numEntries"]);
  var preview = this;
  feed.load(function(result) {
    preview.render_(result);
  });
}
FeedReader.prototype.render_ = function(result) {
  if (!result.feed || !result.feed.entries) return;
  while (this.container_.firstChild)
    this.container_.removeChild(this.container_.firstChild);
  if (!this.params_["hideTitle"]) {
    this.header_ = this.createDiv_(this.container_, "elpais-channelTitleDiv");
		this.createChannelTitle_(this.header_, result, this.params_["hideLogo"], this.params_["logoUrl"], this.params_["width"]);
	}
  this.feeds_ = this.createDiv_(this.container_, "elpais-feeds");
	this.adjustFeeds_();
	this.feeds_.style.overflowX = "hidden";
	if (this.params_["scrollable"]) 
		this.feeds_.style.overflowY = "auto";
	else
		this.feeds_.style.overflowY = "hidden";
	for (var i = 0; i < result.feed.entries.length; i++) {
    var entry = result.feed.entries[i];
    var div = this.createDiv_(this.feeds_, "elpais-entry");
    var linkDiv = this.createDiv_(div, "elpais-entryTitle");

	  if (!this.params_["hideImages"]) 				this.createEntryImage_(div, entry, i);
		if (!this.params_["hideTitles"]) 				this.createLink_(linkDiv, "elpais-entryLink", entry.link, entry.title);
		if (!this.params_["hideDescriptions"])	this.createDivHTML_(div, "elpais-entryBody", entry.content);
		if (!this.params_["hideDomains"]) 			this.createLink_(div, "elpais-entryDomain", result.feed.link, this.getDomain_(result.feed.link));
  }
	
	this.removeOverflow_();
}
FeedReader.prototype.adjustFeeds_ = function() {
	var header = this.header_ ? this.header_.clientHeight + 20: 0;
	if (this.feeds_) this.feeds_.style.height = (this.params_["height"] - header) + "px";
}
FeedReader.prototype.removeOverflow_ = function() {
	if (!this.params_["scrollable"]) {
		if (this.feeds_ != null && this.feeds_.childNodes != null) {
			var currentHeight = 0;
			var i = 0;
			var entry;
			if (!this.params_["hideTitle"]) currentHeight = this.header_.offsetHeight;
			while (i < this.feeds_.childNodes.length) {
				entry = this.feeds_.childNodes[i];
				if (currentHeight + i * 10 > this.params_["height"]) {
					this.feeds_.removeChild(entry);
					entry = null;
				}
				else {
					currentHeight += entry.offsetHeight;
					i++;
				}
			}
			if (currentHeight > this.params_["height"] && entry != null)
				this.feeds_.removeChild(entry);
		}
	}
}
FeedReader.prototype.createDiv_ = function(parent, className, opt_text) {
  return this.createElement_("div", parent, className, opt_text);
}
FeedReader.prototype.createLink_ = function(parent, className, href, text) {
  var link = this.createElement_("a", parent, className, text);
  link.href = href;
  link.setAttribute("target", "_blank");
  return link;
}
FeedReader.prototype.createElement_ = function(tagName, parent, className, opt_text) {
  var div = document.createElement(tagName);
  div.className = className;
  if (parent != null) parent.appendChild(div);
  if (opt_text) div.appendChild(document.createTextNode(opt_text));
  return div;
}
FeedReader.prototype.createDivHTML_ = function(parent, className, opt_text) {
  return this.createElementHTML_("div", parent, className, opt_text);
}
FeedReader.prototype.createElementHTML_ = function(tagName, parent, className, html, elemId) {
  var div = document.createElement(tagName);
  div.className = className;
  div.innerHTML = html;
  div.id = elemId;
  parent.appendChild(div);
  return div;
}
FeedReader.prototype.createImage_ = function(src, className, alt, title, onload) {
  var image = document.createElement("img");
  image.className = className;
	image.onload = onload;
  image.setAttribute("src", src);
  image.setAttribute("alt", alt);
  image.setAttribute("title", title);
	return image;
};
FeedReader.prototype.findLogo_ = function(result) {
		var imageUrl;
		var image = result.xmlDocument.getElementsByTagName("image")[0];
		if (image != null) image = image.getElementsByTagName("url")[0];
		else image = result.xmlDocument.getElementsByTagName("logo")[0];
		if (image != null) imageUrl = image.firstChild.nodeValue;
		return imageUrl;
}
FeedReader.prototype.createChannelTitle_ = function(parent, result, hideLogo, imgSrc, width) {
  var link = this.createLink_(parent, "elpais-channelTitle", result.feed.link, "");
	if (imgSrc == null)
		imgSrc = this.findLogo_(result);
  if (imgSrc != null && !hideLogo) {
		var feedReader = this;
		var onload = function() {
			feedReader.adjustImageSize_(this);
			feedReader.adjustFeeds_();
			feedReader.removeOverflow_();
		};
		link.appendChild(
			this.createImage_(imgSrc, "elpais-channelLogo", result.feed.title, result.feed.title, onload));
	}
  else this.createElement_("span", link, "elpais-channelTitleSpan", result.feed.title);
  return link;
}
FeedReader.prototype.getDomain_ = function(link) {
	return link.replace("http://", "").split("/")[0];
}
FeedReader.prototype.createEntryImage_ = function(parent, entry, i) {
	var image = google.feeds.getElementsByTagNameNS(entry.xmlNode, "http://search.yahoo.com/mrss/", "thumbnail")[0];
	if (image != null) {
		var imageUrl = image.getAttribute("url");
		if (imageUrl != null) {
			var div = this.createDiv_(parent, "elpais-entryImageDiv");
			var link = this.createLink_(div, "elpais-entryImageLink", entry.link, "");
			var feedReader = this;
			var onload = function() {
				feedReader.adjustImageSize_(this);
				feedReader.removeOverflow_();
			};
			link.appendChild(this.createImage_(imageUrl, "elpais-entryImage", entry.title, entry.title, onload));
			return div;
		}
	}
}
FeedReader.prototype.adjustContainerSize_ = function(width, height) {
	this.container_.style.width = width + "px";
	this.container_.style.height = height + "px";
}
FeedReader.prototype.adjustImageSize_ = function(image) {

	var maxWidth = this.params_["width"];
	if (image.width > maxWidth) {
		image.height = image.height * maxWidth / image.width;
		image.width = maxWidth;
	}
	return image;
}
