function resetCalculator(curValue) {
	jQuery("#display").val(curValue);
	jQuery(".function-button").removeClass("pendingFunction");
	jQuery("#display").data("isPendingFunction", false);
	jQuery("#display").data("thePendingFunction", "");
	jQuery("#display").data("valueOneLocked", false);
	jQuery("#display").data("valueTwoLocked", false);
	jQuery("#display").data("valueOne", curValue);
	jQuery("#display").data("valueTwo", 0);
	jQuery("#display").data("fromPrevious", false);
}

function debugStates() {
	console.log("------------")
	console.log("isPendingFunction: " + jQuery("#display").data("isPendingFunction"));
	console.log("thePendingFunction: " + jQuery("#display").data("thePendingFunction"));
	console.log("valueOneLocked: " + jQuery("#display").data("valueOneLocked"));
	console.log("valueTwoLocked: " + jQuery("#display").data("valueTwoLocked"));
	console.log("valueOne: " + jQuery("#display").data("valueOne"));
	console.log("valueTwo: " + jQuery("#display").data("valueTwo"));
	console.log("fromPrevious: " + jQuery("#display").data("fromPrevious"));
};

jQuery(function(){
	
	resetCalculator("0");
	
	jQuery(".num-button").click(function(){
		
		if (jQuery("#display").data("fromPrevious") == true) {
		
			resetCalculator(jQuery(this).text());
			
		} else if ((jQuery("#display").data("isPendingFunction") == true) && (jQuery("#display").data("valueOneLocked") == false)) {
			
			jQuery("#display").data("valueOne", jQuery("#display").val());
			jQuery("#display").data("valueOneLocked", true);
			
			jQuery("#display").val(jQuery(this).text());
			jQuery("#display").data("valueTwo", jQuery("#display").val());
			jQuery("#display").data("valueTwoLocked", true);
		
		// Clicking a number AGAIN, after first number locked and already value for second number	
		} else if ((jQuery("#display").data("isPendingFunction") == true) && (jQuery("#display").data("valueOneLocked") == true)) {

			var curValue = jQuery("#display").val();
			var toAdd = jQuery(this).text();
		
			var newValue = curValue + toAdd;
		
			jQuery("#display").val(newValue);
			
			jQuery("#display").data("valueTwo", jQuery("#display").val());
			jQuery("#display").data("valueTwoLocked", true);
		
		// Clicking on a number fresh	
		} else {
		
			var curValue = jQuery("#display").val();
			if (curValue == "0") {
				curValue = "";
			}
		
			var toAdd = jQuery(this).text();
		
			var newValue = curValue + toAdd;
		
			jQuery("#display").val(newValue);
		
		}
			
	});
	
	jQuery(".clear-button").click(function(){
		resetCalculator("0");
	});
	
	jQuery(".function-button").click(function(){

		if (jQuery("#display").data("fromPrevious") == true) {
			resetCalculator(jQuery("#display").val());
			jQuery("#display").data("valueOneLocked", false);
			jQuery("#display").data("fromPrevious", false)
		}
		
		// Let it be known that a function has been selected
		var pendingFunction = jQuery(this).text();
		jQuery("#display").data("isPendingFunction", true);
		jQuery("#display").data("thePendingFunction", pendingFunction);
		
		// Visually represent the current function
		jQuery(".function-button").removeClass("pendingFunction");
		jQuery(this).addClass("pendingFunction");
	});
	
	jQuery(".equals-button").click(function(){
	
		if ((jQuery("#display").data("valueOneLocked") == true) && (jQuery("#display").data("valueTwoLocked") == true)) {
			
			if (jQuery("#display").data("thePendingFunction") == "+") {
				var finalValue = parseFloat(jQuery("#display").data("valueOne")) + parseFloat(jQuery("#display").data("valueTwo"));
			} else if (jQuery("#display").data("thePendingFunction") == "–") {
				var finalValue = parseFloat(jQuery("#display").data("valueOne")) - parseFloat(jQuery("#display").data("valueTwo"));
			} else if (jQuery("#display").data("thePendingFunction") == "x") {
				var finalValue = parseFloat(jQuery("#display").data("valueOne")) * parseFloat(jQuery("#display").data("valueTwo"));
			} else if (jQuery("#display").data("thePendingFunction") == "/") {
				var finalValue = parseFloat(jQuery("#display").data("valueOne")) / parseFloat(jQuery("#display").data("valueTwo"));
			}
			
			jQuery("#display").val(finalValue);
						
			resetCalculator(finalValue);
			jQuery("#display").data("fromPrevious", true);
						
		} else {
			// both numbers are not locked, do nothing.
		}
		
	});
	
	jQuery("#calculator").draggable();
	
	jQuery("#opener, #closer").click(function(){
		jQuery("#opener").toggle();
		jQuery("#calculator").toggle();
	});
	
});
