﻿var operation = {
	'+' : '+',
	'-' : '-',
	'X' : '*',
	'/' : '/'
};

function initCalculator() {
	setStartState();
	
	initResetClick();
	initPercentClick();
	initOperationClick();
	initNumberClick();
	initDotClick();
	initResultClick();
};


function initNumberClick() {
	$('#calculator .number').each(function() {
		$(this).bind('click', function() {
			pressButton(this);
			addDigit($(this).text());
		});
	});
};
function initOperationClick() {
	$('#calculator .operation').each(function() {
		$(this).bind('click', function() {
			pressButton(this);
			performOp(operation[$(this).text()]);
		});
	});	
};
function initResultClick() {
	$('.result').bind('click', function() {
		pressButton(this);
		calc();
	});
};
function initResetClick() {
	$('.reset').bind('click', function() {
		pressButton(this);
		clearDisplay();
	});
};
function initPercentClick() {
	var firstDigit;
	$('.percent').bind('click', function() {
		pressButton(this);
		performPercent();
	});
};

/* On dot click */
function initDotClick() {
	$('.dot').bind('click', function() {
		pressButton(this);
		addDecimalPoint();
	});
};

/* Press button */
function pressButton(button) {
	$(button).css({ 'background-position' : 'bottom left'});
	timer = setTimeout('unpressBotton("' + $(button).attr('id') + '")', 200);
};
function unpressBotton(id) {
	$('#'+id).css({'background-position' : 'top left'});
};









