// -------------------------------------------------------------------
// Ajax XML Ticker (txt file source)
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

////////////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 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.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.skipEvent=0;
this.skipPosition = 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
this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'" class="tickerInit"><span class="initTickerMsg">Initialising news...</span></div></div>')
this.getXMLfile()
}

// -------------------------------------------------------------------
// getXMLfile()- Use Ajax to fetch xml file (txt)
// -------------------------------------------------------------------

ajax_ticker.prototype.getXMLfile=function(){
if (this.ajaxobj){
var instanceOfTicker=this
var url=this.xmlfile+"?bustcache="+new Date().getTime()
this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
this.ajaxobj.open('GET', url, true)
this.ajaxobj.send(null)
}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of xml file and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

ajax_ticker.prototype.initialize=function(){ 
if (this.ajaxobj.readyState == 4){ //if request of file completed
if (this.ajaxobj.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful
this.contentdiv=document.getElementById(this.tickerid).firstChild //div of inner content that holds the messages
var xmldata=this.ajaxobj.responseText
this.contentdiv.style.visible="hidden"
this.contentdiv.innerHTML=xmldata
if (this.contentdiv.getElementsByTagName("div").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
for (var i=0; i<this.contentdiv.getElementsByTagName("div").length; i++){
if (this.contentdiv.getElementsByTagName("div")[i].className=="message")
this.messages[this.messages.length]=this.contentdiv.getElementsByTagName("div")[i].innerHTML
}
this.contentdiv.innerHTML="";
this.contentdiv.style.visible="visible";
this.fadetransition("reset");
this.rotatemsg()
}
}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through ticker messages and displays them
// -------------------------------------------------------------------

ajax_ticker.prototype.rotatemsg=function(){
var instanceOfTicker=this;
//this.fadetransition("reset"); //FADE EFFECT- RESET OPACITY
if (this.mouseoverBol==1 && this.skipEvent == 0) {//if mouse is currently over ticker, do nothing (pause it)
  this.timeout1 = setTimeout(function(){instanceOfTicker.rotatemsg()}, 100);
  this.fadetransition("full");
}
else{ //else, construct item, show and rotate it!
   this.fadetransition("reset"); //FADE EFFECT- RESET OPACITY
   clearInterval(this.fadetimer1);
   this.contentdiv.style.visible="hidden";
   this.fadetransition("none"); 

   if ( this.skipEvent != 0 ) {
      if ( this.skipPosition == this.pointer) {
           if ( this.skipEvent < 0 ) {
              if ( this.pointer == 0 ) { this.pointer = this.messages.length };
              this.pointer = this.pointer+this.skipEvent;
              if ( this.pointer < 0 ) { this.pointer = 0 };
           }
      }
   }

   this.contentdiv.innerHTML= '<a name="'+this.tickerid+'"><div id="tickerOuterContainer"><div id="tickerContainer1" class="tickerContainer tickerHidden">'+this.messages[this.pointer]+'</div><br/><div class="tickerNav" id="tickerNav1"></div></div>';
   if ( document.getElementById('tickerDebug') ) {
      document.getElementById('tickerDebug').innerHTML = 'Skip Event?:'+this.skipEvent+'<br/>Pointer:'+this.pointer+'<br/>Skip Position:'+this.skipPosition;                                
   } 
   
   var tickerContainer = document.getElementById('tickerNav1');
   if ( this.pointer<this.messages.length-1 ) {
      var anchor1 = document.createElement("a"); 
      anchor1.setAttribute("href", "#"+this.tickerid);
      anchor1.className='tickerNext';
      anchor1.onclick = function() {instanceOfTicker.nextMessage()};
      var text1 = document.createTextNode("Next");
      anchor1.appendChild(text1);
      tickerContainer.appendChild(anchor1);
   }
   if ( this.pointer > 0 ) {
      var anchor1 = document.createElement("a");
      anchor1.setAttribute("href", "#"+this.tickerid);
      anchor1.className='tickerBack';
      anchor1.onclick = function() {instanceOfTicker.prevMessage()};
      var text1 = document.createTextNode("Back");
      anchor1.appendChild(text1);
      tickerContainer.appendChild(anchor1);
   }
   if ( this.skipEvent )  {
      this.fadetransition("full");
   }
   this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 200) //FADE EFFECT- PLAY IT
    
   this.pointer=(this.pointer<this.messages.length-1)? this.pointer+1 : 0;
   this.skipEvent = 0;
   this.skipPosition = 0;
   instanceOfTicker.skipEvent = 0;
   instanceOfTicker.skipPosition = 0;
   this.timeout1=setTimeout(function(){instanceOfTicker.rotatemsg();instanceOfTicker.fadetransition("none");}, this.delay) //update container periodically
 }
}

ajax_ticker.prototype.nextMessage=function(){
   clearTimeout(this.timeout1);
   this.skipEvent = 1;
   this.skipPosition = this.pointer;
   return this.rotatemsg();
}

ajax_ticker.prototype.prevMessage=function(){
    clearTimeout(this.timeout1);
    this.skipEvent = -2;
    this.skipPosition = this.pointer;
    return this.rotatemsg();
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

ajax_ticker.prototype.fadetransition=function(fadetype, timerid){
var contentdiv=this.contentdiv
if (fadetype=="reset") {
  this.opacitysetting=0.1;
}
if (fadetype=="full") {
  this.opacitysetting=1;
}
if (fadetype=="none") {
  this.opacitysetting=0.0;
  contentdiv.style.visible='hidden';
}

if (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 ( this.opacitysetting > 0 && ( fadetype=="up" || fadetype == "full" ) ) {
   document.getElementById('tickerContainer1').className='tickerContainer';
   contentdiv.style.visible='visible';
}

if ( fadetype=='none' ) {
    clearInterval(this[timerid]);
}

if (fadetype=="up" && this.opacitysetting>=1) {
   clearInterval(this[timerid]);
}

}
