/**
 * =============================================================================
 * Display XML feed panel. (RSS0.91/RSS2.0/ATOM0.3)
 *
 * @author Ryuji Komoriya(U-FACTORY) <ryuji@u-factory.co.jp>
 * @version 0.20
 * @see prototype.jp library http://prototype.conio.net/
 * @see JKL.Hina library http://www.kawa.net/works/js/jkl/hina.html
 * @see XML.ObjTree library http://www.kawa.net/works/js/xml/objtree.html
 * =============================================================================
 * v0.10 2006.09.13 New
 * v0.20 2006.09.14 impliment RSS0.91/RSS2.0
 * =============================================================================
*/
if (typeof(UFL) == 'undefined') var UFL = new Object();

function IsArray(array) {
    return (typeof(array) == "object" &&
    array != null &&
    array.constructor.toString().indexOf(" Array(") >= 0);
}
// 
//UFL.FeedPanel.VERSION = "0.10";

/**
 * UFL.FeedPanel class constructor
 * @access public
 * @param string url XML feeds URI
 * @param hash options 'xml_url'      : XML feed URI
 *                     'template_id'  : template DOM element id. (required)
 *                     'target_id'    : target DOM element id. (required)
 *                     'feed_type'    : XML feed format.(atom03|rss20) (required)
 *                     'max_feed_num' : number of max display feed. default value is 5. (option)
*/
UFL.FeedPanel = function(options) {
	this.template_id = options['template_id'];
	this.target_id   = options['target_id'];
	this.feed_type   = options['feed_type'];
	if (typeof(options['max_feed_num']) == 'undefined') {
		this.max_feed_num = 5;
	} else {
		this.max_feed_num = options['max_feed_num'];
	}
	
	//
	var scope = this;
	
	this.xmltree = new XML.ObjTree();
	this.xmltree.force_array = ["entry"];
	this.xmltree.parseHTTP(
		options['xml_url'],
		{},
		function(tree) {
			switch (scope.feed_type) {
			case 'atom03':
				var p = new UFL.ParseATOM03(tree);
				break;
			case 'rss091':
			case 'rss20':
				var p = new UFL.ParseRSS20(tree);
				break;
			}
			
			if (typeof(p) == 'object') {
				scope.feeds = {};
				scope.feeds.title = p.get_title();
				scope.feeds.url   = p.get_url();
				scope.feeds.rows  = p.get_entries(scope.max_feed_num);
				
				scope.display();
			}
		}
	);
	
	return this;
};

/**
 * display
 * @access public
*/
UFL.FeedPanel.prototype.display = function() {
	var tmpl = new JKL.Hina(this.template_id);
	tmpl.expand(this.feeds, this.target_id);
};

/**
 * UFL.ParseATOM03 class
 * @author Ryuji Komoriya(U-FACTORY) <ryuji@u-factory.co.jp>
*/
UFL.ParseATOM03 = Class.create();
UFL.ParseATOM03.prototype = {
	/**
	 * constructor
	 * @access public
	*/
	initialize : function(tree) {
		this.tree = tree;
	},
	
	/**
	 * Get feed title.
	 * @access public
	*/
	get_title : function() {
		return this.tree.feed.title;
	},
	
	/**
	 * Get feed URL.
	 * @access public
	*/
	get_url : function() {
		return this.tree.feed.link['-href'];
	},
	
	/**
	 * Get entries.
	 * @access public
	*/
	get_entries : function(max_num) {
		var rows = new Array();
		for (var i=0; i<this.tree.feed.entry.length; i++) {
			var row = {};
            issued = this.tree.feed.entry[i].issued;
			row.title   = this.tree.feed.entry[i].title;
			row.url     = this.tree.feed.entry[i].link['-href'];
			row.summary = this.tree.feed.entry[i].summary;
            row.issued  = issued.substr(0, 10);
			rows.push(row);
			if (rows.length >= max_num) {
				break;
			}
		}
		return rows;
	}
};

/**
 * UFL.ParseRSS20 class
 * @author Ryuji Komoriya(U-FACTORY) <ryuji@u-factory.co.jp>
*/
UFL.ParseRSS20 = Class.create();
UFL.ParseRSS20.prototype = {
	/**
	 * constructor
	 * @access public
	*/
	initialize : function(tree) {
		this.tree = tree;
	},
	
	/**
	 * Get feed title.
	 * @access public
	*/
	get_title : function() {
        return this.tree.rss.channel.title;
	},
	
	/**
	 * Get feed URL.
	 * @access public
	*/
	get_url : function() {
		return this.tree.rss.channel.link;
	},
	
	/**
	 * Get entries.
	 * @access public
	*/
	get_entries : function(max_num) {
		var rows = new Array();
		
		/*
		if (!IsArray(this.tree.rss.channel.item.length)) {
		    this.tree.rss.channel.item = new Array(this.tree.rss.channel.item);
		}
		*/
		
		for (var i=0; i<this.tree.rss.channel.item.length; i++) {
			var row = {};
			row.title   = this.tree.rss.channel.item[i].title;
			row.url     = this.tree.rss.channel.item[i].link;
			row.summary = this.tree.rss.channel.item[i].description;
			
			var pub_date = new Date(this.tree.rss.channel.item[i].pubDate);
			var yy = pub_date.getYear();
            var mm = pub_date.getMonth() + 1;      
            var dd = pub_date.getDate();
			if (yy < 2000) { yy += 1900; }
			if (mm < 10) { mm = "0" + mm; }
			if (dd < 10) { dd = "0" + dd; }
			var issued = yy + "-" + mm + "-" + dd;
            row.issued  = issued;
            
			rows.push(row);
			if (rows.length >= max_num) {
				break;
			}
		}
		return rows;
	}
};
