// easycalc.js - Calculator for 8-count sheet page
// $Id: easycalc.js,v 1.4 2007/01/04 17:49:21 cheer Exp $


function modifyText(id, text) {
        if (document.getElementById) {
		  document.getElementById(id).innerHTML = text;
        }
      }

// init code
var time = new Slider(document.getElementById("time-slider"), document.getElementById("time-slider-input"));
time.setMaximum(180);

var BPM = new Slider(document.getElementById("bpm-slider"), document.getElementById("bpm-slider-input"));
BPM.setMinimum(90);
BPM.setMaximum(200);


var bpmi = document.getElementById("bpm-input");
bpmi.onchange = function () {
    BPM.setValue(parseInt(this.value));
};

var eci = document.getElementById("8c_input");

time.onchange = function() {

    var t = time.getValue();
    var mins = t/60;
    var secs = t%60;

    modifyText('minutes', Math.floor(mins));
    modifyText('seconds', secs);

    calc8c();

    if (typeof window.onchange == "function")
        window.onchange();

}

BPM.onchange = function () {

    bpmi.value = BPM.getValue();
    
    calc8c();

    if (typeof window.onchange == "function")
        window.onchange();
};


resetMusicCalc();
// end init

function resetMusicCalc() {

    time.setValue(30); 
    BPM.setValue(136);
}


function calc8c () {

    var timeInSecs = time.getValue();
    var beatsPerMin = BPM.getValue();
    var beatsPerSec = beatsPerMin/60;
    var totalBeats = beatsPerSec * timeInSecs;
    var eightCounts = totalBeats/8;
    
    eci.value = Math.floor(eightCounts);
    modifyText('the_eight_counts', Math.floor(eightCounts));
}

function calcTheBPMs ()
{
    var timeInSecs = time.getValue();
    var eightCounts = eci.value;
    var bpmcalc_o = BPM.getValue();

    var totalbeats = eightCounts*8;
    var beatsPerSec = totalbeats/timeInSecs;
    var bpmcalcs = beatsPerSec * 60;

    if(bpmcalcs < 100 ){
        return 1;
    }

    else if(bpmcalcs > 190){
        return 1;
    }
    else{
        var bpmvalue = Math.floor(bpmcalcs);
        bpmi.value = bpmvalue;
        BPM.setValue(bpmvalue);
        return 0;
    }

    return 0;
}


function  generateSheet()
{
    var teamName = document.getElementById("team_name");

    myRef = window.open('','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=1,scrollbars=yes');
    var tmp = myRef.document;
    tmp.write('<html><body bgcolor=white><center>')
    tmp.write('<h1>'+ teamName.value + ' Custom 8-Count Music Sheet</h1>');
    tmp.write('<h2>Generated by: Cheerleadingremixes.com</h2></center><br>');

    var musiclen = time.getValue();
    var thebpms = BPM.getValue();
    var therows = eci.value;
    var cnt = 1;
    
    tmp.write('<b>Music Time:' + musiclen + '</b><br>');
    tmp.write('<b>Total 8-counts:' + therows + '</b><br>')
    tmp.write('<b>Beats Per Minute (BPM):' + thebpms + '</b><br><br>');
    while( cnt <= therows)
    {

        tmp.write('<table cellpadding=0 cellspacing=0 style=\"border: 1px solid #a9a9a9;\">');
        tmp.write('<tr>');
        tmp.write('<td width=25 align=center rowspan=3 bgcolor=#c1c1c1 valign=middle><b>' + cnt + '</b></td>');

        for(var i=1; i<=8; i++)
            {
                var bg = "#ffffff";
                if( i%2 == 0) bg = "#ededed";
            
                if( i == 8) tmp.write('<td width=75 bgcolor=' + bg + '>' + i + '</td>');
                else tmp.write('<td style=\"border-right: 1px dotted #a9a9a9;\" width=75 bgcolor=' + bg + '>' + i + '</td>');
            }

        tmp.write('</tr><tr>');
        for(var i=1; i<=8; i++)
            {
                var bg = "#ffffff";
  	            if( i%2 == 0) bg = "#ededed";
                if( i == 8) tmp.write('<td style=\"border-bottom: 1px #a9a9a9 solid;\" width=75 bgcolor=' + bg + '>&nbsp;</td>');
				else tmp.write('<td style=\"border-right: 1px dotted #a9a9a9; border-bottom: 1px #a9a9a9 solid;\" width=75 bgcolor=' + bg + '>&nbsp;</td>');
            }

        tmp.write('</tr>');
        tmp.write('<tr><td colspan=8>song:</td></tr>');
        tmp.write('</table><br><br>');
        cnt++;
    }

    tmp.write('</body></html>');
    tmp.close();
}

function setRgb(r, g, b) {
    time.setValue(time);
    BPM.setValue(g);
}

function getRgb() {
    return {
        r:    time.getValue(),
        g:    BPM.getValue(),
        b:    0
    };
}

function fixSize() {
    time.recalculate();
    BPM.recalculate();
}

window.onresize = fixSize;

fixSize();

