/*** ROLLING COUNTER ***/
var rollingCounter = {
 num : '1',
 dH : 35, // Height of one digit
 tickStep : 1,    // how much to tick each interval
 tickTime : 150, // length of tick interval
 flipStep : 5,  // How much to move each flip interval [must be divisor of height!]
 flipTime : 40, // length of flip interval
 theCeiling: 78449795,
 intervals    : new Array(20),
 flipProgress : new Array(20),

	// Initialize counter	
	init : function( newNum, time ){
            step = 6537482;
		newNum = parseInt( newNum );
		if( newNum == 'NaN'){ return; }
            newNum = 0;
            var ts = Math.round(new Date().getTime() / 1000);
            secsSinceStamp = ts - time;
//            newNum += (secsSinceStamp / (rollingCounter.tickTime / 1000));
		// Read the dH from rolling counter's clientHeight
	      rollingCounter.dH = $('rollingCounterNoScript').clientHeight;

		// Put the number on the page
		$('rollingCounterNoScript').style.display = 'none';
		rollingCounter.num = '' + newNum;
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			rollingCounter.addDigit(i);
		}	
		rollingCounter.putNum();

		// check inc amt and inc time variables
		step = parseInt( step );
		if( step == 'NaN' ){ return; }
		rollingCounter.tickStep = step;

/*		time = parseInt( time );
		if( time == 'NaN' ){ return; }
		rollingCounter.tickTime = time * 1000;
*/
		// If we are good and positive, start the ticking	
		if( rollingCounter.tickStep > 0 ){
			rcid = setInterval( rollingCounter.tick, rollingCounter.tickTime );
		}
	},

	// Add one digit window
	addDigit : function( i ){
		// Every 3rd digit from 1 (4, 7, 10,...) needs a comma
		if( i > 3 && i % 3 == 1 ){
			var comma       = document.createElement('div');
			comma.className = 'digitComma';
			comma.innerHTML = ',';
			$('counter').insertBefore(comma, $('counterFirst').nextSibling );
  	}

		// Add the digit window
		var elem = $('init_digitWindow').clone();
		elem.id = 'digitWindow' + i;
		elem.style.display = '';
		elem.firstChild.id = 'digitSlider' + i;
	
		$('counter').insertBefore(elem, $('counterFirst').nextSibling );
	},


	/** Puts the number into the space **/
	putNum: function(){
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			var thisDigit = rollingCounter.num.substr( rollingCounter.num.length - i, 1);
			rollingCounter.putDigit(i, thisDigit);
		}
	},

	// Puts one digit into place
	putDigit : function( i, val ){
		// Complex! 
		// dH * 10 is for the extra digits [think of them as 10->19, ex:for flip from 9 -> 14]
		// 9 - val because they are arranged from bottom to top.
		$('digitSlider' + i).style.bottom = 
			((rollingCounter.dH * 10) + ((9 - val) * rollingCounter.dH)) + 'px';
	},

	/** For fliping a single digit **/
	flipDigit : function(i, thisDigit, delta ){
		clearInterval( rollingCounter.intervals[i] ); // sanity
		rollingCounter.flipProgress[i] = 0;           // sanity
		rollingCounter.intervals[i] = setInterval( 
			'rollingCounter.flipper('+i+','+ thisDigit + ',' + delta +');', 
			rollingCounter.flipTime
		);
	},

	// This function is called on interval to slowly move the new digit into place
	flipper : function( i, thisDigit, delta ){
		// Exit condition: when we moved delta digits
		if( rollingCounter.flipProgress[i] >= delta * rollingCounter.dH ){
			clearInterval( rollingCounter.intervals[i] );
			return;
		}
		rollingCounter.flipProgress[i] += rollingCounter.flipStep * delta; // Rate of flipping
		// Move the digit slider down some
		$('digitSlider' + i).style.bottom = 
			((rollingCounter.dH * 10) + ((9 - thisDigit) * rollingCounter.dH) - rollingCounter.flipProgress[i]) + 'px';
	},


	/** increments the number and flips digits **/
	tick : function(){
            if(rollingCounter.num >= rollingCounter.theCeiling) {
             rollingCounter.num = parseInt(rollingCounter.theCeiling);
            }

            if((parseInt(rollingCounter.num) + rollingCounter.tickStep) > rollingCounter.theCeiling) {
             newNum = parseInt(rollingCounter.theCeiling) + '';
            } else {
 		 rollingCounter.putNum(rollingCounter.num); 
 		 newNum = (parseInt(rollingCounter.num) + rollingCounter.tickStep) + '';

             if(newNum >= rollingCounter.theCeiling) {
              newNum = (parseInt(rollingCounter.theCeiling)) + '';
             }
            }

		// If the new number is longer than the old one, we need to add digit(s)
		if( newNum.length > rollingCounter.num.length ) {
	  	for( i = rollingCounter.num.length + 1; i <= newNum.length; i++ ){
				rollingCounter.addDigit(i);
				// pad the number so the digits line up correctly
				rollingCounter.num = '0' + rollingCounter.num; 
			}
		}

		// Foreach digit, calculate the delta, then flip
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			var thisDigit1 = rollingCounter.num.substr( rollingCounter.num.length - i, 1);
			var thisDigit2 = newNum.substr( newNum.length - i, 1);
			if( thisDigit1 != thisDigit2 ){
				var delta = thisDigit2 - thisDigit1;
				if( delta < 0 ){ delta += 10; }       // No negative delta please!
				rollingCounter.flipDigit(i, thisDigit1, delta );
			}
		}

		rollingCounter.num = newNum; // set the new number
	}
} // End rolling counter

addLoadEvent(initSavings);

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  /* more code to run on page load */
});

function initSavings() {
// document.getElementById('earnings').innerHTML = "78449795+";
 document.getElementById('suppliers').innerHTML = "1,567+";
 rollingCounter.init(78449795,1328574480);
}

