// JavaScript Document
// Modified from O'Reilly's exemple code
// HTML full options :
// <a href="">
// <img />
// <span>
// </span>
// </a>

var rssajax2 = {
	init:function(url,nb,S){
		rssajax2.showchannel = false;
		rssajax2.showimage = true;
		rssajax2.getRSS(url);
		rssajax2.effect=new Array();
		rssajax2.nbl = 0;	
		rssajax2.nb = nb;
		rssajax2.size = S;
	},
	putMO:function(){
		$('prdcts').getElements('a').each(function(lk){	
			var currentnbl = rssajax2.nbl;	
			var x = lk.getElement('span');
			rssajax2.effect[currentnbl] = new Fx.Morph(x, {duration: 50, transition: Fx.Transitions.Sine.easeOut});
			lk.addEvents({
				'mouseover' : function(){
					rssajax2.effect[currentnbl].start({'top': 90});
				}
			});
			lk.addEvents({
				'mouseout' : function(){
					rssajax2.effect[currentnbl].start({'top': 120});
				}
			});	
			rssajax2.nbl += 1;
		});
		
	},
	//object containing the RSS 2.0 item
	RSS2Item: function(itemxml){
		//required
		this.title;
		this.link;
		this.description;

		//optional objects
		this.image;

		var properties = new Array("title", "link","description");
		var tmpElement = null;
		for (var i=0; i<properties.length; i++){
			tmpElement = itemxml.getElementsByTagName(properties[i])[0];
			if (tmpElement != null)
				eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
		// var tmpImage = itemxml.getElementsByTagName("image")[0];
		// var tmpUrl = tmpImage.getElementsByTagName("url")[0];
		// this.image = tmpUrl.childNodes[0].nodeValue;
	},

	//object containing RSS image tag info
	RSS2Image: function(imgElement){
		if (imgElement == null){
			this.url = null;
			this.link = null;
		} else {
			imgAttribs = new Array("url","link");
			for (var i=0; i<imgAttribs.length; i++)
				if (imgElement.getElementsByTagName(imgAttribs[i]) != null)
					eval("this."+imgAttribs[i]+"=imgElement.getElementsByTagName(imgAttribs[i]).childNodes[0].nodeValue");
		}
	},

	//object containing the parsed RSS 2.0 channel
	RSS2Channel: function(rssxml){
		//required
		this.title;
		this.link;
		this.description;

		//array of RSS2Item objects
		this.items = new Array();

		var chanElement = rssxml.getElementsByTagName("channel")[0];
		var itemElements = rssxml.getElementsByTagName("item");

		for (var i=0; i<itemElements.length; i++){
			Item = new rssajax2.RSS2Item(itemElements[i]);
			this.items.push(Item);
			//chanElement.removeChild(itemElements[i]);
		}

		var properties = new Array("title", "link", "description");
		var tmpElement = null;
		for (var i=0; i<properties.length; i++){
			tmpElement = chanElement.getElementsByTagName(properties[i])[0];
			if (tmpElement!= null)
				eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
	},

	//PROCESSES

	//uses xmlhttpreq to get the raw rss xml
	getRSS: function(url){
		//call the right constructor for the browser being used
		if (window.ActiveXObject)
			 rssajax2.xhr = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest)
			rssajax2.xhr = new XMLHttpRequest();
		else
			alert("not supported");

		//prepare the xmlhttprequest object
		rssajax2.xhr.open("GET",url,true);
		rssajax2.xhr.setRequestHeader("Cache-Control", "no-cache");
		rssajax2.xhr.setRequestHeader("Pragma", "no-cache");
		rssajax2.xhr.onreadystatechange = function() {
			if (rssajax2.xhr.readyState == 4){
				if (rssajax2.xhr.status == 200){
					if (rssajax2.xhr.responseText != null)
						rssajax2.processRSS(rssajax2.xhr.responseXML);
					else {
						alert("Failed to receive RSS file from the server - file not found.");
						return false;
					}
				} else
					alert("Error code " + rssajax2.xhr.status + " received: " + rssajax2.xhr.statusText);
			}
		}

		//send the request
		rssajax2.xhr.send(null);
	},

	//processes the received rss xml
	processRSS: function(rssxml){
		RSS = new rssajax2.RSS2Channel(rssxml);
		rssajax2.showRSS(RSS);
	},

	//shows the RSS content in the browser
	showRSS: function(RSS){

		//populate the items
		document.getElementById("prdcts").innerHTML = "";
		for (var i=0; i<RSS.items.length && i<rssajax2.nb; i++){
			item_html = "";
			item_html += (RSS.items[i].link == null) ? "" : "<a href='" + RSS.items[i].link + "'>";
			item_html += "<span>" + RSS.items[i].title + "</span>";
			if(rssajax2.size=="S"){
				RSS.items[i].description = RSS.items[i].description.replace("/contentimages/","/contentimages/S_" )
			}
			item_html += RSS.items[i].description;
			document.getElementById("prdcts").innerHTML += item_html;
		}
		rssajax2.putMO();
		return true;
	}
}
