﻿var speed_per_kiro_arr = [6.5, 6, 5.5, 0, 7, 6.5, 6, 5.5, 5, 4.5, 0, 0, 0, 0, 0, 0];
var speed_per_hour_arr = [0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 13];
var bpm_arr = [160, 165, 170, 160, 160, 160, 165, 170, 175, 180, 160, 160, 165, 170, 175, 180];

// ポップアップの表示
function showPopup(triggerButtonId, errorId) {
    clickButton(triggerButtonId)
}
// ボタンのクリック
function clickButton(triggerButtonId) {
    // FireFox
    if (document.createEvent) {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById(triggerButtonId).dispatchEvent(e);
    }
    // IE
    else {
        document.getElementById(triggerButtonId).click();
    }
}

// BPMの計算
function calcBpm(speedId, styleId, bpmId, conditionId, stepId, displayId) {
    var speedIdx = $get(speedId).selectedIndex;
    var styleIdx = $get(styleId).selectedIndex;

    // Calcurate BPM
    var bpm = bpm_arr[speedIdx];
    if (styleIdx == 1) bpm += 5;
    else if (styleIdx == 2) bpm -= 5;
    if (bpm > 180) bpm = 180;
    else if (bpm < 160) bpm = 160;

    // Calcurate Step
    var step = 0;
    if (speed_per_kiro_arr[speedIdx] != 0) {
        step = roundStep2(100000 / (bpm * speed_per_kiro_arr[speedIdx]));
    } else {
        step = roundStep2((speed_per_hour_arr[speedIdx] * 100000) / (bpm * 60));
    }
    var step_bpm160 = 0;
    if (speed_per_kiro_arr[speedIdx] != 0) {
        step_bpm160 = roundStep2(100000 / (160 * speed_per_kiro_arr[speedIdx]));
    } else {
        step_bpm160 = roundStep2((speed_per_hour_arr[speedIdx] * 100000) / (160 * 60));
    }
    var step_bpm180 = 0;
    if (speed_per_kiro_arr[speedIdx] != 0) {
        step_bpm180 = roundStep2(100000 / (180 * speed_per_kiro_arr[speedIdx]));
    } else {
        step_bpm180 = roundStep2((speed_per_hour_arr[speedIdx] * 100000) / (180 * 60));
    }

    // Set Step
    var step_drop = $get(stepId)
    for (var j = 0; j < step_drop.length; j++) {
        step_drop.options[0] = null;
    }
    var i = 0;
    var s = step_bpm180;
    while (s <= step_bpm160) {
        step_drop.options[i] = new Option(s.toString() + 'cm', s.toString());
        i++;
        s += 2.5;
    }
    for (var i = 0; i < step_drop.options.length; i++) {
        var option = step_drop.options[i];
        if (option.text == step.toString() + 'cm') {
            option.selected = true;
            break;
        }
    }

    $get(bpmId).innerHTML = bpm.toString();
    if (speed_per_hour_arr[speedIdx] == 0) {
        $get(conditionId).innerHTML = '(スピード キロ' + speed_per_kiro_arr[speedIdx].toString() + '分、歩幅 ';
    } else {
        $get(conditionId).innerHTML = '(スピード 時速' + speed_per_hour_arr[speedIdx].toString() + 'km、歩幅 ';
    }
    $get(displayId).style.display = "inline";
}

// BPM2の計算 (歩幅固定)
function calcBpm2(speedId, styleId, bpmId, conditionId, stepId, displayId) {
    //if (validateHeight(heightId, errorId)) {
    var height = 165;
    var speedIdx = $get(speedId).selectedIndex;
    var styleIdx = $get(styleId).selectedIndex;

    var bpm = bpm_arr[speedIdx];
    if (styleIdx == 1) bpm += 5;
    else if (styleIdx == 2) bpm -= 5;
    if (bpm > 180) bpm = 180;
    else if (bpm < 160) bpm = 160;

    var step = 0;

    if (speed_per_hour_arr[speedIdx] == 0) {
        step = Math.round(100000 / (bpm * speed_per_kiro_arr[speedIdx]));
    } else {
        step = Math.round((speed_per_hour_arr[speedIdx] * 100000) / (bpm * 60));
    }
    step = roundStep2(step);

    $get(bpmId).innerHTML = bpm.toString();
    if (speed_per_hour_arr[speedIdx] == 0) {
        $get(conditionId).innerHTML = '(スピード キロ' + speed_per_kiro_arr[speedIdx].toString() + '分、歩幅' + step.toString() + 'cm';
    } else {
        $get(conditionId).innerHTML = '(スピード 時速' + speed_per_hour_arr[speedIdx].toString() + 'km、歩幅' + step.toString() + 'cm';
    }
    $get(displayId).style.display = "inline";
    //}
}

// 歩幅変更イベント
function changeStep(speedId, bpmId, conditionId, stepId) {
    var bpm = 0;
    var speedIdx = $get(speedId).selectedIndex;
    var stepstr = $get(stepId).options[$get(stepId).selectedIndex].text;
    var step = stepstr.substring(0, stepstr.length - 2);
    if (speed_per_hour_arr[speedIdx] == 0) {
        bpm = 100000 / (step * speed_per_kiro_arr[speedIdx]);
    } else {
        bpm = (speed_per_hour_arr[speedIdx] * 100000) / (step * 60);
    }
    bpm = roundBpm(bpm);
    $get(bpmId).innerHTML = bpm.toString();
    if (speed_per_hour_arr[speedIdx] == 0) {
        $get(conditionId).innerHTML = '(スピード キロ' + speed_per_kiro_arr[speedIdx].toString() + '分、歩幅';
    } else {
        $get(conditionId).innerHTML = '(スピード 時速' + speed_per_hour_arr[speedIdx].toString() + 'km、歩幅';
    }
}

// 入力身長の検証
function validateHeight(heightId, errorId) {
    var height = $get(heightId).value;
    if (height.match(/^[1-9][0-9]*$/)) {
        h = parseInt(height);
        if ((150 <= h) && (h <= 180)) {
            return true;
        }
    }
    $get(errorId).style.display = "block";
    return false;
}

// 歩幅の5cm丸め
function roundStep(step) {
    var _step = step / 5;
    _step = Math.round(_step);
    return _step * 5;
}
// 歩幅の2.5cm丸め
function roundStep2(step) {
    var _step = step / 2.5;
    _step = Math.round(_step);
    return _step * 2.5;
}

// BPMの5BPM丸め
function roundBpm(bpm) {
    var _bpm = bpm / 5;
    _bpm = Math.round(_bpm);
    _bpm = _bpm * 5;
    if (_bpm > 180) _bpm = 180;
    else if (_bpm < 160) _bpm = 160;
    return _bpm;
}

function getPitch(bpm, id) {
    var baseBpm = 0;
    switch (id) {
        case '39':
        case '44':
        case '49':
            baseBpm = 160;
            break;
        case '40':
        case '45':
        case '50':
        case '51':
            baseBpm = 165;
            break;
        case '41':
        case '42':
        case '46':
        case '47':
        case '52':
        case '53':
            baseBpm = 170;
            break;
        case '54':
            baseBpm = 175;
            break;
        default:
            baseBpm = -1;
            break;
    }
    var pitch = -1;
    if (baseBpm != -1) {
        pitch = bpm - baseBpm;
        if (pitch < 0) pitch = 0;
        else if (pitch > 10) pitch = 10;
    }
    return pitch;
}