/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
	var ctr = {	digits: 8,
				fontsize: 120,
				width: 322,
				values: [0],
				running_total: 1337,
				leading_zeroes: false,
				delay: 25,
				home: 'http://dev.davidallgroup.com/~ebowen/leaderboard/public/kimbia/' };

	$(document).ready( function()
	{
		initCtr();
		animateCtr();
	});

	function initCtr()
	{
		// how many digits?
		ctr.running_total = env.count;

		ctr.digits = ctr.running_total.toString().length;

		$('#ctr').empty();
		
		for(var i = 0; i < ctr.digits; i++)
		{
			$('#ctr').append("<div class='num'></div>");
		}
	}

	function animateCtr()
	{
		$('#ctr').show();

		// set up n values approaching the running total
		var n = 100;
		var p = 0;
		var step = ctr.running_total / n;

		for( var i = 0; i < n; i++ )
		{
			p += step;
			ctr.values.push( Math.round( p + ( Math.random() * step ) ) );
		}

		// we want to end on the right number
		ctr.values.push( ctr.running_total );

		// then start a loop to display each value in turn
		stepCtr();
	}

	function stepCtr()
	{
		if( ctr.values.length )
		{
			setCtr( ctr.values.shift() );
			setTimeout( stepCtr, ctr.delay );
		}
	}

	function setCtr( x )
	{
		// break x into a set of decimals:
		var str = x.toString();

		if( str.length < ctr.digits )
		{
			// pad the string with zeroes or a control char
			var padding;

			if( ctr.leading_zeroes )
			{
				padding = "00000000";
			}
			else
			{
				padding = "xxxxxxxx";
			}

			str = padding.substr(0, ctr.digits - str.length) + str;
		}
		

		// now iterate through the string
		for( var i = 0; i < ctr.digits; i++ )
		{
			setNumeric( i, str.substr(i, 1), ctr.fontsize );
		}
	}

	function incrementCtr()
	{
		setCtr( ++ctr.running_total );
	}

	function setNumeric( index, value, fontsize )
	{
		// check for control char
		if( value == 'x' ) value = '10';

		var offset = "0px " + ( -fontsize * value ) + "px";
		$('#ctr div:eq(' + index + ')').css({ backgroundPosition: offset } );

	}