var rssDefaults = {
	proxy : '/includes/asp/rss/rssProxy.asp',
	templates : {
		feed : 'ul',
		itemElement:'li',
		item: '<a href="javascript:void(0)" >#{title}</a><div id="rssdetails#{feedid}_#{id}" style="display:none"><h3>#{title}</h3>#{description}</li>',
		link : '<a href="#{link}" target="_blank">More...</a>',
		linkMoreNews : 'More...'
	}	
}

var rssDisplayer=Class.create({
	initialize: function(url,options){
		options=options||{};
		this.rssList={}
		this.counter=0;
		this.displayed=false;
		this.pageLoaded=false;
	},	
	toggle : function(id){
		if(this.displayed){
			$(this.displayed).toggle();			
			if(this.displayed==id){this.displayed=false;return false;}
			this.displayed=false;
		}
		try{$(id).toggle();}catch(ex){}
		this.displayed=id;
	},
	getFeed : function(url, options){
		new Ajax.Request(
			rssDefaults.proxy,
			{
				asynchronous:true,method:'get',parameters : 'url='+escape(url),
				onComplete: function(info){
					this.parseFeed(info,url,options)
				}.bind(this)
			}
		);
	},
	returnContent : function(node){
		var o = {};         
		for (var x = 0; x < node.childNodes.length; x++) {			
			if(node.childNodes[x].nodeType==1){			
				o[node.childNodes[x].nodeName] = node.childNodes[x].firstChild?node.childNodes[x].firstChild.nodeValue:'';
			}
		}
		return o;
	},

	parseFeed : function(info,url, options){	
		//1-channel information		
		try{var ch = this.returnContent( info.responseXML.getElementsByTagName('channel')[0]);}
		catch(ex){ch={}}
		this.rssList[url].templates = Object.extend(rssDefaults.templates, options.templates)
		this.rssList[url].channel = ch;
		this.rssList[url].maxid=0;
		var rel = new Element('link',{rel:'alternate',type:'application/rss+xml', title:ch.title||'News from'+document.title, href:url})
		$$('head')[0].insert(rel);
		
		//2-items
		var items=[];
		$A(info.responseXML.getElementsByTagName('item')).each(
			function(node){
				try{var o = this.returnContent(node);}catch(ex){}				
				this.rssList[url].maxid++;
				o.id=this.rssList[url].maxid;
				items.push(o);
			}.bind(this)
		);
		this.counter++;
		this.rssList[url].id = this.counter;
		this.rssList[url].items = items;
		if(options.target){this.displayFeed(url,options.target,options)}
	},
	/*
		options : maxDisplay
	*/
	displayFeed : function(url,target,options){
		options=options||{}		
		var feed = this.rssList[url];
		var root = new Element(feed.templates.feed);
		var currentRss = this;
		for(var i = 0;i<Math.min(feed.items.length, (options.forceDisplay?feed.items.length:feed.options.maxDisplay||feed.items.length) );i++){
			root.insert(
				new Element(rssDefaults.templates.itemElement,{id:feed.id+'_'+feed.items[i].id}).update(new Template(feed.templates.item).evaluate(Object.extend(feed.items[i], {feedid:feed.id}))).observe(
					'click',function(){currentRss.toggle('rssdetails'+this.id)}
				)
			)
		}
		try{
			if(!options.forceDisplay && (feed.items.length>feed.options.maxDisplay)){
				root.insert(
					new Element(rssDefaults.templates.itemElement).insert(
						new Element('a',{id:'more'+feed.id,href:'javascript:void(0)'}).update(feed.templates.linkMoreNews).observe('click',
							function(){
								this.displayFeed(url,target,Object.extend(options, {forceDisplay:true}))
							}.bind(this)
						)
				))
			}
		}catch(ex){}
		
		Event.observe(document,'keypress',function(e){if(e.keyCode==Event.KEY_ESC){this.toggle(this.displayed)}}.bind(this))
		this.displayed=false;
		$(target).update()
		$(target).insert(root);
	},
	/*
	options:
		target : target div
		maxDisplay : max RSS to be displayed
		moreLink : if nb items > maxDisplay, a link will be proposed to display more items
	*/
	add : function(feedurl, feedoptions){
		var o = {url : feedurl, options : feedoptions, cache:[]}		
		this.rssList[o.url]=o;
		if(this.pageLoaded){ this.getFeed(feedurl, feedoptions); }		
	},
	initLoadedFeeds : function(){
		for(feed in this.rssList){
			this.getFeed(feed, this.rssList[feed].options)
		}
		this.pageLoaded=true;
	}	
});
var rssDisplay = new rssDisplayer()
Event.observe(window,'load',rssDisplay.initLoadedFeeds.bindAsEventListener(rssDisplay));
