// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

//Relative URL syntax:
var lastrssbridgeurl="lastrss/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_ajax2(RSS_id, cachetime, divId, divClass, delay, logicswitch, use_wait, pubdate_below){
this.pubdate_below = pubdate_below ? true : false;
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.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
this.pointer=0
this.pointer2=1
this.opacitysetting=0.2 //Opacity value when reset. Internal use.
this.title=[], this.link=[], this.description=[], this.pubdate=[] //Arrays to hold each component of an RSS item
this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'" >Loading ...</div>')
if (window.getComputedStyle) //detect if moz-opacity is defined in external CSS for specified class
this.mozopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity")==1)? 0 : 1
this.getAjaxcontent(use_wait)
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "bridge.php" with the supplied parameters
// -------------------------------------------------------------------

rssticker_ajax2.prototype.getAjaxcontent=function(use_wait){
if (this.ajaxobj){
	if (!use_wait) {
		use_wait = 0;
	} else {
		use_wait = 1;
	}
var instanceOfTicker=this
var parameters="url="+location.href+"&id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
parameters += "&use_wait=" + use_wait
this.ajaxobj.onreadystatechange=function(){instanceOfTicker.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_ajax2.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
document.getElementById(this.tickerid).innerHTML=""
return
}
var instanceOfTicker=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

var descriptions = this.feeditems[i].getElementsByTagName("description");

if (descriptions.length > 0) {
	this.description[i] = '';

	if (descriptions[0].firstChild) {
		this.description[i] = descriptions[0].firstChild.nodeValue.replace(/\r\n|\r|\n/g, "<br />");
	}
} else {
	this.description[i] = '';
}

//this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue.replace(/\r\n|\r|\n/g, "<br />");
this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue
}
document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
//document.getElementByID('go_up').onmouseover=function(){instanceOfTicker.direction=1}
//document.getElementByID('go_down').onmouseover=function(){instanceOfTicker.direction=-1}
this.rotatemsg()
}
}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

var t;
var direction = 1;
var tmp;
rssticker_ajax2.prototype.goup=function(){
direction=-1;
}
rssticker_ajax2.prototype.godown=function(){
direction=1;
}
rssticker_ajax2.prototype.rotatemsg=function(){
clearTimeout(t);
var instanceOfTicker=this
if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
else{ //else, construct item, show and rotate it!
var all='';

var tickerDiv=document.getElementById(this.tickerid)
/*
var linktitle='<div class="rsstitle">'+this.title[this.pointer]+'</div>'
var description='<div class="rssdescription"><i>'+this.description[this.pointer]+'</i></div><br>'
var feeddate='<div class="rssdate">'+this.pubdate[this.pointer]+'</div>'
var linktitle2='<div class="rsstitle">'+this.title[this.pointer2]+'</div>'
var description2='<div class="rssdescription"><i>'+this.description[this.pointer2]+'</i></div><br>'
var feeddate2='<div class="rssdate">'+this.pubdate[this.pointer2]+'</div>'
if (this.logicswitch.indexOf("description")==-1) description=""
if (this.logicswitch.indexOf("date")==-1) feeddate=""
var tickercontent=feeddate+linktitle+description+feeddate2+linktitle2+description2//STRING FOR FEED CONTENTS 
*/

if (!this.news) {
	for (var i=0; i<this.feeditems.length; i++){
		if (!this.pubdate_below) {
			all=all+'<div class="rssdate">Last Updated: '+this.pubdate[i]+'</div>'
		}

		all=all+'<div class="rsstitle">'+'<a class=rsstitle href="'+this.link[i]+'">'+this.title[i]+'</a>'+'</div>'
		all=all+'<div class="rssdescription"><i>'+this.description[i]+'</i></div>'

		if (this.pubdate_below) {
			all=all+'<div class="rssdate">Last Updated: '+this.pubdate[i]+'</div>'
		}

		all=all+"<br>";
	}

	this.news = all;
} else { 
	all = this.news;
}

var tickercontent = all;
var div = document.getElementById('ddbox');

//this.fadetransition("reset") //FADE EFFECT- RESET OPACITY

if (!this.distance) {
	this.distance = parseInt(tickerDiv.scrollHeight);
}

if (!this.once) {
	tickerDiv.innerHTML=tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent + tickercontent;
	this.once = true;
} else {
	if (tickerDiv.scrollTop >= (this.distance - 20)) {
		tickerDiv.innerHTML = tickerDiv.innerHTML  + tickerDiv.innerHTML;
		this.distance = parseInt(this.distance) *  2;
	}
}

//this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
//this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
//this.pointer2=(this.pointer2<this.feeditems.length-1)? this.pointer+1 : 0
//setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
//document.getElementById("ddbox").scrollBy(0,50); // horizontal and vertical scroll increments
//scrolldelay = setTimeout(function(){instanceOfTicker.rotatemsg()},100); // scrolls every 100 milliseconds
  //div.scrollTop = div.scrollTop - 1;
     div.scrollTop=div.scrollTop+(direction*1); //scroll 1 pixel up

     t = setTimeout(function(){instanceOfTicker.rotatemsg()}, 80);
     /*t = setTimeout(function(){instanceOfTicker.rotatemsg()}, 80);
     if (div.scrollTop == 0) {
	direction = 1;
	}
	//alert(div.scrollTop + " " + div.clientHeight + " " + div.scrollHeight);
	tmp = div.scrollHeight - div.clientHeight;
	//alert(tmp + ' ' + div.scrollTop);
     if (div.scrollTop == tmp){
	direction = -1;
	}*/
}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

rssticker_ajax2.prototype.fadetransition=function(fadetype, timerid){
var tickerDiv=document.getElementById(this.tickerid)
if (fadetype=="reset")
this.opacitysetting=0.2
if (tickerDiv.filters && tickerDiv.filters[0]){
if (typeof tickerDiv.filters[0].opacity=="number") //IE6+
tickerDiv.filters[0].opacity=this.opacitysetting*100
else //IE 5.5
tickerDiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
}
else if (typeof tickerDiv.style.MozOpacity!="undefined" && this.mozopacityisdefined){
tickerDiv.style.MozOpacity=this.opacitysetting
}
if (fadetype=="up")
this.opacitysetting+=0.2
if (fadetype=="up" && this.opacitysetting>=1)
clearInterval(this[timerid])
}
