/**
 * @class HD.util.RSS
 * @description Retrieves RSS feeds and returns the content converted to JSON.
 */
HD.util.RSS = {
	/**
	 * Inject a value into into a JSON object (convert to array if more than one)
	 * @param {object} node The node to inject a property and value into
	 * @param {string} property The property to add
	 * @param {object} value The value to inject into the property
	 * @private
	 */
	_injectValue : function(node, property, value) {
		if (!node[property]) {
			node[property] = [];
		}
		node[property].push(value);
	},
	
	/**
	 * Recursively parse the XML tree into JSON
	 * @param {object} node The XML node to parse and convert to JSON
	 * @returns {object} The resulting JSON object
	 * @private
	 */
	_parseNode : function(node) {
		var jsonNode = {};
		for (var i = 0, l = node.childNodes.length; i < l; i++) {
			var nodeName = node.childNodes[i].nodeName.replace(/[:]/g,'__');
			if (nodeName != '#text' && node.childNodes[i].childNodes.length > 0) {
				this._injectValue(jsonNode, nodeName, this._parseNode(node.childNodes[i]));
			}
			else if (nodeName == '#text') {
				var text = null;
				if(typeof node.childNodes[i].textContent != "undefined") {
					text = node.childNodes[i].textContent || null;
				} else {
					text = node.childNodes[i].nodeValue || null;
				}
				return text;
			}
			else {
				this._injectValue(jsonNode, nodeName, node.childNodes[i].textContent || null);
			}
		}
		return jsonNode;
	},
	
	/**
	 * Parse an RSS/XML feed into JSON
	 * @param {object} data The data response to parse
	 * @param {function} callback The callback to pass the resulting JSON-converted feed
	 * @private
	 */
	_parseFeed : function(data, callback) {
		var jsonFeed = { channel: [] };
		
		var channels = data.responseXML.getElementsByTagName('channel');
		for (var i = 0, l = channels.length; i < l; i++) {
			this._injectValue(jsonFeed, 'channel', this._parseNode(channels[i]));
		}
		
		callback(jsonFeed);
	},
	
	/**
	 * Retrieves a RSS feed given a set of parameters passes the JSON conversion of it to the callback
	 * @param {string} feed The name of the feed to retrieve
	 * @param {string} params A list of parameters to include with the feed
	 * @param {function} callback The callback to pass the resulting JSON-converted feed
	 */
	getFeed : function(feed, params, callback) {
		var self = this;
		YAHOO.util.Connect.asyncRequest('GET', comSvcBaseUrl+'/hd/hd_rss/assets/proxy.jsp?feed='+feed+'&params='+params, {
			success: function(data) { 
				self._parseFeed(data, callback);
			},
			failure: function(data) { 
				callback(false);
			}
		}); 
	},
	
	/**
	 * Retrieves a RidePlanner RSS feed given a set of parameters passes the JSON conversion of it to the callback
	 * @param {string} params A list of parameters to include with the feed
	 * @param {function} callback The callback to pass the resulting JSON-converted feed
	 */
	getRidePlannerFeed : function(params, callback) {
		this.getFeed('rideplanner', params, callback);
	}
};

HD.register("hd_rss", HD.util.RSS, {version: "1.0", build: "1"});

