// Copyright (c) 2006 Robin Schuil (http://lunchpauze.blogspot.com, http://www.sayoutloud.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// VERSION 0.1

// modified by morita@clockworksweb.com

Ajax.RssReader = Class.create();
Ajax.RssReader.prototype = {
    initialize: function(url, options) {
	options = options || {};
	var onSuccess = options.onSuccess || Prototype.emptyFunction;
	var onFailure = options.onFailure || Prototype.emptyFunction;
	
	var rss = this;

	var func_s = function(t) {
	    rss.onSuccess(t);
	    onSuccess(rss);
	}

	var func_f = function(t) {
	    rss.onFailure(t);
	    onFailure(rss);
	}

	options.onSuccess = func_s;
	options.onFailure = func_f;

	if (!options.method) {
	    options.method = "get";
	}
	new Ajax.Request(url, options);
    },
    onSuccess : function( t ) {
	try {
	    var node = t.responseXML;
	    var xmlChannel = node.getElementsByTagName('channel').item(0);
	}
	catch(e) {
	    return false;
	}
	this.channel = {
	    title: this._getElementValue(xmlChannel, 'title'),
	    link: this._getElementValue(xmlChannel, 'link'),
	    description: this._getElementValue(xmlChannel, 'description'),
	    language: this._getElementValue(xmlChannel, 'language'),
	    copyright: this._getElementValue(xmlChannel, 'copyright'),
	    managingEditor: this._getElementValue(xmlChannel, 'managingEditor'),
	    webMaster: this._getElementValue(xmlChannel, 'webMaster'),
	    pubDate: this._getElementValue(xmlChannel, 'pubDate'),
	    lastBuildDate: this._getElementValue(xmlChannel, 'lastBuildDate')
	};
	this.items = new Array();
	var xmlItems = this._getElementsByTagNameNS(node, "*", 'item');
	for (var n=0; n<xmlItems.length; n++) {
	    var xmlItem = xmlItems[n];
	    var subjects = [];
	    this._getElementsValue(xmlItem, 'subject').each(function(ss){
		    ss.split(" ").each(function(s) {
			    subjects.push(s);
			});
		});
	    if (subjects.length == 0) {
		subjects = this._getElementValue(xmlItem, 'category').split(" ");
	    }
	    if (!subjects[0]) {
		subjects = [];
	    }
	    var date = this._getElementValue(xmlItem, 'date');
	    if (!date) {
		date = this._getElementValue(xmlItem, 'pubDate');
	    }

	    var creator = this._getElementValue(xmlItem, 'creator');
	    if (!creator) {
		creator = this._getElementValue(xmlItem, 'credit');
	    }

	    var item = {
		title: this._getElementValue(xmlItem, 'title'),
		link: this._getElementValue(xmlItem, 'link'),
		description: this._getElementValue(xmlItem, 'description'),
		creator: creator,
		category: this._getElementValue(xmlItem, 'category'),
		comments: this._getElementValue(xmlItem, 'comments'),
		guid: this._getElementValue(xmlItem, 'guid'),
		thumbnail: this._getAttributeValue(xmlItem, 'thumbnail', 'url'),
		thumbnail_w: this._getAttributeValue(xmlItem, 'thumbnail', 'width'),
		thumbnail_h: this._getAttributeValue(xmlItem, 'thumbnail', 'height'),
		date: date,
		subjects: subjects
	    };
	    this.items.push(item);
	}
	return true;
    },
    onFailure : function(t) {
    },
    _getElementValue : function(node, elementName) {
	try {
	    var nl = node.getElementsByTagName(elementName);
	    var value = nl.length? nl.item(0).firstChild.data:
	    this._getElementsByTagNameNS(node, "*", elementName)[0].firstChild.data;
	}
	catch(e) {
	    var value = '';
	}
	return value;
    },
    _getElementsValue : function(node, elementName) {
	try {
	    var nl = $A(node.getElementsByTagName(elementName));
	    var values = [];
	    if (nl.length == 0) {
		nl = this._getElementsByTagNameNS(node, "*", elementName);
	    }
	    nl.each(function(n) {
		    values.push(n.firstChild.data);
		});
	}
	catch(e) {
	    var value = [];
	}
	return values;
    },
    _getAttributeValue : function(node, elementName, attributeName) {
	try {
	    var nl = node.getElementsByTagName(elementName);
	    var value = nl.length? nl.item(0).getAttribute(attributeName):
	    this._getElementsByTagNameNS(node, "*", elementName)[0].getAttribute(attributeName);
	}
	catch(e) {
	    var value = '';
	}
	return value;
    },
    _getElementsByTagNameNS : function(node, ns, elm) {
	if (document.all && document.attachEvent){
	    var nodes = node.getElementsByTagName("*");
	    return $A(nodes).inject([], function(elements, child) {
		    if (child.nodeName.split(":").pop() == elm)
			elements.push(child);
		    return elements;
		});
	}
	else if (document.implementation.createDocument) {
	    return $A(node.getElementsByTagNameNS(ns,elm));
	}
    }
};
