newsokRSSreader = Class.create();
newsokRSSreader.prototype = Object.extend(new Ajax.Request(), {
	initialize: function( url, max, placeholder, opt ) {
		this.url		= url;
		this.maxitems		= max;
		this.placeholder	= placeholder;
		this.options		= opt || {};
		this.items		= new Array();
		this.channel		= {};
		this.more		= 0;

		var onSuccess = this.options.onSuccess || Prototype.emptyFunction;
		var onFailure = this.options.onFailure || Prototype.emptyFunction;

		this.options.onSuccess = (function(t) { if( this.onSuccess(t) ) { onSuccess(this); } else { this.onFailure(t); onFailure(this); } }).bind(this);
		this.options.onFailure = (function(t) { this.onFailure(t); onFailure(this); }).bind(this);
		this.params = '';

	 	new Ajax.Request( this.url, { method: 'get', parameters: this.params, onComplete: this.options.onSuccess });
	},
	onSuccess:  function(t) {
		try {
			var node = t.responseXML;
			var xmlChannel = node.getElementsByTagName('channel').item(0);	
		} catch(e) {
			return false;
		}
		
		try {
			this.channel = { 
				title: node.getElementsByTagName('title').item(0).firstChild.data,
				link: node.getElementsByTagName('link').item(0).firstChild.data
			}
		} catch(e) { }
		
		var xmlItems = node.getElementsByTagName('item');

		text = "";
		for (var n=0; (n<xmlItems.length) && (n < this.maxitems); n++) {
			try {
				var xmlItem = xmlItems[n];
				var cat = "";
				var xmlCat = xmlItem.getElementsByTagName('category');
				for(var j=0; j<xmlCat.length; j++){
					if(j>0) cat += ',';
					cat += xmlCat[j].firstChild.nodeValue;
				}
			} catch(e) { }
			var item = {
				title:		this._getElementValue(xmlItem, 'title'),
				link:		this._getElementValue(xmlItem, 'link'),
				description:	this._getElementValue(xmlItem, 'description'),
				author:		this._getElementValue(xmlItem, 'author'),
				comments:	this._getElementValue(xmlItem, 'comments'),
				guid:		this._getElementValue(xmlItem, 'guid'),
				pubDate:	this._getElementValue(xmlItem, 'pubDate'),
				category:	cat	
			};

			if (item.title.length) this.items.push(item);
		}
		if (xmlItems.length > this.maxitems) this.more = 1;
		return true;
	},

	_getElementValue: function( node, elementName ) {
		var value = ''; 
		try { 
			var cells = node.getElementsByTagName(elementName);
			value = cells[0].firstChild.data;
		} catch(e) { }
		return value;
	},

        onFailure : function( t ) { 
	}
		
});

