
	
/*var time_taken = 0;
var time_dir = 1;
var timer_interval = false;
var timer_id = false;

var score = 0;*/

var total_stars = 0;

var question_number = 0;
var popup_timeout = false;


var total_correct = 0;
var total_answered = 0;

var game_mode = 0;

var results = [];

function init()
{
show_frontpage();
}

function show_frontpage()
{
$('#game_div').hide();
$('#question_popup').hide();
$('#frontpage_div').show();

//stop_timer();
}

function random(range)
{
return parseInt(Math.random() * range);
}

function random_range(start, end)
{
return parseInt(Math.random() * (end - start)) + start;
}

function array_shuffle(a)
{
var d, c, b = a.length;
while (b)
	{
	c = Math.floor(Math.random() * b);
	d = a[--b];
	a[b] = a[c];
	a[c] = d
	}
return a;
}





function sound(src) {
    this.sound = document.createElement("audio");
    this.sound.src = src;
    this.sound.setAttribute("preload", "auto");
    this.sound.setAttribute("controls", "none");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
        this.sound.play();
    }
    this.stop = function(){
        this.sound.pause();
    }    
}







function start_game(selected_mode)
{
document.getElementById('frontpage_div').style.display = 'none';
document.getElementById('game_div').style.display = 'block';

game_mode = selected_mode;

/*score = 0;

if (game_mode == 1) start_timer();

update_score_header();*/

next_question(false);
}

function update_score_header()
{
/*if (game_mode == 1)
	{
	$('#score_div').html('Score: ' + score);
	$('#timer_div').html('Time: ' + text_time(time_taken));
	}
	else
	{
	$('#score_div,#timer_div').html('');
	}*/
}

var number_length = 0;
var decimals_length = 0;
var random_pos_orig = [0, 1, 2, 3, 4, 5, 6];

var random_number = 0;

function next_question(redisplay)
{
document.getElementById('question_popup').style.display = 'none';

number_length = $('#number_size').val();
decimals_length = $('#decimals_size').val();

if (!redisplay)
	{
	random_number = ('' + (Math.floor(Math.random() * (900000) + 100000))).substring(0, number_length);
	do
		{
		random_decimals = ('' + (Math.floor(Math.random() * (900000) + 100000))).substring(0, decimals_length);
		}
	while (random_decimals.indexOf('0') > -1)
	}

$('#numbers_div').html('');

if (game_mode == 0)
	{
	var left_start = 398 - ((number_length - -decimals_length) / 2 * 18);

	for (var i = 0; i < number_length; i ++)
		{
		var split_number = ('' + random_number)[i] + '0000000'.substring(1, number_length - i);
		$('#numbers_div').append('<div class="number" style="top:0px;left:' + (left_start + (i * 18)) + 'px;">' + split_number + '</div>');
		$('#numbers_div').append('<div class="number" id="number_part_' + i + '" style="top:0px;left:' + (left_start + (i * 18)) + 'px;">' + split_number + '</div>');

		setTimeout("move_number_part(" + i + ");", (i + 3) * 1500);
		}

	if (decimals_length > 0)
		{
		$('#numbers_div').append('<div class="number" style="top:0px;left:' + ((left_start + 1) + (i * 18)) + 'px;background:none;">.</div>');
		}

	for (var j = 0; j < decimals_length; j ++)
		{
		var split_number = '.' + ('0000000'.substring(0, decimals_length));
		split_number = split_number.substr(0, j + 1) + random_decimals[j]; + split_number.substr(j + 2);

		split_number = split_number.replace(/0/g, '&nbsp;');
		
		$('#numbers_div').append('<div class="number" style="top:0px;left:' + ((left_start + 18) + ((number_length - 1) * 18)) + 'px;background:none;">' + split_number + '</div>');
		$('#numbers_div').append('<div class="number" id="number_part_' + i + '" style="top:0px;left:' + ((left_start + 18) + ((number_length - 1) * 18)) + 'px;background:none;">' + split_number + '</div>');

		setTimeout("move_number_part(" + i + ");", (i + 3) * 1500);

		i ++;
		}

	$('#numbers_div').append('<div id="game_buttons" style="display:none;"><span class="large blue button" onclick="next_question(false);">SEE ANOTHER</span>&nbsp;&nbsp;&nbsp;<span class="large blue button" onclick="show_frontpage();">RETURN TO MENU</span></div>');
	}
	else
	{
	random_pos = array_shuffle(random_pos_orig.slice(0, number_length));
	
	$('#numbers_div').append('Drag the notations to their correct spaces to create a number.');
	
	var left_start = 398 - ((number_length - -decimals_length) / 2 * 21); 
	
	dropped_numbers = [];
	
	for (var i = 0; i < number_length; i ++)
		{
		dropped_numbers[i] = -1;	
		
		var split_number = ('' + random_number)[i] + '0000000'.substring(1, number_length - i);
		$('#numbers_div').append('<div class="number" id="number_part_' + i + '" style="top:0px;left:' + (left_start + (i * 21)) + 'px;">' + split_number + '</div>');
		$('#numbers_div').append('<div class="numberdrop" id="number_drop_' + i + '" style="top:36px;left:' + (left_start + (i * 21)) + 'px;">' + split_number + '</div>');

		move_number_part_fast(i);
		}

	if (decimals_length > 0)
		{
		$('#numbers_div').append('<div class="number" style="top:36px;left:' + ((left_start + 1) + (i * 21)) + 'px;background:none;">.</div>');
		}

	random_pos = array_shuffle(random_pos_orig.slice(0, decimals_length));

	for (var j = 0; j < decimals_length; j ++)
		{
		dropped_numbers[i] = -1;
		
		var split_number = '.' + ('0000000'.substring(0, decimals_length));
		split_number = split_number.substr(0, j + 1) + random_decimals[j];

		$('#numbers_div').append('<div class="numberdrop" id="number_drop_' + i + '" style="top:36px;left:' + ((left_start + 21) + ((number_length) * 21)) + 'px;background:none;">' + split_number.replace('.', '') + '</div>');
		$('#numbers_div').append('<div class="number" id="number_part_' + i + '" style="top:0px;left:' + ((left_start + 21) + ((number_length) * 21)) + 'px;background:none;">' + split_number + '</div>');

		move_number_part_fast(i);
		
		i ++;
		}

	$('#numbers_div').append('<div id="game_buttons"><span class="large blue button" onclick="check_answer();">Check</span></div>');
		
	$('#numbers_div').append('<div id="reload"><span class="large blue button" onclick="refresh_page();">Reload</span></div>');

	$('.number').draggable({start: function(){$(this).html($(this).html().replace(/&nbsp;/g, '0'));dropped_ok = false;}, containment: '#game_div', stop: number_dropped});

	$('.numberdrop').droppable(
		{
		drop: function (ev, ui)
			{
			dropped_ok = true;
	
			if ($(ui.draggable).html().indexOf('.') > -1) $(ui.draggable).html($(ui.draggable).html().replace(/0/g, '&nbsp;'));
			}
		});
	}
}

var dropped_numbers = [];

function number_dropped(ev, ui)
{
if (!dropped_ok) return;

var drop_x = parseInt(this.style.left);
var drop_y = parseInt(this.style.top);

var closest = 999;
var closest_x = 0;
var closest_html = '';
var closest_id = -1;

for (var i = 0; i < number_length + decimals_length; i ++)
	{
	var droppable_x = parseInt($('#number_drop_' + i).css('left'));
	var distance = Math.abs(droppable_x - drop_x);

	if (distance < closest)
		{
		closest = distance;
		closest_x = droppable_x;
		closest_html = $('#number_drop_' + i).html();
		closest_id = i;
		}
	}

if ($(this).html().indexOf('.') > -1) closest_x -= 21;

$(this).css({'top': parseInt($('#number_drop_0').css('top')) + 2, 'left': closest_x + 1});

if (closest_html == this.innerHTML)
	{
	dropped_numbers[closest_id] = 1;
	}
	else
	{
	dropped_numbers[closest_id] = -1;
	}
}
	
	

function check_answer()
{
var prev_x = 0;
var all_correct = true;

for (var i = 0; i < number_length; i ++)
	{
	var drop_x = parseInt($('#number_part_' + i).css('left'));
	var drop_y = parseInt($('#number_part_' + i).css('top'));
	
	var droppable_x = parseInt($('#number_drop_' + i).css('left'));

	if (drop_y != 38 || Math.abs(drop_x - droppable_x) > 4)
		{
		all_correct = false;
		break;
		}	
	}
	
if (all_correct)
	{
		var mySound;
    
   			mySound = new sound("tingright.mp3");
			mySound.play();
	document.getElementById('question_popup').innerHTML = 'CORRECT!<br><br><span class="large blue button" onclick="next_question(false);">NEXT NUMBER</span>';
	document.getElementById('question_popup').style.display = 'block';
//	score ++;
	update_score_header();
		
			 
	}
	else
	{
		var wrongAnswer;
			
			var mySound2;
    
   			mySound2 = new sound("tingwrong.mp3");
			mySound2.play();
			
	document.getElementById('question_popup').innerHTML = 'DO YOU WANT TO<br><br><span class="large blue button" onclick="next_question(true);">REPEAT</span>&nbsp;&nbsp;&nbsp;<span class="large blue button" onclick="next_question(false);">MOVE TO NEXT NUMBER</span>';
	document.getElementById('question_popup').style.display = 'block';
	}
}

function move_number_part(number_part_id)
{
var new_top = 200;
var new_left = number_part_id * 140;

if (number_part_id >= number_length)
	{
	new_left = (number_part_id - number_length) * 140;
	new_top = 250;
	
	$('#number_part_' + number_part_id).html($('#number_part_' + number_part_id).html().replace(/&nbsp;/g, '0'));
	}

$('#number_part_' + number_part_id).animate({top:new_top, left: new_left});

if (number_part_id == number_length - -decimals_length - 1) $('#game_buttons').fadeIn();
}

function move_number_part_fast(number_part_id)
{
var new_top = 200;
var new_left = random_pos[number_part_id] * 140;

if (number_part_id >= number_length)
	{
	new_left = random_pos[number_part_id - number_length] * 140;
	new_top = 250;
	}

$('#number_part_' + number_part_id).css({top:new_top, left: new_left});
}

/*
function game_over()
{
stop_timer();

document.getElementById('question_popup').innerHTML = 'YOU SCORED ' + score + '!<br><br><span class="large blue button" onclick="show_frontpage();">PLAY AGAIN</span>';
document.getElementById('question_popup').style.display = 'block';
}

function start_timer()
{
time_taken = 120;
timer_interval = setInterval(update_time, 1000);
}

function update_time()
{
time_taken --;
update_score_header();

if (time_taken <= 0) return game_over();
}

function stop_timer()
{
clearInterval(timer_interval);
}
*/
	function refresh_page()
{
location.reload(true);
}
	

function text_time(s)
{
return(s-(s%=60))/60+(9<s?':':':0')+s;
}

function print_results()
{
window.print();
}

function popup(message)
{
if (popup_timeout)
	{
	clearTimeout(popup_timeout);
	popup_timeout = false;
	}

document.getElementById('question_popup').style.display = message ? 'block' : 'none';

if (message) document.getElementById('question_popup').innerHTML = message;
if (message) popup_timeout = setTimeout("popup(false);", 2000);
}

function leftClick(e)
{
window.focus();
if (!e) e = event;
return ((typeof e.which == 'undefined') ? (e.button == 1) : (e.which == 1 || e.button == 0));
}

function nrc(e)
{
if (leftClick(e) == false)
	{
	return ce(e);
	}
}

function cp(e)
{
if (!e) e = event;
if (e.stopPropagation) e.stopPropagation(); else if (typeof e.cancelBubble != 'undefined') e.cancelBubble = true;
}

function ce(e)
{
if (!e) e = event;

if (typeof e.preventDefault != 'undefined')
	{
	e.preventDefault();
	}
	else if (typeof e.cancelBubble != 'undefined')
	{
	e.returnValue = 0;
	e.cancelBubble = true;
	}

return false;
}