
/**
 * Primary body of JavaScript code responsible for controlling the
 * interface.
 * @author Edward Z. Yang
 */

/**
 * Called once document finishes loading, sets up questions and events
 */
function main() {
    
    // setup controls
    $('curQuestion').value = 1;
    questions = [
        null, // no zeroth question
        ProjectileLab.Question.NoAngleRange,
        ProjectileLab.Question.NoHeightRange,
        ProjectileLab.Question.Range,
        ProjectileLab.Question.Height,
        ProjectileLab.Question.Speed,
        ProjectileLab.Question.Tank
    ];
    var jumpQuestion = $('jumpQuestion');
    for (i = 1; i < questions.length; i++) {
        var option = new Option('#' + i + ": " + questions[i].prototype.name, i,
            i == 1, i == 1);
        try {
            // standards compliant; doesn't work in IE
            jumpQuestion.add(option, null);
        } catch (e) {
            jumpQuestion.add(option); // IE only
        }
    }
    Event.observe($('firstQuestion'), 'click', function(e) {
        $('curQuestion').value = 1;
        return setup(e);
    });
    Event.observe($('prevQuestion'), 'click', function(e) {
        $('curQuestion').value--;
        return setup(e);
    });
    Event.observe($('nextQuestion'), 'click', function(e) {
        $('curQuestion').value++;
        return setup(e);
    });
    Event.observe($('lastQuestion'), 'click', function(e) {
        $('curQuestion').value = questions.length - 1;
        return setup(e);
    });
    Event.observe($('jumpQuestion'), 'change', function(e) {
        var j = $('jumpQuestion');
        $('curQuestion').value = j.options[j.selectedIndex].value;
        return setup(e);
    });
    
    Event.observe($('controlForm'), 'submit', check);
    Event.observe($('controlForm'), 'reset', setup);
    setup();
    
}

/**
 * Sets up a question based on the contents of curQuestion
 */
function setup(e) {
    $('answer').disabled = $('submit').disabled = $('reset').disabled = false; // re-enable form controls
    $('answer').value = ''; // clear answer form
    $('answer').focus(); // focus on answer form
    $('plotArea').innerHTML = $('plotInfo').innerHTML = ''; // clear plot
    
    // toggle submit back on
    $('submit').style.display = 'inline';
    $('reset').style.display = 'none';
    
    // get question number, modify questionControls as necessary
    var curQuestion = parseInt($F('curQuestion'));
    var toggleQuestionButtons = function (first, prev, next, last) {
        $('firstQuestion').disabled = first;
        $('prevQuestion').disabled  = prev;
        $('nextQuestion').disabled  = next;
        $('lastQuestion').disabled  = last;
    }
    if (curQuestion == 1) {
        toggleQuestionButtons(true, true, false, false);
    } else if (curQuestion == questions.length - 1) {
        toggleQuestionButtons(false, false, true, true);
    } else {
        toggleQuestionButtons(false, false, false, false);
    }
    $('jumpQuestion').selectedIndex = curQuestion - 1;
    
    question = questions[curQuestion].generate();
    $('questionBox').innerHTML = question.getQuestion();
    if (e && e.preventDefault) e.preventDefault();
    return false;
}

/**
 * Animates an answer. Question needs to have been setup first.
 */
function check(e) {
    $('submit').disabled = true;
    var answer = $F('answer');
    if (question.answers && question.answers > 1) {
        answer = answer.split(/\s*,\s*/, question.answers);
        answer.each(function (value, index) {
            answer[index] = parseFloat(value);
        });
        var error = '<ul>';
        var real_answer = question.getAnswer();
        for (var i = 0; i < question.answers; i++) {
            if (!answer[i]) answer[i] = 0;
            error += '<li>Actual ' +
                question.answerLabels[i] + ': ' +
                real_answer[i].toFixed(3) + ' ' + question.answerUnit[i] +
                ' (' + Math.percentError(real_answer[i], answer[i]) +
                '% error)</li>';
        }
        error += '</ul>';
    } else {
        answer = parseFloat(answer);
        if (!answer) answer = 0;
        var real_answer = question.getAnswer();
        var error = 'Actual: ' +
            real_answer.toFixed(3) + ' ' + question.answerUnit +
            ' (' + Math.percentError(real_answer, answer) + 
            '% error)';
    }
    question.render('plotArea', answer);
    $('answer').disabled = true;
    $('plotInfo').innerHTML = error;
    if (e && e.preventDefault) e.preventDefault();
    return false;
}

/**
 * Switches to reset button after animation ends
 */
function animationEnd() {
    $('submit').style.display = 'none';
    $('reset').style.display = 'inline';
    $('reset').focus();
}

Event.observe(window, 'load', main);
