/**
* Some classes to read XML feeds.
*
* @author Marco Baumgartl <baumgartl@boerse-go.de>
*/

var BG = BG || {};

/**
* @constructor
*/
BG.FeedItem = function() {
    /**
    * @type {string}
    * @private
    */
    this.title  = null;

    /**
    * @type {string}
    * @private
    */
    this.content= null;

    /**
    * @type {string}
    * @private
    */
    this.link   = null;

    /**
    * @type {Date}
    * @private
    */
    this.date   = null;
};

BG.FeedItem.prototype = {
    /**
    * @param {string}
    */
    setTitle: function(value) {
        this.title = value;
    },

    /**
    * @return {string|null}
    */
    getTitle: function() {
        return this.title;
    },

    /**
    * @param {string}
    */
    setContent: function(value) {
        this.content = value;
    },

    /**
    * @return {string|null}
    */
    getContent: function() {
        return this.content;
    },

    /**
    * @param {string}
    */
    setLink: function(value) {
        this.link = value;
    },

    /**
    * @return {string|null}
    */
    getLink: function() {
        return this.link;
    },

    /**
    * @param {string}
    */
    setDate: function(value) {
        this.date = value;
    },


    getDate: function() {
        return this.date;
    }
};

/**
* Represents an item in a RSS feed.
*
* @constructor
* @extends BG.FeedItem
*/
BG.RssFeedItem = function() {};

BG.RssFeedItem.prototype = new BG.FeedItem();

/**
* Check date strings from RSS feeds ("Tue, 18 Jan 2011 11:27:27 +0100").
*/
BG.RssFeedItem.prototype.dateRegex = /[a-z]{3},\s\d{1,2}\s[a-z]{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\+\d{4}/i;

/**
* Parse date from an RSS feed into a Date object.
*
* @override
*/
BG.RssFeedItem.prototype.setDate = function(value) {

    var date = null;

    if (value !== null && this.dateRegex.test(value)) {
        date = new Date(value);
    }

    this.date = date;
};

/**
* @constructor
*/
BG.XmlParser = function() {};

/**
* Creates a DOM document object from a string.
*/
BG.XmlParser.prototype.getDocumentFromString = function(content) {

    var xmlDoc = null,
        parser = null;

    if (typeof DOMParser !== 'undefined') {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(content, 'text/xml');
    } else {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.loadXML(content);
    }

    return xmlDoc;
};

/**
* @constructor
*/
BG.FeedReader = function(parser) {};

/**
* @return {Array}
*/
BG.FeedReader.prototype.getItems = function() {
    return this.items;
};

/**
* RSS feed reader.
*
* @constructor
* @extends BG.FeedReader
*/
BG.RssFeedReader = function(parser) {
    /**
    * @type {BG.XmlParser}
    * @private
    */
    this.parser = parser;

    /**
    * @type {Array}
    * @private
    */
    this.items = [];
};

BG.RssFeedReader.prototype = new BG.FeedReader();

/**
* Parse XML document into BG.FeedItem objects.
*/
BG.RssFeedReader.prototype._parseDocument = function(xmlDoc) {
    var items = xmlDoc.getElementsByTagName('item'),
        title,
        content,
        link,
        date,
        item;

    for (var i=0, len=items.length; i<len; ++i) {

        item = new BG.RssFeedItem();

        title   = items[i].getElementsByTagName('title')[0].firstChild.nodeValue;
        link    = items[i].getElementsByTagName('link')[0].firstChild.nodeValue;
        date    = items[i].getElementsByTagName('pubDate')[0].firstChild.nodeValue;

        if (items[i].getElementsByTagName('content:encoded').length === 1) {
            content = items[i].getElementsByTagName('content:encoded')[0].firstChild.nodeValue;
        } else if (items[i].getElementsByTagName('encoded').length === 1) { // webkit
            content = items[i].getElementsByTagName('encoded')[0].firstChild.nodeValue;
        }

        item.setTitle(title);
        item.setContent(content);
        item.setLink(link);
        item.setDate(date);

        this.items.push(item);
    }
};

BG.RssFeedReader.prototype.read = function(content) {
/*    if (content instanceof Document) {
        this._parseDocument(content);
    }*/

    this.importString(content);
}

/**
* Import RSS feed items from a string.
*/
BG.RssFeedReader.prototype.importString = function(content) {
    var xmlDoc = this.parser.getDocumentFromString(content);
    this._parseDocument(xmlDoc);
};
