/**
 * @name:			Text Ticker
 * @description: 	iterates through a list of copy for display
 * @requires:		DOM/DHTML objects in: /includes/javascript.js
 * @author: 		Rahmin Pavlovic
 * @date: 			2006-09-08
 * @version: 		2008-12-25
 *
 */

/**
 * @author:			Timothy Dalbey  // I just modified the junk.
 * @date			2009-06-01
 * @version			2009-06-01
 */
 
var tickerRef = null; // 'global' ref for each instance
function Ticker(containerID) {
	this.delay = 10;
	this.fade = 400;
	this.html = '';
	this.repeat = 1;
	this.counter = 0;
	this.timer = null;
	this.entry = [];
	this.containerID = containerID;
	this.ticker = document.getElementById(this.containerID);

	this.play = function() {
		if(document.getElementById(this.containerID)) {
			this.show();
			this.counter++;
			this.stop();

			if(this.counter == this.entry.length+1) {
				if(this.repeat) {
					this.counter = 0;
					this.play();
				}
			}else {
				tickerRef = this;
				this.timer = setTimeout('tickerRef.play()', this.delay*1000);
				tickerRef.length = 0; // destroy reference to free memory
			}
		}
	}

	this.show = function() {
		this.setOpacity(this.containerID, 0);
		//alert(this.entry[this.counter]);
		document.getElementById(this.containerID).innerHTML = this.entry[this.counter];
		this.fadeFunct(this.containerID, 0, 100, this.fade);
	}

	this.start = function() {
		tickerRef = this;
		this.timer = setTimeout('tickerRef.play()', this.delay*1000);
		tickerRef.length = 0; // destroy reference to free memory
	}

	this.stop = function() {
		if(this.timer) {
			clearTimeout(this.timer);
		}
	}

	this.next = function() {
		this.stop();
		if(this.counter == this.entry.length) {
			this.counter = 0;
		}
		this.play();
	}

	this.prev = function() {
		this.stop();
		if(this.counter == 1) {
			this.counter = this.entry.length-1;
		}
		else {
			this.counter -= 2;
		}
		this.play();
	}
	
	this.setOpacity =  function(id, opacity){
		el=document.getElementById(id);
		el.style.opacity=(opacity / 100);
		el.style.MozOpacity=(opacity / 100);
		el.style.KhtmlOpacity=(opacity / 100);
		el.style.filter="alpha(opacity=" + opacity + ")";
	}
	
	this.fadeFunct = function(id, start, end, time) {
		var speed=Math.round(time / 100);
		var timer=0;
		var tickerRef = this;
		if(start > end) {
			for(var i=start; i>=end; i--) {
				setTimeout("tickerRef.setOpacity('" + id + "', " + i + ")",(timer * speed));
				timer++;
			}
		}else if(start < end) {
			for(var i=start; i <= end; i++) {
				setTimeout("tickerRef.setOpacity('" + id + "', " + i + ")",(timer * speed));
				timer++;
			}
		}
	}
};