RSSHandler = function() { }

RSSHandler.prototype = {
	newsTitle: "Map Viewer News",
	
	loadRSSHeader: function(channel)
	{
		var title = this.rssTitle;
		if(title){
			title.removeChild(title.firstChild);
			if(channel.link){
				var link = $element('a');
				link.href = channel.link;
				link.appendChild($text(channel.title));
				title.insertBefore(link, title.firstChild);
			}else
				title.appendChild($text(channel.title));
		}
	},

	getRSSItems: function(channel)
	{
		var items = new Array();
		if(channel.items)
			for(var n=0;n<channel.items.length;n++)
				items.push(channel.items[n]);
		return items;
	},


	updateRSSContainer: function(count)
	{
	// count = 0 ==> show all items
		if(this.rss && this.rss.channels){
			var container = this.rssContainer;
			var channel = this.rss.channels[0];
			var items = channel? channel.items : null;
			container.innerHTML = '';
			if(items && items.length > 0){
				if(count > items.length || count == 0)
					count = items.length;
				for(var n=0;n<count;n++){
					var item = items[n];
					var itemNode = item.itemNode(true, RSS.rssLength);
					itemNode.className = 'rss-item';
					container.appendChild(itemNode);
				}

				var link = $element('a');
				link.appendChild($text("Show all"));
				link.href = this.rssUrl;
				link.className = 'rss-footer';
				link.control = this;
				container.appendChild(link);
			}else if(this.rss.error){
				container.innerHTML = "RSS request error: " + this.rss.error;
				container.style.color = "rgb(171,146,146)";
			}else{
				container.innerHTML = "There is currently no news.";
				container.style.fontStyle = "italic";
				container.style.textAlign = "center";
				container.style.color = "rgb(103,97,97)";
			}
		}else{
			var container = this.rssContainer;
			container.innerHTML = "News not available";
			container.style.color = "rgb(171,146,146)";
		}
	},

	loadRSS: function(note)
	{
		this.rss = note.userInfo;
		this.rssIndex = 0;
		this.updateRSSContainer(RSS.rssCount);
		NotificationCenter.removeObserver(this, RSS.RSSLoadedNote, null);
		NotificationCenter.removeObserver(this, RSS.RSSFailedNote, null);
	},

	setItemFrame: function(infoPanel, elementFrame)
	{
		var r1 = Rect.elementRect(infoPanel);
		infoPanel.style.top = (elementFrame.y + 20) + "px";
		infoPanel.style.left = (elementFrame.x - r1.width - 20) + "px";
	},

	showRSSItem: function(note)
	{
		var item = note.object;
		if(window.Ext){
			this.showRSSItemWindow(item, {title:this.newsTitle, el:note.userInfo.node});
		}else if(this.infoPanel){
			var info = note.userInfo;
			var newNode = item.itemNode();
			if(this.infoPanel.itemNode)
				this.infoPanel.removeChild(this.infoPanel.itemNode);
			this.infoPanel.itemNode = newNode;
			this.infoPanel.appendChild(this.infoPanel.itemNode);
			this.infoPanel.className = 'rss-info';
			var r = Rect.elementRect(info.node.parentNode); // use parent node - IE returns incorrect offset for info.node
			this.setItemFrame(this.infoPanel, r);
			this.infoPanel.style.visibility = 'visible';
			if(info.event)
				info.event.cancelBubble = true;
		}
	},

	showRSSError: function(note)
	{
		var msg;
		switch(note.userInfo.errorCode){
			case Utils.ParserError:
				msg = "XML parser error:<br>" + note.userInfo.argument;
				break;
			case Utils.LoadError:
				msg = "RSS load error: " + note.userInfo.argument;
				break;
			case Utils.ServerError:
				msg = "RSS server error: server returned status " + note.userInfo.argument;
				break;
			case Utils.RequestError:
				msg = "RSS request error: " + note.userInfo.argument;
				break;
		}
		if(this.rssContainer)
			this.rssContainer.innerHTML = msg;
		NotificationCenter.removeObserver(this, RSS.RSSLoadedNote, null);
		NotificationCenter.removeObserver(this, RSS.RSSFailedNote, null);
	},

	getRSS: function()
	{
		try{
			NotificationCenter.addObserver(this, "loadRSS", RSS.RSSLoadedNote, null);
			NotificationCenter.addObserver(this, "showRSSError", RSS.RSSFailedNote, null);
			RSS.getRSS(this.newsRequest);
			NotificationCenter.addObserver(this, "showRSSItem", "ShowRSSItem", null);
		}catch(e){
			logError(e);
		}
	},

// Ext window for RSS
	rssTitle: "News",
	rssWidth: 400,
	rssBodyCls: "rg-rss",
	rssHeaderCls: "rg-rss-header",
	rssOffset: [0, 0],
	rssAlign: "c-c",

	getRSSPanel: function(){
		if(!this.rssPanel){
			this.rssPanel = new Ext.Window({
				autoCreate:true,
				title: this.rssTitle,
				resizable: true,
				constrain: true,
				constrainHeader: true,
				minimizable: false,
				maximizable: false,
				stateful: false,
				modal: false,
				shim: false, // true does not work on OSX FF
				width: this.rssWidth,
				height: "auto",
				autoHeight:true,
				plain:true,
				footer:false,
				closable:true,
				closeAction:"hide"
			});
			this.rssPanel.render(document.body);
			if(this.rssHeaderCls)
				this.rssPanel.header.addClass(this.rssHeaderCls);
		}
		return this.rssPanel;
	},
			
	getTemplate: function(){
		if(!this.template){
			this.template = new Ext.Template(
				"<div><h3>{title}</h3>",
				"<span class='rg-rss-date'>{date}</span>",
				"<div>{description}</div></div>"
			);
			this.template.compile();
		}
		return this.template;
	},
	
	showRSSItemWindow: function(item, config){
		var conf = config || {};
		var offset = conf.offset || this.rssOffset;
		var title = conf.title || this.rssTitle;
		var align = conf.align || this.rssAlign;
		var bodyCls = conf.bodyCls || this.rssBodyCls;
		var p = this.getRSSPanel();
		if(this.bodyEl){
			this.bodyEl.remove();
			this.bodyEl = null;
		}
		if(title)
			p.setTitle(title);
		var t = this.getTemplate();
		var el = config.el || p.container;
			
		this.bodyEl = t.append(p.body, {title:item.title, description:item.description, date:item.getReleaseDate()}, true);
		if(bodyCls)
			this.bodyEl.addClass(bodyCls);
		p.alignTo(el, align, offset);
		p.show();
		return this;
	}
	
}
