/*
 ======================================================================
 RSS Feed reader for local feeds. 
 Author Rex Frobenius
 Created: Oct 2006
 
 Based on the code published for 
 RSS JavaScript Ticker object
 Author: George at JavaScriptKit.com/ DynamicDrive.com
 Created: Feb 5th, 2006. Updated: Feb 5th, 2006
 ======================================================================
*/

function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
		httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return httprequest
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rss_ticker(RSS_id, divId, divClass)
// -------------------------------------------------------------------

function rss_ticker(RSS_file, divId, divClass){
	this.RSS_file=RSS_file //encodeURI(RSS_id) //Array key indicating which RSS feed to display
	this.tickerid=divId //ID of ticker div to display information
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'">Retrieving headlines...</div>')
	this.getAjaxcontent()
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to local RSS file with the supplied parameters
// -------------------------------------------------------------------

rss_ticker.prototype.getAjaxcontent=function(){
	if (this.ajaxobj){
		var instanceOfTicker=this
		
		//call initialize funtion repeatedly while request is processed
		this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
		this.ajaxobj.open('GET', this.RSS_file, true)
		this.ajaxobj.send(null)
	}
}

rss_ticker.prototype.initialize=function(){ 
	if (this.ajaxobj.readyState == 4){ //if request of file completed
		if (this.ajaxobj.status==200){ //if request was successful
			var xmldata=this.ajaxobj.responseXML
			if(xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
				document.getElementById(this.tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"+this.ajaxobj.responseText
				return
			}
			var instanceOfTicker=this
			var channel = xmldata.getElementsByTagName("channel")[0]
			var title = channel.getElementsByTagName("title")
			this.titleText = title[0].firstChild.nodeValue
			var linkref = channel.getElementsByTagName("link")
			this.linkText = linkref[0].firstChild.nodeValue
			this.feeditems=xmldata.getElementsByTagName("item")
			this.pointer=0
			
			//Cycle through RSS XML object and store each peice of the item element as an attribute of the element
			for (var i=0; i<this.feeditems.length; i++){
				this.feeditems[i].setAttribute("ctitle", this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue)
				this.feeditems[i].setAttribute("clink", this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue)
				this.feeditems[i].setAttribute("cdescription", this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue)
			}
			this.displaymsg()
		}
	}
}

// -------------------------------------------------------------------
// displaymsg()- Display all headlines
// -------------------------------------------------------------------

rss_ticker.prototype.displaymsg=function(){
	var instanceOfTicker=this
	var tickercontent = ""
	var processString = ""
	var tickerDiv=document.getElementById(this.tickerid)
	tickercontent = '<p><a href="'+ this.linkText + '">' + this.titleText + '</a></p>'
	for (i=0; i<this.feeditems.length && i<7; i++){
		tickercontent += '<p><a href="'+this.feeditems[i].getAttribute("clink")+'">'
		tickercontent += this.feeditems[i].getAttribute("ctitle")+'</a></p>'
		processString = this.feeditems[i].getAttribute("cdescription")
		tickercontent+="<p>"+processString + "</p>"
		}
	tickerDiv.innerHTML=tickercontent

}

