File: //proc/thread-self/root/home/awaldron/www/aw/math/js/math.js
$(document).ready(function() {
var ctr = 0;
var rightCtr = 0;
var timeExp = false;
$('.reload').click(function() {
var interval_id = window.setInterval("", 9999); // Get a reference to the last // interval +1
for (var i = 1; i < interval_id; i++) window.clearInterval(i);
var opout = '';
var op = 'mlt';
var n1 = 0;
var n2 = 0;
var ans = 0;
var f1type = $('#f1type').val();
var f2type = $('#f2type').val();
var f1a = parseInt($('#f1a').val());
var f1b = parseInt($('#f1b').val());
var f2a = parseInt($('#f2a').val());
var f2b = parseInt($('#f2b').val());
var timeout = $('#timeout').val();
var addOn = $( "#addOn:checked" ).length;
var subOn = $( "#subOn:checked" ).length;
var mltOn = $( "#mltOn:checked" ).length;
var divOn = $( "#divOn:checked" ).length;
var opsOn = [];
if (addOn == 1) opsOn[opsOn.length] = 'add';
if (subOn == 1) opsOn[opsOn.length] = 'sub';
if (mltOn == 1) opsOn[opsOn.length] = 'mlt';
if (divOn == 1) opsOn[opsOn.length] = 'div';
var selOp = getRandomInt( 0, opsOn.length - 1 );
op = opsOn[selOp];
timeExp = false;
if (f1type == 'r') { n1 = getRandomInt(f1a, f1b); } else { n1 = f1a; }
if (f2type == 'r') { n2 = getRandomInt(f2a, f2b); } else { n2 = f2a; }
if (op == 'add') {
if (n2 > n1) {
tmp = n1;
n1 = n2;
n2 = tmp;
}
ans = n1 + n2;
opout = "+";
}
if (op == 'mlt') {
ans = n1 * n2;
opout = "x";
}
if (op == 'sub') {
if (n2 > n1) {
tmp = n1;
n1 = n2;
n2 = tmp;
}
ans = n1 - n2;
opout = "-";
}
if (op == 'div') {
if (n2 == 0) {
tmp = n2;
n2 = n1;
n1 = tmp;
}
ans = n1 * n2;
tmp = n1;
n1 = ans;
ans = tmp;
opout = "÷";
}
clearInterval(interval);
$('#theAnswer').val(ans);
$('#n1out').html(n1);
$('#n2out').html(n2);
$('#opout').html(opout);
$('#myAnswer').val('').removeAttr('disabled');
$('#ansCorrect, #ansWrong, #ansTimeout, #nextLink').hide();
$('#timer').html(timeout);
$('#response').removeClass('respRight').removeClass('respAgain');
console.log(ctr+': '+n1+' x '+n2+' = '+ans+' ('+timeout+')');
var alen = ans.toString().length;
//alert(alen);
var counter = 0;
var interval = setInterval(function() {
counter++;
// Display 'counter' wherever you want to display it.
var timer = timeout - counter;
$('#timer').html(timer);
if (counter == timeout) {
// Display a login box
clearInterval(interval);
$('#myAnswer').val(ans).attr('disabled','disabled');
$('#ansCorrect, #ansWrong').hide();
$('#ansTimeout, #nextLink').show();
$('#response').removeClass('respRight').addClass('respAgain');
timeExp = true;
ctr++;
$('#scRight').html(rightCtr);
$('#scTotal').html(ctr);
$('#scPct').html( Math.floor(rightCtr / ctr * 100) );
}
}, 1000);
$('#myAnswer').keyup(function() {
var myAnswer = $(this).val();
//console.log($(this).val().length + '==' + alen);
if (myAnswer.length < alen) {
$('#ansCorrect, #ansWrong, #ansTimeout, #nextLink').hide();
$('#response').removeClass('respRight').removeClass('respAgain');
} else if (myAnswer == ans) {
clearInterval(interval);
$('#ansWrong, #ansTimeout').hide();
$('#ansCorrect, #nextLink').show();
$('#response').addClass('respRight').removeClass('respAgain');
$('#myAnswer').blur();
ctr++;
rightCtr++;
$('#scRight').html(rightCtr);
$('#scTotal').html(ctr);
$('#scPct').html( Math.floor(rightCtr / ctr * 100) );
timeExp = true;
} else {
//clearInterval(interval);
$('#ansCorrect, #ansTimeout, #nextLink').hide();
$('#ansWrong').show();
$('#response').removeClass('respRight').addClass('respAgain');
}
});
return false;
});
$('.numBtn').click(function() {
var theID = $(this).attr('id').substr(5);
var theNum = $(this).html();
var theAns = $('#myAnswer').val();
if (theID == "S") {
$('#settings').slideDown();
} else {
if (theID == "X") {
theAns = theAns.substr(0, theAns.length - 1);
} else {
theAns = theAns + theNum;
}
if (!timeExp) $('#myAnswer').val(theAns).keyup();
}
});
$('#close-settings').click(function() {
$('#settings').slideUp();
});
$('#open-settings').click(function() {
$('#settings').slideDown();
});
$('#start-game').click(function() {
$('#game, #stats').show();
$('#welcome').hide();
$('.reload').click();
});
$("#f1type, #f2type").change(function() {
var thisId = $(this).attr('id').substr(0,2);
if ( $(this).val() == 'f' ) {
$('#'+thisId+'b').hide();
} else {
$('#'+thisId+'b').show();
}
});
var ww = $(window).width();
var wh = $(window).height();
var sh = wh / 750;
//alert(ww+' '+wh+' '+sh);
var zoom = 0;
//$('.zoom').width( ww ).height( wh );
$('.zoom').css('transform','scale('+sh+')');
$('body').css('top', -(document.documentElement.scrollTop) + 'px')
.addClass('noscroll');
$('.bg-preview').click(function() {
var thisFile = $(this).attr('id');
$('body').css('background-image','url(img/'+thisFile+'.jpg)');
});
});
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
$(window).resize(function() {
var ww = $(window).width();
var wh = $(window).height();
var sh = wh / 750;
//alert(ww+' '+wh+' '+sw+' '+sh);
var zoom = 0;
//$('.zoom').width( ww ).height( wh );
$('.zoom').css('transform','scale('+sh+')');
});