/*/ JavaScript Document
// listRSSFeed.js
// A basic script to list RSS headlines
// Relies on rssfetch.php to retrieve the RSS data
Based on the following script:
======================================================================
 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 List Object function
// rssList(RSS_id, cachetime, divId, divClass, delay, optionalswitch)
// -------------------------------------------------------------------

function rssList(RSS_id, cachetime, divId, divClass){
this.RSS_id= RSS_id //encodeURI(RSS_id) //Array key indicating which RSS feed to display
this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
this.tickerid=divId //ID of ticker div to display information

//this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'">Fetching headlines...</div>')
this.getAjaxcontent(this.tickerid)
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
// -------------------------------------------------------------------

rssList.prototype.getAjaxcontent=function(tickerid){
	var ajaxobj=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		ajaxobj=new XMLHttpRequest()
		if (ajaxobj.overrideMimeType)
			ajaxobj.overrideMimeType('text/xml')
		}
	else if (window.ActiveXObject){ // if IE
		try {
			ajaxobj=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
			ajaxobj=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	//var instanceOfTicker=this
	var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
	ajaxobj.onreadystatechange=function(){

	if (ajaxobj.readyState == 4){ //if request of file completed
		if (ajaxobj.status==200){ //if request was successful
			var xmldata=ajaxobj.responseXML
			//document.getElementById(this.tickerid).innerHTML=xmldata
			if(xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
				document.getElementById(tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"
				return
			}
			//var instanceOfTicker=this
			this.feeditems=xmldata.getElementsByTagName("item")
			
			//Cycle through RSS XML object and store each peice of the item element as an attribute of the element
			//var tickerDiv=document.getElementById(tickerid)
			var tickercontent = '<h2><a href="' + xmldata.getElementsByTagName("link")[0].firstChild.nodeValue + '">'+ xmldata.getElementsByTagName("title")[0].firstChild.nodeValue + "</a></h2>"
			for (var i=0; i<this.feeditems.length; i++){
			
			tickercontent += "<p><a href=" + this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue + ">" + this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue + "</a></p>"
			
			}
		document.getElementById(tickerid).innerHTML=tickercontent
		}
		else { document.getElementById(tickerid).innerHTML=encodeURIComponent(this.RSS_id) }
		}
	}
	
ajaxobj.open('GET', "rssfetch.php?"+parameters, true)
ajaxobj.send(null)

}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssList.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
document.getElementById(this.tickerid).innerHTML=xmldata
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 />"
return
}
var instanceOfTicker=this
this.feeditems=xmldata.getElementsByTagName("item")

//Cycle through RSS XML object and store each peice of the item element as an attribute of the element
var tickerDiv=document.getElementById(this.tickerid)
var tickercontent = '<h2><a href="' + xmldata.getElementsByTagName("link")[0].firstChild.nodeValue + '">'+ xmldata.getElementsByTagName("title")[0].firstChild.nodeValue + "</a></h2>"
for (var i=0; i<this.feeditems.length; i++){

tickercontent += "<p><a href=" + this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue + ">" + this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue + "</a></p>"

}
document.getElementById(this.tickerid).innerHTML=tickercontent
}
else { document.getElementById(this.tickerid).innerHTML=encodeURIComponent(this.RSS_id) }
}
}

