var MWJ_ldD = []; // XML IMPORT

// -------------------------------------------------------------------
// Main Ajax Ticker Object function
// ajax_ticker(xmlfile, divId, divClass, delay, optionalfadeornot)
// -------------------------------------------------------------------

function ajax_ticker(xmlfile, divId, divClass, delay, fadeornot){
	this.xmlfile = xmlfile //Variable pointing to the local ticker xml file (txt)
	this.xmlfile += ( ( this.xmlfile.indexOf('?') + 1 ) ? '&' : '?' ) + ( new Date() ).getTime(); //prevent cache
	this.tickerid=divId //ID of ticker div to display information
	this.delay= delay //Delay between msg change, in miliseconds.
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer=0
	this.opacitystring=(typeof fadeornot!="undefined")? "width: 100%; filter:progid:DXImageTransform.Microsoft.alpha(opacity=100); -moz-opacity: 1" : ""
	if (this.opacitystring!="") this.delay+=500 //add 1/2 sec to account for fade effect, if enabled
	this.opacitysetting=0.2 //Opacity value when reset. Internal use.
	this.messages=[] //Arrays to hold each message of ticker
	document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'; background-color:#FFDEC1;">Initializing ticker...</div></div>')
	this.getXMLfile();
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of xml file and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------
ajax_ticker.prototype.initialize=function(){ 
	this.contentdiv=document.getElementById(this.tickerid).firstChild //div of inner content that holds the messages
	this.contentdiv.style.display="none"
	if (this.xmlData != null && this.xmlData.getElementsByTagName("tickeritem").length==0){ //if no messages were found
		this.contentdiv.innerHTML="<b>Error</b> fetching remote ticker file!"
		return
	}
	var instanceOfTicker=this
	document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
	document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
	if (window.attachEvent) //Clean up loose references in IE
	window.attachEvent("onunload", function(){instanceOfTicker.contentdiv=instanceOfTicker.ajaxobj=null})
	//Cycle through XML object and store each message inside array
	if (this.xmlData != null){
		for (var i=0; i<this.xmlData.getElementsByTagName("tickeritem").length; i++){
			this.messages[this.messages.length]=this.xmlData.getElementsByTagName("tickeritem")[i].firstChild.nodeValue;
		}
	}
	this.contentdiv.innerHTML=""
	this.contentdiv.style.display="block"
	this.rotatemsg()
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through ticker messages and displays them
// -------------------------------------------------------------------

ajax_ticker.prototype.rotatemsg=function(){
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!
this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
this.contentdiv.innerHTML=this.messages[this.pointer]
this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
this.pointer=(this.pointer<this.messages.length-1)? this.pointer+1 : 0
setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container periodically
}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

ajax_ticker.prototype.fadetransition=function(fadetype, timerid){
	var contentdiv=document.getElementById(this.tickerid).firstChild;
	//alert(typeof contentdiv.filters);
	if (fadetype=="reset")
		this.opacitysetting=0.2
	if (typeof contentdiv.filters != "unknown" && contentdiv.filters && contentdiv.filters[0]){
		if (typeof contentdiv.filters[0].opacity == "number") //IE6+
			contentdiv.filters[0].opacity=this.opacitysetting*100
		//else //IE 5.5
		//contentdiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
	}
	else if (typeof contentdiv.style.MozOpacity!="undefined" && this.opacitystring!=""){
		contentdiv.style.MozOpacity=this.opacitysetting
	}
	else
		this.opacitysetting=1
	
	if (fadetype=="up")
		this.opacitysetting+=0.1
	if (fadetype=="up" && this.opacitysetting>=1)
		clearInterval(this[timerid])
}


// XML IMPORT
ajax_ticker.prototype.getXMLfile=function(){
	//note: in XML importing event handlers, 'this' refers to window
	if( window.XMLHttpRequest ) {
		//alternate XMLHTTP request - Gecko, Safari 1.2+ and Opera 7.6+
		MWJ_ldD[MWJ_ldD.length] = new XMLHttpRequest();
		MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 && MWJ_ldD['+(MWJ_ldD.length-1)+'].status < 300 ) {tickerFastFacts.xmlData = MWJ_ldD['+(MWJ_ldD.length-1)+'].responseXML;  tickerFastFacts.initialize(); }' );
		MWJ_ldD[MWJ_ldD.length-1].open("GET", this.xmlfile, true);
		MWJ_ldD[MWJ_ldD.length-1].send(null);
		return true;
	}
	if( !navigator.__ice_version && window.ActiveXObject ) {
		//the Microsoft way - IE 5+/Win (ICE produces errors and fails to use try-catch correctly)
		try { //IE Mac has the property window.ActiveXObject but produces errors if you try and use it
			try { var tho = new ActiveXObject( 'Microsoft.XMLDOM' ); //newer
			} catch(e) { var tho = new ActiveXObject( 'Msxml2.XMLHTTP' ); } //older
			MWJ_ldD[MWJ_ldD.length] = tho;
			MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 ) { tickerFastFacts.xmlData = MWJ_ldD['+(MWJ_ldD.length-1)+']; tickerFastFacts.initialize(); }' );
			MWJ_ldD[MWJ_ldD.length-1].load(this.xmlfile);
			return true;
		} catch(e) {}
	}
	if( document.createElement && document.childNodes ) {
		//load the XML in an iframe
		/*var ifr = document.createElement('DIV');
		ifr.style.visibility = 'hidden'; ifr.style.position = 'absolute'; ifr.style.top = '0px'; ifr.style.left = '0px';
		//onload only fires in Opera so I use a timer for all
		if( !window.MWJ_XML_timer ) { window.MWJ_XML_timer = window.setInterval('MWJ_checkXMLLoad();',100); }
		ifr.innerHTML = '<iframe src="'+this.xmlfile+'" name="MWJ_XML_loader_'+MWJ_ldD.length+'" height="0" width="0"><\/iframe>';
		MWJ_ldD[MWJ_ldD.length] = oFunct+'MWJ_SPLIT'+(oDelay?oDelay:1)+'';
		document.body.appendChild(ifr);
		return true;
		*/
		return false;
	}
	return false;
}
