RSS = {
	newsFeedUrl: "mvnews.cgi",
	rssUrl: "mvnews-rss.cgi",
	rssCount: 4,
	rssLength: 64,
	maxRSSInline: 250, // max RSS length to display inline
	RSSLoadedNote: "RSSLoadedNote",
	RSSFailedNote: "RSSFailedNote",

	getRSS: function(request){
		var onError = function(err, arg){
			Notification.postNotification(RSS.RSSFailedNote, this, {errorCode:err, argument:arg});
		}
		var onLoad = function(el){
			var rss = new RSS.RSS(el);
			Notification.postNotification(RSS.RSSLoadedNote, this, rss);
		}
		Utils.sendHTTPRequest(RSS.newsFeedUrl, request, this, onLoad, onError);
	},
	
	RSS: function(el)
	{
		Utils._setObjectProperties(this, el, RSS.model.RSS);
		return this;
	},


	RSSChannel:function(el)
	{
		Utils._setObjectProperties(this, el, RSS.model.RSSChannel);
		return this;
	},

	RSSItem: function RSSItem(el)
	{
		Utils._setObjectProperties(this, el, RSS.model.RSSItem);
		this.name = el.nodeName;
		return this;
	}
}

RSS.model = {
	RSS: {
		channel: {name:"channels", array:true, objectClass: RSS.RSSChannel}
	},
	RSSChannel: {
		item: {name:"items", array:true, objectClass: RSS.RSSItem},
		attributes: {name:String}
	},
	RSSItem: { }
}

RSS.RSSItem.prototype = {
	toString: function() {return 'RSSItem';},

	setEntryDate: function(value) 
	{
		this.entryDate = new Date(value);
	},

	setReleaseDate: function(value) 
	{
		this.releaseDate = new Date(value);
	},


	getReleaseDate: function() 
	{
		return Utils.dateToString(this.releaseDate);
	},

	descriptionNode: function() 
	{
		if(this.description){		
			var div = $element('div');
			div.innerHTML = this.description;
			div.setAttribute('class', 'rss');
			return div;
		}else
			return null;
	},

	// Returns rss item node (div) truncating decription to length if trunc == true
	itemNode: function(trunc, length)
	{
		var node = $element('div');
	//	var span = $element('span');
		var span = $element("p");
		span.className = "rss-date";
		span.appendChild($text(this.getReleaseDate()));
		node.appendChild(span);

		var h = $element('h4'); //????
		if(this.link && this.link.indexOf('void') < 0){ // if has link and the link does not contain 'void'
			var link = $element('a');
			link.href = this.link;
			link.appendChild($text(this.title));
			h.appendChild(link);
		}else
			h.appendChild($text(this.title));
	//	h.appendChild(span);
		h.className = 'rss';
		node.appendChild(h);
		var descEl = null;
		if(trunc)
			descEl = this.truncatedDescriptionNode(length);
		else
			descEl = this.descriptionNode();
		if(descEl)
			node.appendChild(descEl);
		return node;
	},

	truncatedDescriptionNode: function(length) 
	{
		var descNode = this.descriptionNode();
		if(descNode){
			var h = new Object();
			h.length = length;
			var node = this.truncatedNode(descNode, h);
			if(h.truncated){ // add link 'more'
				var anchor = $element('a');
				anchor.href = 'javascript:void(0)';
				anchor.node = node;
				anchor.item = this;
				anchor.appendChild($text('more'));
				node.appendChild(anchor);
				var toggle = function(src, eventObj){
					var item = src.item;
					var info = new Object();
					info.node = src.node.parentNode;
					info.event = eventObj;
					Notification.postNotification("ShowRSSItem", src.item, info);
				}
				if(anchor.addEventListener)
					anchor.addEventListener('click', function(eventObj){
						var target = eventObj.target;
						toggle(target, eventObj);
						//eventObj.cancelBubble = true;
					}, false);
				else if(anchor.attachEvent)
					anchor.attachEvent('onclick', function(){
						var target = event.srcElement;
						toggle(target, event);
						//event.cancelBubble = true;
					});
			}
			return node;
		}
		return null;
	},

	truncatedNode: function(node, h) 
	{
		var newNode = null;
		try{
			if(node.nodeType == 3){
				if(node.nodeValue && node.nodeValue.length > h.length){
					if(h.length > 4)
						newNode = $text(node.nodeValue.substr(0, h.length - 4) + '... ');
					else
						newNode = $text('... ');
					h.length = 0;
					h.truncated = true;
				}else{
					newNode = $text(node.nodeValue);
					h.length -= node.nodeValue.length;
				}
			}else if(node.nodeType == 1){
				var l = 0;
				var n = 0;
				var k = node.childNodes.length;
				newNode = node.cloneNode(false);
				for(n=0;n<node.childNodes.length && h.length > 0;n++){
					var child = this.truncatedNode(node.childNodes[n], h);
					if(child)
						newNode.appendChild(child);
				}
			}
		}catch(e){
			alert("Error in truncateNode: " + e);
		}
		return newNode;
	}
}

RSS.RSS.prototype = {
	toString: function() { return 'RSS';},
	
	setError: function(value)
	{
		this.error = value;
	}
}

RSS.RSSChannel.prototype.toString = function() {return 'RSSChannel';}

