var ProtoCounter = Class.create();
ProtoCounter.prototype = {
	
	initialize : function () {
		this.digitsCount = 5;
		this.digit = [];
		this.pase = 1;
		this.desiredCount = 0;
		this.currentCount = 0;
		this.counterContainer = $('counter');
		this.animationArray = [];
		
		for (var i=this.digitsCount-1; i>=0; i--){
			var digitDiv = document.createElement("div");
			Element.extend(digitDiv);
			digitDiv.className = "digit";
			digitDiv.id = "digit_"+i;
			$('counter').appendChild(digitDiv);
			this.digit[i] = 0;
		}//end for
		
		
		this.intervalReference = setInterval((function(a){
			return function () {
				if (a.currentCount != a.desiredCount) {
					a.currentCount += a.pase;
					a.explodeNumber(a.currentCount);
				}
				
				if (a.animationArray.length > 0){
					var currentStep = a.animationArray.shift();
					while (currentStep.length){
						currentAnimation = currentStep.pop();
						var bx = -1*currentAnimation.value*24;
						var by = currentAnimation.y;
						$('digit_'+currentAnimation.position).style.backgroundPosition = bx+"px "+by+"px";
					}
				}
			}
		})(this), 100);
		
	},
	
	setValueProgresive : function (value) {
		this.desiredCount = value;
		if (this.currentCount != this.desiredCount){
			this.pase = this.desiredCount > this.currentCount ? 1 : -1;
		}
	},
	
	setValue : function (value) {
		this.desiredCount = value;
		this.currentCount = value;
		this.explodeNumber(value);
	},
	
	explodeNumber : function (value) {
		var valueString = value.toString();
		var l = valueString.length;
		var beforeAnimationStep = [];
		var animationStep = [];
		for (var i=0; i<l; i++){
			var pos = l-1-i;
			if (this.digit[i] != parseInt(valueString.charAt(pos))){
				this.digit[i] = parseInt(valueString.charAt(pos));
				beforeAnimationStep.push({
					position: i,
					value 	: this.digit[i],
					y		: -34
				});
				
				animationStep.push({
					position: i,
					value	: this.digit[i],
					y		: 0
				});
			}
		}//end for
		this.animationArray.push(beforeAnimationStep);
		this.animationArray.push(animationStep);
	}//end draw
	
}