//
// Resistor colour code training app
//
// Sam May september 2009
//
//

// Firstly, ALL HAIL JQUERY

// map digits to colours
var colours = new Array("black","brown","red","orange","yellow","green","blue",
                       "violet","grey","white");

// resistor sizes
var e12_range = new Array(10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82);
var sizes = new Array(0,1,2,3,4,5);

var resistor;
var asking	= true;
var asked	= 0;
var correct	= 0;
var score	= 0;

function randomElement(array) {
    return array[Math.round(Math.random()*(array.length-1))];
}

// Resistor object
function Resistor() {
    e12_value = randomElement(e12_range);
    size = randomElement(sizes);

    // extract digits
    digit1 = Math.floor(e12_value/10);
    digit2 = e12_value % 10;

    // assign resistor properties. they all have 10% tolerance lol
    this.value = e12_value * Math.pow(10,size);
    // leave out the tolerance band for now, set it permanantly to gold
//    this.colorBands = new Array(colours[digit1],colours[digit2],colours[size],"gold");
    this.colorBands = new Array(colours[digit1],colours[digit2],colours[size]);
}

// convert resistor value into nice output string
function value2prefix(value) {
    prefixes = new Array("","K","M");
    size = 1;
    i = 0;
    while (value >= 1000*size && i < prefixes.length) {
	size *= 1000;
	i++;
    }
    return (value/size).toString() + prefixes[i];
}

// input parsing
function prefix2value (prefix_str) {
    // if someone somehow manages to actually input an ohm sign, we will handle
    // that correctly.
    if (prefix_str.match(/^[0-9]+[rR\u2126]?$/)) {
	return parseInt(prefix_str);
    } else if (prefix_str.match(/^[0-9]+(\.[0-9]+)?[kK]\u2126?$/)) {
	return parseFloat(prefix_str)*1000;
    } else if (prefix_str.match(/^[0-9]+(\.[0-9]+)?[mM]\u2126?$/)) {
	return parseFloat(prefix_str)*1000000;
    } else {
	return -1;
    }
}

// these two are the main functions
function newQuestion() {
    // remove the old colour classes if they are there
    if (resistor) {
	$("#firstDigit").removeClass(resistor.colorBands[0]);
	$("#secondDigit").removeClass(resistor.colorBands[1]);
	$("#multiplier").removeClass(resistor.colorBands[2]);
    } else {
	// if resistor is null, this is the first time we have been called, so set
	// the resistor tolerance colour.
	$("#tolerance").addClass("gold");
    }

    // add new classes
    var resistor = new Resistor();
    $("#firstDigit").addClass(resistor.colorBands[0]);
    $("#secondDigit").addClass(resistor.colorBands[1]);
    $("#multiplier").addClass(resistor.colorBands[2]);

    // clear text box and result prompt.
    $("form")[0].reset();
    $("#result").html("");

}

function markAnswer() {
    var answer = prefix2value($("input").val());
    if (answer == resistor.value) {
	$("#result").css("color","green");
	$("#result").html("Correct!");
	correct++;
    } else {
	$("#result").css("color","red");
	$("#result").html("Wrong. It's "+value2prefix(resistor.value));
    }
}

// called from 'onsubmit'
function updateApp() {

    // perform appropriate action on form submission
    if (asking) {
	// mark a question
	asked++;
	markAnswer();
	asking = false;
    } else {
	// create a new question
	newQuestion();
	asking = true;
    }

    // update score
    score=(correct/asked)*100;
    $("#score").html(
	correct + "/" + asked + " correct (" + score.toFixed(1) + "%)"
    );

}

$(document).ready(function(){
    $("form").submit(function(e) {
	// prevent form from being submitted so page does not refresh
	e.preventDefault();
	// trigger change in state, marking or generating a new question
	updateApp();
    });

    $("div.toggle > a").click(function () {
	$(this).parent().next().toggle();
    });

    newQuestion();
});
