if (typeof Effect == 'undefined') 
	throw("protocounter.js requires including script.aculo.us' effects.js library!");
	
var protocounter = Class.create();
protocounter.prototype = {


initialize: function(container, thenum, big) {
	 this.contain = $(container);
	 this.position = 0;
	 this.increse = 5;
	 this.max_position = 174;
	 this.fps = 20;
	 this.incer = { inc: 0 };
	 this.stopFlag = false;
	 this.snapBack = 4;
	 this.endNumPos = 0;
	 this.thenumber = thenum;
	 this.classwrapper = "counterwrapper";
	 this.classnumbers = "counternumbers";
	 this.maxSpeed = 5.0;
	 this.numheight = 18;
	 if(big)
	 {
	 	this.max_position = 450;
	 	this.classwrapper = "bigcounterwrapper";
	 	this.classnumbers = "bigcounternumbers";
	 	this.maxSpeed = 11.0;
	 	this.numheight = 45;
	 	this.fps = 30;
	 }
	 
	 this.wrapper = new Element("div", {'class': this.classwrapper}).update(
	  this.numbers = new Element("div", {'class': this.classnumbers})
	  );
	  
	  this.contain.insert({'top':this.wrapper});
	  
	  var bindresetField = this.stopSpin.bindAsEventListener(this);
		Event.observe(this.wrapper, 'mousedown', bindresetField);
		
	 },
	 
	 startSpin: function(callback) {
	 	if(this.timeoutId){
				window.clearInterval(this.timeoutId);
			};
	 	
	 	new Effect.Tween (this.incer, 0.0, this.maxSpeed, { duration: 0.6,afterFinish: function() {  callback.triggerNext();  } }, 'inc');
	 	
	 	this.timeoutId = setInterval(this.render.bind(this),parseInt(1000/this.fps));
	 	
	 	this.timeoutId2 = setTimeout(this.stopSpin.bind(this),1500);
			
			
	 },
	 stopSpin: function() {
	 this.endNumPos = this.thenumber * this.numheight;
	 var self = this;
	  self.stopFlag = true;
	 	//new Effect.Tween (this.incer, 5.0, 1.0, { duration: 1, afterFinish: function() { self.stopFlag = true; } }, 'inc');
	 	//new Effect.Tween (this.incer, 5.0, 1.0, { duration: 2 }, 'inc'); 
	 	 
	 },
	 reset: function() {
	 	this.wrapper.scrollTop = this.endNumPos;
	 },
	 render: function() {
	 
	 	
	 	
	 	
	 	 
	 	 if(this.position < this.max_position - this.incer.inc)
	 	 {
	 	 
	 	 	 	 	 
	 	 		 	 
	 	 
	 	 	this.position =  this.position + this.incer.inc;
	 	 	this.wrapper.scrollTop = this.position;
	 	 	return;
	 	 	
	 	 	
	 	 }
	 	 
	 	 
		if(this.stopFlag)
	 	 {
	 	 		
	 	 		
	 	 		
	 	 		this.position = 0;
	 			this.wrapper.scrollTop = this.position;
	 			window.clearInterval(this.timeoutId);
	 	 		
	 	 		var self = this;
	 	 		
	 	 		new Effect.Tween(this.incer, 0, this.endNumPos+this.snapBack, { duration: 0.4, afterFinish: function() { self.reset(); } }, function(p) {
        			self.wrapper.scrollTop = p;
    				}
				);
				
				return;
	 	 }

	 	 
	 	 
	 	 
	 	this.position = 0;
	 	this.wrapper.scrollTop = this.position;
	 	
	 	
	 }
}