window.onload="loadTicker()";


var theTicker;

function loadTicker() {
    theTicker = new Ticker("newsxml.php")
}
//http://blueshadow.no-ip.com/sites/other/guild/ticker/

function Ticker(xmlUrl) {

    // START Animation Variables
    this.typeDelay = 30;
    this.scrollDelay = 7200;
    // END Animation Variables

    this.typeInterval = null;
    this.storyTimeout = null;

    /* Scrolling something for the future
    this.scrollTimeout = null;
    this.scrollInterval = null;
    */

    this.currentStory = 0;
    
    // This line makes references to the current object possible in out-of-scope functions like callbacks
    var me = this;

    // req used for XML request object in multiple functions
    var req = null;

    this.ticker_element = document.getElementById('ticker_holder');
    this.news = new Array();
    
    

    this.extractStories = function(xml,updateObject) {
	var news = new Array();
	// Pick stories from file
	var stories = xml.getElementsByTagName('item');
	if( stories.length > 0 ) {
	    
	    for( var i = 0; i < stories.length; i++ ) {
		
		// For each story, in order
		story = stories[i];
		
		// Grab the attributes
		var href = story.getElementsByTagName('link')[0].firstChild.nodeValue;
		var story_title = story.getElementsByTagName('title')[0].firstChild.nodeValue;
		news[news.length] = new TickerStory(story_title,href);
	    }

	    if(updateObject) {
		me.news = news;
	    }

	    return news;
	}

	return false;
	
    };
    
    this.loadXML = function(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
	    req = new XMLHttpRequest();
	    req.onreadystatechange = this.processXMLRequest;
	    req.open("GET", url, true);
	    req.send(null);
	    // branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    if (req) {
		req.onreadystatechange = this.processXMLRequest;
		req.open("GET", url, true);
		req.send();
	    }
	}
    };
    
    this.processXMLRequest = function() {
	var goodResponse = false;
	// only if req shows "complete"
	if (req.readyState == 4) {
	    switch(req.status) {
			
		case 200:
		    goodResponse = true;
		    break;
		case 304:
		    goodResponse = true;
		    break;
		default:
		    alert("Status: "+req.status);
		    break;
	    }
	    
	    if(goodResponse) {
		me.init(req.responseXML);
	    }
	}
    };

    // build(): inserts elements into the document
    this.build = function() {
	ticker = me.ticker_element;
	
	var ticker_visible = document.createElement('div');
	ticker_visible.setAttribute('id','ticker_visible');
	
	var ticker_label = document.createElement('div');
	ticker_label.setAttribute('id','ticker_label');
	ticker_label.appendChild(document.createTextNode('Latest News:'));
	
	var ticker_buttons = document.createElement('div');
	ticker_buttons.setAttribute('id','ticker_buttons');

	var prev_link = document.createElement('a');
	prev_link.setAttribute('id','prev_link');
	prev_link.setAttribute('href','#');
	prev_link.setAttribute('title','Previous Story');
	prev_link.onclick = function() {theTicker.goStory(-1);return false;};
	prev_link.onfocus = function() {this.blur();};
	prev_link.appendChild(document.createTextNode('prev'));

	var next_link = document.createElement('a');
	next_link.setAttribute('id','next_link');
	next_link.setAttribute('href','#');
	next_link.setAttribute('title','Next Story');
	next_link.onclick = function() {theTicker.goStory(1);return false;};
	next_link.onfocus = function() {this.blur();};
	next_link.appendChild(document.createTextNode('next'));

	ticker_buttons.appendChild(prev_link);
	ticker_buttons.appendChild(document.createTextNode(' | '));
	ticker_buttons.appendChild(next_link);
	
	var ticker_pad = document.createElement('div');
	ticker_pad.setAttribute('id','ticker_pad');
	var link_node = document.createElement('a');
	link_node.setAttribute('class','ticker_item');
	link_node.setAttribute('id','ticker_link');
	link_node.setAttribute('target','_blank');
	link_node.setAttribute('href','');
	link_node.appendChild(document.createTextNode(''));


	
	ticker_pad.appendChild(link_node);

	ticker_visible.appendChild(ticker_label);
	if(this.news.length > 2) {
	    ticker_visible.appendChild(ticker_buttons);
	}

	ticker_visible.appendChild(ticker_pad);	
	ticker.appendChild(ticker_visible);
	
	return true;
    }

    // start(): sets off the animation
    this.start = function() {
	me.showStory(0);
    }

    this.goStory = function(offset) {
	clearTimeout(this.storyTimeout);
	clearInterval(this.typeInterval);
	this.currentStory += offset;
	if(this.currentStory < 0) {
	    this.currentStory += this.news.length;
	}
	this.currentStory %= this.news.length;
	this.showStory(this.currentStory);
	}
	
	
	
    this.showStory = function(index) {
	if(me.news.length < index) {
	    return false;
	}

	var item = me.news[index]

	var ticker_element = document.getElementById('ticker_pad');
	var new_element = document.createElement('a');
	new_element.setAttribute('class','ticker_item');
	new_element.setAttribute('id','ticker_link');
	new_element.setAttribute('target','_blank');
	new_element.setAttribute('title',item.story_title);
	new_element.setAttribute('href',item.url);
	new_element.appendChild(document.createTextNode(''));

	ticker_element.removeChild(ticker_element.lastChild);
	
	ticker_element.appendChild(new_element);
	
	this.typeInterval = window.setInterval("theTicker.revealText('ticker_link','"+item.story_title.replace(/'/g,"\\'")+"')",this.typeDelay);

	return true;
    }

    this.revealText = function(id,string) {
	    var node = document.getElementById(id).firstChild;
		var layer =  document.getElementById(id);
	    var xChars = new Array('-','-');

	    var text_width;
	    var buttons_width;
	    var el_width;
	    var space;
	    var fader = new Fadomatic(layer, 6.5, 0);

			
			
	
		
		// Assign new string
		node.nodeValue = string;
		
		//fade in new string
		fader.fadeIn();
		
		//setTimeout('alert("foo")',2000);
		clearInterval(this.typeInterval);
		this.storyTimeout = window.setTimeout('theTicker.goStory(1)',this.scrollDelay)
	   
    }

    /** init(): Called when XML has been obtained
      *
      * Should check if XML is valid and if so, start the ticker
      */
    this.init = function(xml) {
	if(me.extractStories(xml,true)) {
	    me.build();
	    me.start();
	}
    }

    // This is the line of code which starts the whole process off. 
    // Only when the XML is safely loaded is the rest of the ticker put together.
    this.loadXML(xmlUrl);
}

function TickerStory(story_title, url) {
    this.story_title = story_title;
    this.url = url;
}

