var operand_keys = "0123456789.";
var operator_keys = "+-*/";
var operand = new Array(2);
var operator = '';
var degrees = true;



var len_limit = 8;

operand[0] = '0';
operand[1] = '';



function keyb_input(e)
{

	var pressed_key;
	var pressed_char;
	e = window.event;
	pressed_key = e.keyCode;

	switch (pressed_key)
	{
		case 190:
		case 110:
				append_it('.');
							break;
		case 8:		del_it();
							break;
		case 13:	calc_it();
							break;
		case 46:	clear_all();
							break;
		case 111:
		case 106:
		case 109:
		case 107:	pressed_char = String.fromCharCode (pressed_key-64);
				set_operator (pressed_char);
							break;
		default:	pressed_char = String.fromCharCode (pressed_key-48);

							if (operand_keys.indexOf(pressed_char) != -1)
							{
								append_it (pressed_char);
							}
							else
							{
								if (operator_keys.indexOf(pressed_char) != -1)
								{
									set_operator (pressed_char);
								}
							}
	}
}

function active_operand()
{
	if (operator)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function draw_it()
{
	display_value = operand[active_operand()] + '';



	value = Math.abs(parseFloat(display_value));
	value_string = Math.abs(parseFloat(display_value)) + '';

	if (value_string.length > len_limit)
	{
		var max_value = 9;
		for (i = 0; i < len_limit - 1; i++)
		{
			max_value = (max_value * 10) + 9;
		}

		if (value > max_value)
		{
			display_value = parseFloat(display_value).toExponential(len_limit - 5)+'';
		}
		else
		{
			digits = len_limit - (value_string.indexOf('.') + 1);
			display_value = Math.round(display_value*Math.pow(10,digits))/Math.pow(10,digits);
		}
	}



	lcd.innerHTML = display_value;
}

function calc_it()
{
	var multiplier = 10;

	if (operator)
	{
		if (operand[1] == '')
		{
			operand[1] = operand[0];
		}
		operand0 = operand[0] + '';
		operand1 = operand[1] + '';

		if (operator == '^')
		{
			operand[0] = Math.pow(operand[0], operand[1]);
		}
		else
		{

			multiplier =  Math.pow (multiplier, Math.max(operand0.substring(operand0.indexOf('.'),operand0.length).length, operand1.substring(operand1.indexOf('.'),operand1.length).length));
			operand[0] = operand[0] * multiplier;
			operand[1] = operand[1] * multiplier;



			operand[0] = eval(operand[0] + operator + operand[1]);					

			switch (operator)
			{
				case '/':		operand[0] = operand[0] + "";
										break;
				case '*':		operand[0] = operand[0] / multiplier / multiplier + "";
										break;
				case '-':		operand[0] = operand[0] / multiplier + "";
										break;
				case '+':		operand[0] = operand[0] / multiplier + "";
									break;
			}
		}
		
		result = operand[0];
		operand[1] = '';
		operand[0] = '';
		operator = '';

		operand[active_operand()] = result;


		draw_it();
	}
}


function del_it()
{
	operand[active_operand()] = operand[active_operand()].substring(0, operand[active_operand()].length - 1);
	if (operand[active_operand()] == "")
	{
		operand[active_operand()] = '0';
	}
	draw_it();
}

function clear_entry()
{
	operand[active_operand()] = '0';
	draw_it();
}

function clear_all()
{
	operand[0] = '0';
	operand[1] = '';
	operator = '';
	draw_it();
}

function append_it(char)
{
	if ((operand[active_operand()] == "0") && (char != '.'))
	{
		if (operand_keys.indexOf(char) != -1)
		{
			operand[active_operand()] = char;
		}
	}
	else
	{
		if ((operand_keys.indexOf(char) != -1) && ((Math.abs(operand[active_operand()]) + '').length < len_limit))
		{
			if ((char != '.') || ((char == '.') && (operand[active_operand()].indexOf('.') == -1)))
			{
				operand[active_operand()] += char;
			}
		}
	}
	draw_it();
}

function set_operator(op)
{
	if ((operator == op) || (operand[active_operand()]))
	{
		calc_it();
	}
	operator = op;
}

function mod_it()
{
	if ((operator == '%') || (operand[active_operand()]))
	{
		calc_it();
	}
	operator = '%';
}

function neg_it()
{
	if (operand[active_operand()].charAt(0) == '-')
	{
		operand[active_operand()] = operand[active_operand()].substring(1, operand[active_operand()].length) + '';
	}
	else
	{
		if (operand[active_operand()] != '0')
		{
			operand[active_operand()] = '-' + operand[active_operand()];
		}
	}
	draw_it();
}

function sqrt_it()
{
	operand[active_operand()] = Math.sqrt(operand[active_operand()]) + "";
	draw_it();
}

function inv_it()
{
	operand[active_operand()] = 1 / eval(operand[active_operand()]) + "";
	draw_it();
}

function log_it()
{
	operand[active_operand()] = Math.log(operand[active_operand()]) / Math.log(10) + "";
	draw_it();
}

function ln_it()
{
	operand[active_operand()] = Math.log(operand[active_operand()]) + "";
	draw_it();
}

function n_it()
{
	operand[active_operand()] = factorial(operand[active_operand()]) + "";
	draw_it();
}

function factorial(x)
{

	if ((x >= 180) || (x != Math.round(x)))
	{
		return 'Error';
	}

	t = 1;

	while (x > 1)
	{
		t *= x--;
	}

	return t;
}


function tan_it()
{
	if (degrees)
		operand[active_operand()] = Math.tan(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.tan(operand[active_operand()]) + "";
	draw_it();
}

function cos_it()
{
	if (degrees)
		operand[active_operand()] = Math.cos(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.sin(operand[active_operand()]) + "";
	draw_it();
}

function sin_it()
{
	if (degrees)
		operand[active_operand()] = Math.sin(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.sin(operand[active_operand()]) + "";
	draw_it();
}


function x2_it()
{
	operand[active_operand()] = Math.pow(operand[active_operand()], 2) + "";
	draw_it();
}


function x3_it()
{
	operand[active_operand()] = Math.pow(operand[active_operand()], 3) + "";
	draw_it();
}


function xy_it(char)
{
	if ((operator == '^') || (operand[active_operand()]))
	{
		calc_it();
	}
	operator = '^';
}

function pi_it(char)
{
	operand[active_operand()] = Math.PI;
	draw_it();
}


function SettingsClosed()
{
	resultsIn = System.Gadget.Settings.read("resultsIn");

	if (resultsIn != '' && resultsIn == 'radians')
	{
		degrees = false;
	}
	else
	{
		degrees = true;		
	}
	
}

function init()
{
	lcd.focus();


	System.Gadget.onDock = checkState;
	System.Gadget.onUndock = checkState;
	System.Gadget.settingsUI = 'settings.html';
	System.Gadget.onSettingsClosed = SettingsClosed;
}

function checkState()
{
	if (!System.Gadget.docked)
	{
		with (document.body.style)
		{
			width = "220px";
			height = "185px";

		}
		document.body.style.background = "url(images/background-big.png) 0px 0px no-repeat";
		len_limit = 14;
		draw_it();
	}
	else
	{
		with (document.body.style)
		{
			width = "135px";
			height = "185px";
			
		}
		document.body.style.background = "url(images/background.png) 0px 0px no-repeat";
		len_limit = 8;
		draw_it();
	}
}
