﻿var bpm;
var started = false;
var initialized = false;
var starttime;

function addone(){
    var result = 0;
    if(!started){
        starttime = new Date().getTime();
        bpm=0
        started = true;
    }
    else{
        bpm++;
        var color;
        var width;
        var endtime = new Date().getTime();
        var timediff = (endtime - starttime) / 1000;
        result = bpm * 60 /timediff;
        (bpm>49) ? width="100%" : width=bpm*2+"px";
        switch (true){
            case (bpm < 10):
                color = '#FF0000';
                break;
            case (bpm < 20):
                color = '#FF9999';
                break;
            case (bpm < 30):
                color = '#BFFFBF';
                break;
            default:
                color = '#4BFF4B';
                break;
        }
        document.getElementById('colorbar').style.backgroundColor = color;
        document.getElementById('colorbar').style.width = width;
    }
    document.getElementById('bpm').innerHTML = result.toFixed(1) + " bpm";
}

function resetctr(){
    if(initialized){
        document.onkeydown = "";
        initialized = false;
        started = false;
        document.getElementById('Button1').value = "Begin";
        document.getElementById('bpm').innerHTML = "--find the bpm--";
        document.getElementById('colorbar').style.backgroundColor = '#FFF';
    }
    else{
        document.onkeydown = addone;
        document.getElementById('Button1').value = "End";
        document.getElementById('bpm').innerHTML = "Press any key to the beat";
        initialized = true;

    }
}

function toggle(container,divname){
    var mydiv = document.getElementById(divname);
    var divheader = document.getElementById(container.id);
    if(mydiv.style.display=="none"){
        mydiv.style.display = "block";
        divheader.style.backgroundImage = "url('config/images/minus.gif')";
    }
    else {
        mydiv.style.display = "none";
        divheader.style.backgroundImage = "url('config/images/plus.gif')";
    }
}

