/*
	BPM Tapper
	Copyright 2009 Michael Chaney Consulting Corporation, All Rights Reserved
	By: Michael Chaney
*/
function BPMTapper() {
	this.tap_times = [];
}

BPMTapper.prototype.start = function() {
	this.tap_times = [];
	this.tap_times.push(new Date);
}

BPMTapper.prototype.tap = function() {
	this.tap_times.push(new Date);
	// Reset if it's too long
	if (this.tap_times[this.tap_times.length-1]-this.tap_times[this.tap_times.length-2] > 2000) {
		this.start();
	}
}

BPMTapper.prototype.bpms = function() {
	if (this.tap_times.length==1) {
		return '-';
	} else {
		return(Math.round(60*1000/((this.tap_times[this.tap_times.length-1] - this.tap_times[0]) / (this.tap_times.length-1))).toString());
	}
}

var tapper = new BPMTapper();

function bpm_tap(div_id) {
	tapper.tap();
	bpm_p = $$("div#" + div_id+ " p.bpm")[0];
	if (bpm_p) { bpm_p.innerHTML = tapper.bpms(); }
}
