
if (!Function.prototype.apply) {
  // Based on code from http://www.youngpup.net/
  Function.prototype.apply = function(object, parameters) {
	var parameterStrings = new Array();
	if (!object)     object = window;
	if (!parameters) parameters = new Array();
	
	for (var i = 0; i < parameters.length; i++)
	  parameterStrings[i] = 'parameters[' + i + ']';
	
	object.__apply__ = this;
	var result = eval('object.__apply__(' + 
	  parameterStrings.join(', ') + ')');
	object.__apply__ = null;
	
	return result;
  }
}
var Class = {
  create: function() {
	return function() { 
	  this.initialize.apply(this, arguments);
	}
  }
}
Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
	__method.apply(object, arguments);
  }
}

var NewsTicker = Class.create();

NewsTicker.prototype = {
	initialize: function(element, links, data, frequency) {
		this.element = element;
		this.element.appendChild( document.createTextNode('') );
		this.links = links;
		this.data = data;
		this.currentDataIndex = 0;
		this.currentTextIndex = 0;
		this.frequency = frequency;
		
		if( this.data.length > 0 ) {
		this.onTimerEvent();
		}
	},

	onTimerEvent: function() {

		//alert(this.data[this.currentDataIndex].substring(0,this.currentTextIndex ));
		//this.element.firstChild.innerHTML=this.data[this.currentDataIndex].substring(0,this.currentTextIndex );
		//this.element.firstChild.nodeValue=this.data[this.currentDataIndex].substring(0,this.currentTextIndex );
		
		this.element.removeChild( this.element.firstChild );
		this.element.appendChild( document.createTextNode(this.data[this.currentDataIndex].substring(0,this.currentTextIndex )));
		this.element.setAttribute( 'href', this.links[this.currentDataIndex] );
		
		if( this.currentTextIndex >= this.data[this.currentDataIndex].length ) {
			this.currentDataIndex = (this.currentDataIndex+1) % this.data.length;
			this.currentTextIndex = 0;
			setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000);
			
		} else {
		
			if( this.currentTextIndex % 2 == 0 )
				this.element.firstChild.nodeValue = this.element.firstChild.nodeValue + '_';
			else
				this.element.firstChild.nodeValue = this.element.firstChild.nodeValue + '-';

			this.currentTextIndex++;
			setTimeout(this.onTimerEvent.bind(this), 40);
		}
	}
}

function createTickers() {
	//var ticker = new NewsTicker( document.getElementById('ticker'), new Array('http://localhost/hamlet/ticker.html?id=1','http://localhost/hamlet/ticker.html?id=2'), new Array('Ticker string #1','Here is some news. It is longer. Long news. Only at 10!'), 8 );
	
	if(!document.getElementsByTagName || !document.createElement){
		return false;
	}	
	var items = document.getElementsByTagName("ul");
	var i = 0;	
	while (i < items.length )
	{	
		var ticker = items[i];
		var ticker_items = new Array();
		var ticker_links = new Array();
		i++;			
		if( ticker.className.indexOf( "ticker-list" ) != -1 )
		{
			for (var ti=0;ti<ticker.childNodes.length;ti++) {
				if (ticker.childNodes[ti].tagName && ticker.childNodes[ti].tagName.toLowerCase() == 'li') {
					if( ticker.childNodes[ti].firstChild.nodeName && ticker.childNodes[ti].firstChild.nodeName.toLowerCase() == 'a') {
						ticker_items.push( ticker.childNodes[ti].firstChild.firstChild.nodeValue );
						ticker_links.push( ticker.childNodes[ti].firstChild.getAttribute('href') );
					} 
				}
			}
		
		
		ticker.style.display = 'none';
		var ticker_anchor = document.createElement('a');
		ticker_anchor.href = "#";
		ticker_anchor.setAttribute('href', 'null');
		ticker_anchor.className = 'ticker';
		ticker_anchor.setAttribute('class', 'ticker');
						
		ticker.parentNode.appendChild( ticker_anchor );
		new NewsTicker( ticker_anchor, ticker_links,ticker_items, 2 );
		}
	}
}

// Attach function to load
if (window.addEventListener) {
	window.addEventListener("load",createTickers,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",createTickers);
}	