
var feedSrc="userfiles/rssFeedWin/rssFeedWin.php"
var feedData=new Array() //global array to hold RSS feeds contents

var xmlhttp=false;
if(typeof ActiveXObject!='undefined') {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function formatrssmessage(divid, msgnumber, linktarget, datalen, logicswitch){
	var feedData=feedDataArr[divid][msgnumber]
	var linktitle='<div class="rssFeedTitle"><a href="'+unescape(feedData.link)+'" target="'+linktarget+'">'+unescape(feedData.title)+'</a></div>'
	var tmpDesc = unescape(feedData.description);
	if(tmpDesc.length > datalen) tmpDesc = tmpDesc.substring(0,datalen-3) + '...';
	var description='<br /><div class="rssFeedDesc">'+tmpDesc+'</div>'
	var feeddate='<div class="rssFeedDate">'+unescape(feedData.date)+'</div>'
	if (logicswitch.indexOf("description")!=-1 && logicswitch.indexOf("date")!=-1) //Logic switch- Show description and date
		return linktitle+feeddate+description
	else if (logicswitch.indexOf("description")!=-1) //Logic switch- Show just description
		return linktitle+description
	else if (logicswitch.indexOf("date")!=-1) //Logic switch- Show just date
		return linktitle+feeddate
	else
		return linktitle //Default- Just return hyperlinked RSS title
}

function rssFeedWin(RSS_id, divId, divClass, delay, linktarget, length, logicswitch){
	if(!xmlhttp) return;
	this.rssid=RSS_id;
	this.datalen=length;
	this.tickerid=divId 	//ID of ticker div to display information
	this.delay=delay 	//Delay between msg change, in miliseconds.
	this.linktarget=(typeof linktarget!="undefined")? linktarget : ""
	this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : ""
	this.mouseoverBol=0 	//Boolean to indicate whether mouse is currently over win (and pause it if it is)
	this.hiddendivpointer=1 //index of message array for hidden div

	document.write('\t<div id="'+divId+'" class="rssFeed'+divClass+'" style="position: relative;">');
	document.write('\t\t<div class="rssFeed'+divClass+'InnerDiv" style="position: absolute; width: 100%; overflow: hidden;" id="'+divId+'1">');
	document.write('\t\t\t<span style="position: absolute">Initializing 4Drywall News Feed...</span>');
	document.write('\t\t</div>');
	document.write('<div class="rssFeed'+divClass+'InnerDiv" style="position: absolute; width: 100%; visibility: hidden;" id="'+divId+'2"></div></div>')

	if (document.getElementById){ //perform basic DOM browser support
		var parameters="id="+encodeURIComponent(RSS_id)+"&divid="+divId+"&bustcache="+new Date().getTime()
		rssFeedWin.getRSScontentJS(this, feedSrc+"?"+parameters)
	}
}

// -------------------------------------------------------------------
// do_onjsload() - Loads all the divs, etc.
// -------------------------------------------------------------------

rssFeedWin.prototype.do_onjsload=function(){
	var rssInstance=this
		this.tickerdiv=document.getElementById(this.tickerid)
		this.visiblediv=document.getElementById(this.tickerid+"1")
		this.hiddendiv=document.getElementById(this.tickerid+"2")
		this.visibledivtop=parseInt(rssFeedWin.getCSSpadding(this.tickerdiv))

		//set width of inner DIV to outer DIV width minus padding (padding assumed to be top padding x 2)
		this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
		this.visiblediv.innerHTML=formatrssmessage(this.tickerid, 0, this.linktarget, this.datalen, this.logicswitch)
		this.hiddendiv.innerHTML=formatrssmessage(this.tickerid, 1, this.linktarget, this.datalen, this.logicswitch)
		//this.do_ondivsinitialized()
		this.initialize();
}

// -------------------------------------------------------------------
// do_ondivsinitialized()- Checks if two divs of scroller is each populated with RSS message yet
// -If not, continue to check until yes, or abort after certain tries.
// -------------------------------------------------------------------

//rssFeedWin.prototype.do_ondivsinitialized=function(){
	//var rssInstance=this
	//if (parseInt(this.visiblediv.offsetHeight)==0 || parseInt(this.hiddendiv.offsetHeight)==0)
		//setTimeout(function(){rssInstance.do_ondivsinitialized()}, 200)
	//else
		//this.initialize()
//}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------
rssFeedWin.prototype.initialize=function(){
	var rssInstance=this
	this.getinline(this.visiblediv, this.hiddendiv)
	this.hiddendiv.style.visibility="visible"
	//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
	this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
	this.tickerdiv.onmouseover=function(){rssInstance.mouseoverBol=1}
	this.tickerdiv.onmouseout=function(){rssInstance.mouseoverBol=0}
	if (window.attachEvent) //Clean up loose references in IE
		window.attachEvent("onunload", function(){rssInstance.tickerdiv.onmouseover=rssInstance.tickerdiv.onmouseout=null})
	setTimeout(function(){rssInstance.animateup()}, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------
rssFeedWin.prototype.animateup=function(){
	var rssInstance=this
	if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
		this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
		this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
		setTimeout(function(){rssInstance.animateup()}, 50)
	}
	else{
		this.getinline(this.hiddendiv, this.visiblediv)
		this.swapdivs()
		setTimeout(function(){rssInstance.rotatemessage()}, this.delay)
	}
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------
rssFeedWin.prototype.swapdivs=function(){
	var tempcontainer=this.visiblediv
	this.visiblediv=this.hiddendiv
	this.hiddendiv=tempcontainer
}

// -------------------------------------------------------------------
// swapdivs()- Physically swap the actual tops of the divs
// -------------------------------------------------------------------
rssFeedWin.prototype.getinline=function(div1, div2){
	div1.style.top=this.visibledivtop+"px"
	div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"
}

// -------------------------------------------------------------------
// rotatemessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------
rssFeedWin.prototype.rotatemessage=function(){
	var rssInstance=this
	if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
		setTimeout(function(){rssInstance.rotatemessage()}, 100)
	else{
		var i=this.hiddendivpointer
		var ceiling=feedDataArr[this.tickerid].length
		this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1
		this.hiddendiv.innerHTML=formatrssmessage(this.tickerid, this.hiddendivpointer, this.linktarget, this.datalen, this.logicswitch)
		this.animateup()
	}
}

// -------------------------------------------------------------------
// getRSScontentJS()- Fetch RSS feed data via AJAX
// -------------------------------------------------------------------
rssFeedWin.getRSScontentJS=function(theobj, scripturl){
	if(!xmlhttp) return;
	xmlhttp.open('GET',scripturl, true);
	xmlhttp.onreadystatechange = function(obj) {
		return function() {
		if(xmlhttp.readyState==4) {
			if(xmlhttp.status== 200) {
				eval(xmlhttp.responseText);
				obj.do_onjsload();
			} else {
				document.getElementById(this.tickerid).innerHTML=feedDataArr+"<br />"+this.rssid+" feed offline."
			}
		}
		};
	}(theobj)
	xmlhttp.send(null);
}

// -------------------------------------------------------------------
// getCSSpadding()- Get the CSS padding value, if any
// -------------------------------------------------------------------
rssFeedWin.getCSSpadding=function(tickerobj){ 
	if (tickerobj.currentStyle)
		return tickerobj.currentStyle["paddingTop"]
	else if (window.getComputedStyle) //if DOM2
		return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
	else
		return 0
}


