// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// Mod: bmcgregor
// -------------------------------------------------------------------

//Relative URL syntax:
var lastrssbridgeurl="bridge.php"

//Absolute URL syntax. Uncomment below line if you wish to use an absolute reference:
//var lastrssbridgeurl="http://"+window.location.hostname+"/lastrss/bridge.php"

////////////No need to edit beyond here//////////////

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
// rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, logicswitch) {
	this.RSS_id=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.delay=delay; //Delay between msg change, in miliseconds.
	this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : "";
	this.title=[]; this.link=[]; this.description=[]; this.pubdate=[]; //Arrays to hold each component of an RSS item
	if (!(this.ajaxobj=createAjaxObj())) {
		document.write('<div id="'+divId+'" class="'+divClass+'" >Failed to get XMLHttpRequest object</div>');
		return false;
	}
	document.write('<div id="'+divId+'" class="'+divClass+'" >Initializing ticker...</div>');
	this.getAjaxcontent();
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "bridge.php" with the supplied parameters
// -------------------------------------------------------------------

rssticker_ajax.prototype.getAjaxcontent=function() {
	if (this.ajaxobj) {
		var that=this;
		var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime();
		this.ajaxobj.onreadystatechange=function(){ that.initialize() };
		this.ajaxobj.open('GET', lastrssbridgeurl+"?"+parameters, true);
		this.ajaxobj.send(null);
	}
};

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssticker_ajax.prototype.initialize=function() { 
	if (this.ajaxobj.readyState == 4); { //if request of file completed
		try {
			if (this.ajaxobj.status==200) { //if request was successful
				var xmldata;
				if (xmldata=this.ajaxobj.responseXML) {  //if fetch of xml data is successful
					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 that=this;
					this.feeditems=xmldata.getElementsByTagName("item");
					//Cycle through RSS XML object and store each peice of an item inside a corresponding array
					for (var i=0; i<this.feeditems.length; i++) {
						this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
						this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;
						this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue;
						this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
					}
					document.getElementById(this.tickerid).onmouseover=function(){that.mouseoverBol=true};
					document.getElementById(this.tickerid).onmouseout=function(){that.mouseoverBol=false};
					this.pointer = Math.floor(Math.random()*this.feeditems.length);
					this.rotatemsg();
				}
			}
		} catch(e) {}
	}
};

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

function rsstick_onclick(href) {  // headline onclick handler for floatbox
	var options = 'sameBox:true width:94% height:90% caption:' +
		'`<a href="" onclick="fb.instances[fb.ownerInstance(this)].goBack(); return false;"><b>Return to headlines...</b></a>`';
	parent.fb.start(href, options);
	return false;
};
rssticker_ajax.prototype.rotatemsg=function(){
	var that = this;
	if (this.mouseoverBol) { //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function(){that.rotatemsg()}, 100);
	} else { //else, construct item, show and rotate it!
		var tickerDiv=document.getElementById(this.tickerid);
		var linktitle='<div class="rsstitle"><a href="'+this.link[this.pointer]+'" onclick="return rsstick_onclick(this.href);">'+this.title[this.pointer]+'</a></div>';
		var description='<div class="rssdescription">'+this.description[this.pointer]+'</div>';
		var feeddate='<div class="rssdate">'+this.pubdate[this.pointer]+'</div>';
		if (this.logicswitch.indexOf("description")==-1) description="";
		if (this.logicswitch.indexOf("date")==-1) feeddate="";
		var tickercontent=linktitle+feeddate+description; //STRING FOR FEED CONTENTS 
		tickerDiv.innerHTML=tickercontent;
		this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0;
		setTimeout(function(){that.rotatemsg()}, this.delay); //update content this often
	}
};
