var mooTimer = new Class({
	
	options: {
		startCrono: '15:00',
		endCrono: '00:00',
		div: false,
		stopOnComplete: true,
		onComplete: function(){ },
		onEveryMinute: function(){ }
	},
	initialize: function(options){
		this.setOptions(options);
		this.state = 'paused';
		this.reset();
	},
	start: function() {
		if (this.state != 'started') {
			this.timer = this.count.periodical(1000,this); 
			this.state = 'started';
		}
	},
	pause: function() { 
		if (this.state != 'paused') {
			$clear(this.timer); 
			this.state = 'paused';
		}
	},
	count: function(){
		
		this.seg +=this.incremental;
		
		if ((this.seg == -1) || (this.seg == 60)) {
			this.seg = (this.incremental > 0) ? 0 : 59;
			this.min += this.incremental;
			this.fireEvent('onEveryMinute', '');
		}
	
		if (this.options.div != false);
			$(this.options.div).set('text', this.time());

		if ((this.min == this.endMin) && (this.seg == this.endSeg)) {
			this.fireEvent('onComplete', '');
			if (this.options.stopOnComplete)
				this.pause();
		}
	},
	time: function() {
		time_to_show = (this.min < 10) ? "0" + this.min : this.min;
		time_to_show += ((this.seg < 10) ? ":0" : ":") + this.seg;
		return time_to_show;
	},
	reset: function(){
		//reset time to initial values
		start_array = this.options.startCrono.split(":");
		end_array = this.options.endCrono.split(":");
		
		this.startMin = parseInt(start_array[0]);
		this.startSeg = parseInt(start_array[1]);

		this.endMin = parseInt(end_array[0]);
		this.endSeg = parseInt(end_array[1]);
		
		if (this.endMin != this.startMin)
			this.incremental = (this.endMin > this.startMin) ? 1 : -1;
		else
			this.incremental = (this.endSeg > this.startSeg) ? 1 : -1;
		
		this.min = this.startMin;
		this.seg = this.startSeg;

		if (this.options.div != false)
			$(this.options.div).set('text', this.time());
	}
});

mooTimer.implement(new Events, new Options);

