var max_numbers = 5;
var attempts = 0;

// load in previous_scores from local database, if none found created an empty object
var previous_scores = JSON.parse(localStorage.getItem('app_data')) || {};

//$(function(){

$(document).ready(function() {

	load_my_scores();
	
	$('#play_new_game').hide();
	$('#status').hide();
	
	// create array of 5 random unique numbers
	var solution = new_solution();
	
	$('.up').click(function(e) {
	
		e.preventDefault();
		
		// find input sibling
		field = $(this).siblings('input[type=text]');
	
		new_value = parseInt(field.val()) + 1;
		
		if (new_value < 10) {
			field.val(new_value);
		}
		
	});
	
	$('.down').click(function(e) {
	
		e.preventDefault();
		
		// find input sibling
		field = $(this).siblings('input[type=text]');
	
		new_value = parseInt(field.val());
		
		if (new_value > 0) {
			field.val(new_value - 1);
		}
		
	});
	
	$('#check_numbers').click(function(e) {

		e.preventDefault();
		
		//$('#status').show();
		
		// add all the numbers into an array
		var current_numbers = new Array();
		current_numbers.push($('#number_1').val());
		current_numbers.push($('#number_2').val());
		current_numbers.push($('#number_3').val());
		current_numbers.push($('#number_4').val());
		current_numbers.push($('#number_5').val());
		
		if (current_numbers.unique().length != max_numbers) {
		
			// duplicate numbers selected	
			alert('Please choose ' + max_numbers + ' different numbers');
			
		} else {
		
			
			attempts++;
			$('#answers').show();
			
			green = 0;
			orange = 0;
			
			// create new table row
			row = $('<tr></tr>');
			
			// compare chosen numbers with the solution
			$.each(current_numbers, function(i, value) {
			
				//console.log(value);
				
				if (in_array(value, solution)) {
					if (value == solution[i]) {
						// current_number is in the correct place
						green++;
					} else {
						// current_number is in the solution, but in the wrong slot
						orange++;
					}
				}
					
			});
			
			row.append($('<td class="green">' + green + '</td>'));
			row.append($('<td class="orange">' + orange + '</td>'));
			
			// add cells for each number chosen
			$.each(current_numbers, function(i, value) {
				row.append($('<td class="value">' + value + '</td>'));	
			});
			
			$('#answers').prepend(row);
			
			if (green == max_numbers) {
			
				// puzzle solved!
				//console.log('Solved');
				if (attempts == 1) {
					alert_str = 'You solved the puzzle in ' + attempts + ' attempt! Are you psychic?!';
				} else {
					if (attempts < 6) {
						alert_str = 'You solved the puzzle in ' + attempts + ' attempts (awesome!)';
					} else {
						alert_str = 'You solved the puzzle in ' + attempts + ' attempts';
					}
				}
				
				alert(alert_str);
				$('#status').show();
				$('#status').html('Solved!');
				$('#play_new_game').show();
				$('#check_numbers').hide();
				
				// save previous score to localStorage
				d = new Date();
				//time_stamp = d.getTime() + (d.getTimezoneOffset() * 60000);
				time_stamp = d.getTime();
				previous_scores[time_stamp] = { num_attempts:attempts, date:time_stamp };
				
				localStorage.setItem('app_data', JSON.stringify(previous_scores));
				
				// reset attempts
				attempts = 0;
				
				// reload_scores
				load_my_scores();
				
			}
			
		}
		
	});
	
	$('#play_new_game').click(function() {
	
		$('#check_numbers').show();
		$('#status').hide();
		$('#play_new_game').hide();
		
		// reset numbers
		$('#number_1').val('0');
		$('#number_2').val('0');
		$('#number_3').val('0');
		$('#number_4').val('0');
		$('#number_5').val('0');
		
		// store new solution
		solution = new_solution();
		
		// remove all table rows from #answers (except the header)
		$('#answers tr:not([th])').remove();
		
	});

	$('a[target="_blank"]').click(function() {
		if (confirm('This link opens in a new window.')) {
			return true;
		} else {
			$(this).removeClass('active');
			return false;
		}
	});
	
});
