/* ticker.js

    (c) 2004 eg.media GmbH

    Written by Christof Donat and Mathias Meinelt
*/

var Ticker = function() {
	function stop() {
		if( !this.interval ) return;
		clearInterval(this.interval);
		this.interval = false;
	}
	
	function start() {
		if( this.interval ) return;
		this.interval = setInterval("document.getElementById('"+this.container.id+"').ticker.step()",1000/this.speed);
	}

	function changeSpeed(newspeed) {
		this.stop();
		this.speed = newspeed;
		this.start();
	}

	function getTextLength(nodes) {
		var rval = 0;
		for( var i=0; i<nodes.length; i++ ) {
			switch(nodes[i].nodeType) {
				case 1: // Element
					rval += getTextLength(nodes[i].childNodes);
					break;
				case 3: // Text
				case 4: // CDATA
					rval += nodes[i].data.length;
					break;
			}
		}
		return rval;
	}

	function getTextWithLength(nodes,length) {
		var rval = new Array();
		for( var i=0; i<nodes.length; i++ ) {
			switch(nodes[i].nodeType) {
				case 1: // Element
					rval[rval.length] = nodes[i].cloneNode(false);
					ch = getTextWithLength(nodes[i].childNodes,length);
					length -= getTextLength(ch);
					j = rval.length-1;
					for( k = 0; k < ch.length; k++ ) rval[j].appendChild(ch[k]);
					break;
				case 2: // Attribute
					rval[rval.length] = nodes[i].cloneNode(true);
					break;
				case 3: // Text
				case 4: // CDATA
					if( nodes[i].data.length <= length ) {
						rval[rval.length] = nodes[i].cloneNode(true);
						length -= nodes[i].data.length;
					} else {
						rval[rval.length] = document.createTextNode(nodes[i].data.substr(0,length));
						length =0;
					}
					break;
			}
			if( length <= 0 ) return rval;
		}
		return rval;
	}

	function nodeEquals(first,second,recursive) {
		if( first.nodeType != second.nodeType ) return false;
		if( first.nodeType == 3 ) {
			if( first.data != second.data ) 
				return false;
		} else if( first.nodeType == 1 ) {
			if( first.nodeName != second.nodeName ) 
				return false;
			if( recursive ) {
				if( first.childNodes.length != second.childNodes.length ) 
					return false;
				for( var i=0; i < first.childNodes.length; i++ )
					if( !nodeEquals(first.childNodes[i],second.childNodes[i],recursive) ) 
						return false;
			}
		}
		return true;
	}

	function replaceChildNodes(element,nodes) {
		while( element.childNodes.length > nodes.length )
			element.removeChild(element.childNodes[0]);

		for( var i = 0; i < nodes.length; i++ ) {
			if( !element.childNodes[i] )  
				element.appendChild( nodes[i].cloneNode(false) );
			else if( !nodeEquals(element.childNodes[i], nodes[i], false) ) 
				element.replaceChild( nodes[i].cloneNode(false), element.childNodes[i] );
			
			if( !nodeEquals(element.childNodes[i], nodes[i], true) ) 
				replaceChildNodes(element.childNodes[i], nodes[i].childNodes);
		}
	}

	function step() {
		switch( this.type ) {
			case 0: // typewriter
				this.tickerlength += this.stepsize;
				nodes = getTextWithLength(this.text,this.tickerlength);

				replaceChildNodes(this.container,nodes);

				if( this.tickerlength > this.textlength+this.speed ) this.tickerlength = 0;
				break;
			case 1: // switching Content
				while( this.container.hasChildNodes() ) {
					this.container.removeChild(this.container.firstChild);
				}
				if(this.text[this.tickerlength]) this.container.appendChild(this.text[this.tickerlength].cloneNode(true));
				this.tickerlength += this.stepsize;
				if(this.tickerlength >= this.text.length) this.tickerlength = 0;
				break;
			case 2: // two parts
				if( !this.subticker || this.subticker.tickerlength == 0 ) {
					while( this.container.hasChildNodes() ) {
						this.container.removeChild(this.container.firstChild);
					}
					if(this.text[this.tickerlength]) this.container.appendChild(this.text[this.tickerlength].cloneNode(true));
					this.tickerlength += this.stepsize;
					if(this.tickerlength >= this.text.length) this.tickerlength = 0;
					this.subticker = false;
					this.subticker = new Ticker(this.containerID+'_subticker',this.speed,this.stepsize,0);
				}
				this.subticker.step();
				break;
		}
	}

	function createTicker( container, speed, stepsize, type ) {
		this.containerID = container;

		container = document.getElementById(container);

		this.changeSpeed= changeSpeed;
		this.start      = start;
		this.stop       = stop;
		this.step       = step;

		this.container  = container;
		this.speed      = speed;
		this.stepsize   = stepsize;
		this.type       = type;

		this.tickerlength = 0;

		container.ticker = this;
		switch( type ) {
			case 0: // typewriter
				this.text = new Array();
				for( i = 0; i < container.childNodes.length; i++ ) {
					this.text[this.text.length] = container.childNodes[i].cloneNode(true);
				}
				this.textlength = getTextLength(container.childNodes);

				while( container.hasChildNodes() ) {
					container.removeChild(container.firstChild);
				}
				break;
			case 1: // switching Content
				this.text = new Array();
				for( i = 0; i < container.childNodes.length; i++ ) {
					if(container.childNodes[i].nodeType == 1) this.text[this.text.length] = container.childNodes[i].cloneNode(true);
				}
				while( container.hasChildNodes() ) {
					container.removeChild(container.firstChild);
				}
				this.step();
				break;
			case 2: // twoparts 
				//this.tickerlength = 1;
				this.text = new Array();
				c = container;
				if( container.hasChildNodes() ) for( i = 0; i < container.childNodes.length; i++ ) { 
					c = container.childNodes[i];
					if( c.nodeType == 1 ) break; 
				}
				for( i = 0; i < c.childNodes.length; i++ ) {
					if(c.childNodes[i].nodeType == 1) this.text[this.text.length] = c.childNodes[i].cloneNode(true);
				}
				while( c.hasChildNodes() ) {
					c.removeChild(c.firstChild);
				}
				this.step();
				break;
		}
	}

	return createTicker;
}();

