//BRT1.0


///////////////////////////////////////
//
//  Barrel timer by Brothercake
//  http://www.brothercake.com/
//
///////////////////////////////////////



//global object
var tc = new Object;





//numerical bases
tc.bases = [10,10,6,10,6,10];





//digit height
tc.imgHeight = 20;

//animation speed of 100 would mean the digit takes exactly one second to loop around
//therefore you must always set a value which is lower than that
//and never address it from a clock script which iterates faster than 
//once every (speed / 100) seconds
tc.aniSpeed = 25;








//clock timers
tc.timers = [null,null,null,null,null,null];
tc.stopwatch = null;

//timer increments
tc.counters = [0,0,0,0,0,0];

//digit positions
tc.positions = [0,0,0,0,0,0];

//current time
tc.currentTime = [0,0,0,0,0,0];

//new time for set clock animation 
tc.newTime = [0,0,0,0,0,0];

//zero stop
tc.zeroStop = 0;










//stopwatch function
function stopWatch(countUp,zeroStop)
{
	
	//if stopwatch timer is not going
	if(tc.stopwatch == null)
	{
		//start timer
		tc.stopwatch = setInterval('animateDigit(5, ' + countUp + ', 1, ' + zeroStop + ')',1000);
	
		//remember which way we're going
		tc.goingUp = countUp;
	}
	
	//otherwise
	else
	{
		//clear timer
		clearInterval(tc.stopwatch);
		tc.stopwatch = null

		//if this is a command in a new direction
		if(countUp != tc.goingUp)
		{
			//start timer again
			tc.stopwatch = setInterval('animateDigit(5, ' + countUp + ', 1, ' + zeroStop + ')',1000);
	
			//remember which way we're going
			tc.goingUp = countUp;
		}
	}
	
};










//set the clock digits to a particular value
function setClock(now)
{
	
	//for each digit
	for(var i=0; i<6; i++)
	{
		//set alt text to digit number
		document.getElementById('digit' + i).setAttribute('alt',tc.newTime[i]);
		
		//if we are setting it immediately
		if(now)
		{
			//set current time digit to new time digit
			tc.currentTime[i] = tc.newTime[i];

			//calculate top position from digit number
			tc.positions[i] = (0 - (tc.imgHeight * tc.currentTime[i]));

			//set digit to that position
			document.getElementById('digit' + i).style.top = tc.positions[i] + 'px';
		}

		//otherwise
		else
		{
			//get digit difference
			tc.diff = (tc.newTime[i] - tc.currentTime[i]);

			//if new digit is less than old digit
			if(tc.diff < 0)
			{
				//animate digit down to that position
				animateDigit(i, false, (0 - tc.diff));
			}

			//if difference is greater than 0
			if(tc.diff > 0)
			{
				//animate digit up to that position
				animateDigit(i, true, tc.diff);
			}
		}
			
	}
	
};








//initiate animation for a single digit
function animateDigit(digID,moveUp,increments,zeroStop)
{
	
	//if zero stop is present and set to true
	if(typeof zeroStop != 'undefined' && zeroStop)
	{
		//counter to decide whether to stop
		tc.stop = 0;
		
		//for all digits in current time
		for(var i=0; i<6; i++)
		{
			//if this is set to 0 
			if(tc.currentTime[i] == 0)
			{
				//add one to break score
				tc.stop ++;
			}
		}
		
		//if break score equals 6
		if(tc.stop == 6)
		{
			//clear stopwatch timer
			clearInterval(tc.stopwatch);
			tc.stopwatch = null
			
			//don't continue
			return false;
		}
	}
	
	//if this timer is not already going
	if(tc.timers[digID] == null)
	{
		//if digit is moving up
		if(moveUp)
		{
			//add increment to time
			tc.currentTime[digID] += increments;

			//if time gets to numeric base or more
			if(tc.currentTime[digID] >= tc.bases[digID])
			{
				//remove numeric base to reset it
				tc.currentTime[digID] -= tc.bases[digID];

				//if this is not digit 0
				if(digID > 0)
				{
					//increment previous digit
					animateDigit((digID - 1), true, 1);
				}
			}
		}
		//else digit is moving down
		else
		{
			//take increment from time
			tc.currentTime[digID] -= increments;

			//if time gets to -1 or less
			if(tc.currentTime[digID] <= -1)
			{
				//add numeric base to make it positive
				tc.currentTime[digID] += tc.bases[digID];
	
				//if this is not digit 0
				if(digID > 0)
				{
					//decrement previous digit
					animateDigit((digID - 1), false, 1);
				}
			}
		}

		//move this digit
		tc.timers[digID] = setInterval('moveDigit(' + digID + ',' + moveUp + ',' + increments + ')',tc.aniSpeed);
			
		//set alt text to digit number
		document.getElementById('digit' + digID).setAttribute('alt',tc.currentTime[digID]);
	}
	
	return true;
};






//move a digit
function moveDigit(digID,moveUp,increments)
{
	
	//if digit is moving up
	if(moveUp)
	{
		//reduce position value
		tc.positions[digID]--;

		//if position gets to (0 - (image height * numerical base) - 1)
		if(tc.positions[digID] == (0 - (tc.imgHeight * tc.bases[digID]) - 1))
		{
			//reset to -1 to loop the image
			tc.positions[digID] = -1;
		}
	}

	//else digit is going down
	else
	{
		//increase position value
		tc.positions[digID]++;

		//if position gets to 1
		if(tc.positions[digID] == 1)
		{
			//reset to (0 - (image height * numerical base) + 1) to loop the image
			tc.positions[digID] = (0 - (tc.imgHeight * tc.bases[digID]) + 1);
		}
	}

	//apply new position to digit
	document.getElementById('digit' + digID).style.top = tc.positions[digID] + 'px';

	//increment counter
	tc.counters[digID]++;

	//if counter has reached (img height * increments)
	if(tc.counters[digID] == (tc.imgHeight * increments))
	{
		//reset timer
		clearInterval(tc.timers[digID]);
		tc.timers[digID] = null;

		//reset counter
		tc.counters[digID] = 0;
	}

};


