function RSSWidget( config )
{
	if (!$("#newsItem"))
	{
		var div = document.createElement("div");
		div.id = "newsItem";
		$(".tabBarPages")[0].appendChild(div);
	}
	this.position = config.position;
	this.type = config.type;
	this.name = config.name;
	this.baseURL = config.baseURL;
	this.path = config.path;
	this.slug = config.slug;
	this.carousel = config.carousel;
	this.cssClass = this.carousel ? "Carousel" : this.type;
	this.feedURL = config.feedURL;
}

RSSWidget.prototype.init = function()
{
	var widget = this;
	Loader.start();
	// Do Ajax to fetch feed
	$.ajax( "GET", this.path, function( widgetData )
	{
		widget.processFeed( widgetData );
		Loader.stop();
	});
};

RSSWidget.prototype.unload = function()
{
	if (this.carousel)
	{
		clearInterval(this.carouselTimer);
	}
	$("#" + this.slug + "_content").innerHTML = "";
};

RSSWidget.prototype.processFeed = function(feed)
{
	if (feed)
	{
		this.feed = feed;
		var widget = this;
		fillTemplate("#" + this.slug + "_content", this.type + "_list_tmpl", this);
		$.each("#" + this.slug + "_content img", function(img) {
			if(img || img.attributes["src"])
			{
				img.src = widget.baseURL + img.attributes["src"].value;
			}
		});
		$("#" + this.slug + "_content .entries")[0].addEventListener("click", function(e){ widget.clickHandler(e); }, false);
		if (widget.carousel)
		{
			this.currentCarouselIndex = 0;
			widget.carouselTimer = setInterval(function() {
				var images = $("#carousel li");
				var newCarouselIndex = (widget.currentCarouselIndex + 1) % images.length;
				$.addClass(images[newCarouselIndex], "active");
				$.removeClass(images[widget.currentCarouselIndex], "active");
				widget.currentCarouselIndex = newCarouselIndex;
			}, 7000);
		}
	}
	// Show the page
	Strike.show("#" + this.slug);
};

RSSWidget.prototype.clickHandler = function( e ){
	var currentNode = e.target;
	while (currentNode.nodeName !== "LI")
	{
		currentNode = currentNode.parentNode;
	}
	this.loadContent(currentNode.id.split('_')[1], "#newsItem", "next");
};

RSSWidget.prototype.loadContent = function(index, destination, anim)
{
	var widget = this;
	var entry = this.feed.entries[index];
	Loader.start();
	var ajaxOptions = {
		type: "HTML",
		url: widget.path + entry.link,
		method: "GET",
		success: function( content ){
			entry.content = content;
			fillTemplate(destination, widget.type + "_detail_tmpl", {"entry" : entry});
			var images = $(destination + " img");
			var loadedImages = 0;
			$.each(images, function(img) {
				img.src = widget.baseURL + img.attributes["src"].value;
				img.addEventListener("load", function(){
					if(images.length > 0 && ++loadedImages === images.length){
						Strike.rebindScroller();
					}
				}, false);
			});
			$.each(destination + " a", function(a) {
				var href = a.attributes["href"].value;
				if (href && href[0] === '/')
				{
					a.href = widget.baseURL + href;
				}
			});
			Loader.stop();
			manager.toggleBackButton(true);
			Strike[ anim || "next" ](destination);
		}
	};
	$.ajax(ajaxOptions);
};


