/*
ALL SCRIPTING IN THIS DOCUMENT WAS WRITTEN BY NICK ANNIS FOR THE ENGINES AND ENERGY CONVERSION LAB (CSU)
	-last revised: 2/19/09
	
AVAILABLE FUNCTIONS:
-constructor				:	generates all necessary html elements for the ticker and applies them as
								innerHTML value to the case passed in as an argument
									-args:	name 		- STRING what the ticker will reference itself as in
														  window["<name>"] calls
											element 	- ELEMENT case to apply html to
											numDigits	- INT number of digits to initially generate the ticker with

-preloadImages				:	loads ticker images locally
-setValue					:	takes an integer value and sets the initial ticker number to that value
		  							-args:	newVal		- INT value to have ticker show
-setRate					:	takes an integer value and sets the rate that setInterval will call increment
		 							-args:	newRate		- INT value, time (ms) between function calls
												NOTE:	initial rate set to 50ms, rates < 20ms can cause problems
-setTickAmt					:	sets the value that the number increments by (default: 1)
									-args:	newAmt		- INT value, how much ticker is incremented by when 
														  increment() gets called
-start						:	starts the ticker counting up
-freeze						:	freezes the ticker at the current value
-increment					:	increments the current number shown on the ticker
-truncate					:	truncates leading 0's on the number displayed by the ticker (sets their 
								style.display = 'none', if increment is called and the new number spills 
								over to one of these digits, it will be visible again)
-setLooping					:	when called, sets the ticker's max value to maxNum and counts back up from 0
									-args:	maxNum		-INT value, maximum number to count to before starting over
							
*/

function ticker(name,element,numDigits){
	if(typeof ticker.count == "undefined"){
		ticker.count = 0;
	}
	ticker.count++;
	
	this.name = name;
	window[this.name] = this;
	this.object = "window[\""+this.name+"\"]";
	this.UID = ticker.count;
	this.dObj = element;
	
	this.digits = numDigits;
	this.value = 0;
	this.rate = 50;
	this.interval = null;
	this.incAmt = 1;
	
	this.looping = false;
	this.maxLoop = 0;
	
		this.zero = "../frames/0.gif";
		this.one = "../frames/1.gif";
		this.two = "../frames/2.gif";
		this.three = "../frames/3.gif";
		this.four = "../frames/4.gif";
		this.five = "../frames/5.gif";
		this.six = "../frames/6.gif";
		this.seven = "../frames/7.gif";
		this.eight = "../frames/8.gif";
		this.nine = "../frames/9.gif";
		
		this.srcArray = new Array(this.zero,this.one,this.two,this.three,this.four,
								  this.five,this.six,this.seven,this.eight,this.nine);
		
		var inHtml = "";
		
		for(var i=0;i<this.digits;i++){
			inHtml += "<img src='"+this.zero+"' name='tick"+i+"."+this.UID+"' id='tick"+i+"."+this.UID+"' border='0'/>";
			if((i+1)%3 == 0 && i != (this.digits-1)){
				inHtml += "&nbsp;";
			}
		}
				 
	this.dObj.innerHTML = inHtml;
	
	return true;
}

//CLASS FUNCTIONS

	ticker.prototype.preloadImages = function(){
		this.tickLoad = new Array();
		for(var i=0;i<this.srcArray.length;i++){
			this.tickLoad[i] = new Image();
			this.tickLoad[i].src = this.srcArray[i];
		}
	}

	ticker.prototype.setValue = function(newVal){
		if(typeof newVal == "number"){
			this.value = newVal-this.incAmt;
			this.increment();
		}
	}
	
	ticker.prototype.setRate = function(newRate){
		if(typeof newRate == "number"){
			this.rate = newRate;
			this.freeze();
		}
	}
	
	ticker.prototype.setTickAmt = function(newAmt){
		if(typeof newAmt == "number"){
			this.incAmt = newAmt;
		}
	}
	
	ticker.prototype.start = function(){
		this.freeze();
		this.interval = setInterval(this.object+".increment()",this.rate);
	}
	
	ticker.prototype.freeze = function(){
		clearInterval(this.interval);
	}
	
	ticker.prototype.increment = function(){
		this.value += this.incAmt;
		if(this.value<0){
			this.value == 0;
			this.freeze();
		}
		if(this.looping){
			this.value = this.value%this.maxLoop;
		}
		var valueString = this.value+"";
		var curr;
		for(var i=this.digits-1,j=valueString.length-1;i>=0;i--,j--){
			curr = document.getElementById("tick"+i+"."+this.UID);
			var currDigit;
			if(j>=0){
				currDigit = valueString.charAt(j);
			}
			else{
				currDigit = "0";
			}
			curr.src = this.srcArray[parseInt(currDigit)];
		}
		if(curr.style.display == "none" && j==0 && !this.looping){
			curr.style.display = "inline";
		}
	}
	
	ticker.prototype.truncate = function(){
		for(var i=0;i<this.digits;i++){
			var curr = document.getElementById("tick"+i+"."+this.UID);
			if(curr.src=="http://www.eecl.colostate.edu"+this.zero.replace("..","")){
				curr.style.display = "none";
			}
			else{
				i = this.digits;
			}
		}
	}
	
	ticker.prototype.setLooping = function(maxNum){
		this.maxLoop = maxNum;
		this.looping = true;
	}