/* jFeed : jQuery feed parser plugin
 * Copyright (C) 2007 Jean-François Hovinne - http://www.hovinne.com/
 * Dual licensed under the MIT (MIT-license.txt)
 * and GPL (GPL-license.txt) licenses.
 */

jQuery.getFeed = function(options) {

    options = jQuery.extend({
    
        url: null,
        data: null,
        success: null
        
    }, options);

    if(options.url) {

        $.ajax({
            type: 'GET',
            url: options.url,
            data: options.data,
            dataType: 'text',
            success: function(data)
            {
				var xml;
				if(	(data != null)
					&& (typeof(data) == 'string')
					&& (data.length > 0)
				)
				{
					if (window.DOMParser)
					{
						var parser = new DOMParser();
						xml = parser.parseFromString(data, "text/xml");
					}
					else // Internet Explorer
					{
						xml = new ActiveXObject("Microsoft.XMLDOM");
						xml.async="false";
						xml.loadXML(data);
					}
				}
				else
				{
					if(jQuery.isFunction(options.fail))
					{
						options.fail("Empty data.");
					}
				}
				if(xml != null)
                {
					var feed = new JFeed(xml);
					if(jQuery.isFunction(options.success))
					{
						options.success(feed);
					}
				}
            },
            error: function(s, xhr, status, errMsg)
            {
				if(jQuery.isFunction(options.fail))
				{
					options.fail(errMsg);
				}
            }
        });
    }
};

function JFeed(xml) {
    if(xml) this.parse(xml);
};

JFeed.prototype = {

    type: '',
    version: '',
    title: '',
    link: '',
    description: '',
    parse: function(xml) {
        
        if(jQuery('channel', xml).length == 1) {
        
            this.type = 'rss';
            var feedClass = new JRss(xml);

        } else if(jQuery('feed', xml).length == 1) {
        
            this.type = 'atom';
            var feedClass = new JAtom(xml);
        }
        
        if(feedClass) jQuery.extend(this, feedClass);
    }
};


