var capsOn = false;
var shiftOn = false;
var altOn = false;
var keyboard_target;

var keys = {
	220:{"w":14, "h":27, "id":"1_01", "val_l":"ё", "val_u":"Ё"},
	49:{"w":14, "h":27, "id":"1_02", "val_l":"1", "val_u":"!"},
	50:{"w":14, "h":27, "id":"1_03", "val_l":"2", "val_u":"\""},
	51:{"w":14, "h":27, "id":"1_04", "val_l":"3", "val_u":"§"},
	52:{"w":14, "h":27, "id":"1_05", "val_l":"4", "val_u":"$"},
	53:{"w":14, "h":27, "id":"1_06", "val_l":"5", "val_u":"%"},
	54:{"w":14, "h":27, "id":"1_07", "val_l":"6", "val_u":"&"},
	55:{"w":14, "h":27, "id":"1_08", "val_l":"7", "val_u":"/"},
	56:{"w":14, "h":27, "id":"1_09", "val_l":"8", "val_u":"("},
	57:{"w":14, "h":27, "id":"1_10", "val_l":"9", "val_u":")"},
	48:{"w":14, "h":27, "id":"1_11", "val_l":"0", "val_u":"="},
	189:{"w":14, "h":27, "id":"1_12", "val_l":"-", "val_u":"_"},
	187:{"w":14, "h":27, "id":"1_13", "val_l":"=", "val_u":"+"},
	8:{"w":40, "h":27, "id":"1_14", "val_l":String.fromCharCode(8), "val_u":String.fromCharCode(8)},
	
	81:{"w":14, "h":27, "id":"2_01", "val_l":"й", "val_u":"Й", "val_a": "@"},
	87:{"w":14, "h":27, "id":"2_02", "val_l":"ц", "val_u":"Ц"},
	69:{"w":14, "h":27, "id":"2_03", "val_l":"у", "val_u":"У", "val_a":"€"},
	82:{"w":14, "h":27, "id":"2_04", "val_l":"к", "val_u":"К"},
	84:{"w":14, "h":27, "id":"2_05", "val_l":"е", "val_u":"Е"},
	90:{"w":14, "h":27, "id":"2_06", "val_l":"н", "val_u":"Н"},
	85:{"w":14, "h":27, "id":"2_07", "val_l":"г", "val_u":"Г"},
	73:{"w":14, "h":27, "id":"2_08", "val_l":"ш", "val_u":"Ш"},
	79:{"w":14, "h":27, "id":"2_09", "val_l":"щ", "val_u":"Щ"},
	80:{"w":14, "h":27, "id":"2_10", "val_l":"з", "val_u":"З"},
	219:{"w":14, "h":27, "id":"2_11", "val_l":"х", "val_u":"Х"},
	221:{"w":14, "h":27, "id":"2_12", "val_l":"ъ", "val_u":"Ъ"},
	1001:{"w":14, "h":27, "id":"2_13", "val_l":";", "val_u":":"}, // FAKE
	1002:{"w":14, "h":27, "id":"2_14", "val_l":"{", "val_u":"}"}, // FAKE
	//8:{"w":40, "h":27, "id":"2_14"}, // TODO

	20:{"w":14, "h":27, "id":"3_01", "val_l":"", "val_u":""},
	65:{"w":14, "h":27, "id":"3_02", "val_l":"ф", "val_u":"Ф"},
	83:{"w":14, "h":27, "id":"3_03", "val_l":"ы", "val_u":"Ы"},
	68:{"w":14, "h":27, "id":"3_04", "val_l":"в", "val_u":"В"},
	70:{"w":14, "h":27, "id":"3_05", "val_l":"а", "val_u":"А"},
	71:{"w":14, "h":27, "id":"3_06", "val_l":"п", "val_u":"П"},
	72:{"w":14, "h":27, "id":"3_07", "val_l":"р", "val_u":"Р"},
	74:{"w":14, "h":27, "id":"3_08", "val_l":"о", "val_u":"О"},
	75:{"w":14, "h":27, "id":"3_09", "val_l":"л", "val_u":"Л"},
	76:{"w":14, "h":27, "id":"3_10", "val_l":"д", "val_u":"Д"},
	186:{"w":14, "h":27, "id":"3_11", "val_l":"ж", "val_u":"Ж"},
	222:{"w":14, "h":27, "id":"3_12", "val_l":"э", "val_u":"Э"},
	13:{"w":41, "h":28, "id":"3_13", "val_l":String.fromCharCode(13), "val_u":String.fromCharCode(13)},
	
	16:{"w":14, "h":27, "id":"4_01", "val_l":"", "val_u":""},
	89:{"w":14, "h":27, "id":"4_02", "val_l":"я", "val_u":"Я"},
	88:{"w":14, "h":27, "id":"4_03", "val_l":"ч", "val_u":"Ч"},
	67:{"w":14, "h":27, "id":"4_04", "val_l":"с", "val_u":"С"},
	86:{"w":14, "h":27, "id":"4_05", "val_l":"м", "val_u":"М"},
	66:{"w":14, "h":27, "id":"4_06", "val_l":"и", "val_u":"И"},
	78:{"w":14, "h":27, "id":"4_07", "val_l":"т", "val_u":"Т"},
	77:{"w":14, "h":27, "id":"4_08", "val_l":"ь", "val_u":"Ь"},
	188:{"w":14, "h":27, "id":"4_09", "val_l":"б", "val_u":"Б"},
	190:{"w":14, "h":27, "id":"4_10", "val_l":"ю", "val_u":"Ю"},
	191:{"w":14, "h":27, "id":"4_11", "val_l":".", "val_u":","},
	16:{"w":14, "h":27, "id":"4_12", "val_l":"", "val_u":""},


	"1F":{"w":83, "h":27, "id":"5_01", "val_l":"", "val_u":""},
	226:{"w":14, "h":27, "id":"5_02", "val_l":"<", "val_u":">"},
	"4F":{"w":14, "h":27, "id":"5_03", "val_l":"´", "val_u":"`"},
	"5F":{"w":14, "h":27, "id":"5_04", "val_l":"]", "val_u":"["},
	32:{"w":14, "h":27, "id":"5_05", "val_l":" ", "val_u":" "},
	18:{"w":14, "h":27, "id":"5_06", "val_l":"/", "val_u":"?"},
	"3F":{"w":14, "h":27, "id":"5_07", "val_l":"\\", "val_u":"|"},
	"2F":{"w":83, "h":27, "id":"5_08", "val_l":"", "val_u":""}
};
	
function Keyboard(On, Off)
{
	this.lastKey = new String();
	this.OnFunc = On;
	this.OffFunc = Off;
}

InsertText = function(obj, text)
{	
	var r, ss, se;
	var isOpera = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
	var verOpera = (isOpera) ? navigator.userAgent.substring(navigator.userAgent.indexOf('Opera') + 6, navigator.userAgent.length) : false;
	
	var is_moz = (typeof(obj.selectionStart) != 'undefined'); // Mozilla
	var is_ie = (typeof(document.selection) != 'undefined'); // IE
	
	switch (text) 
	{
		case String.fromCharCode(8): // special backspace handler
		{
			if (is_moz) 
			{
				// get caret/selection position
				ss = obj.selectionStart;
				se = obj.selectionEnd;

				// if don't selected, expand selection for 1 char left
				if (ss == se) --ss;

				// insert/replace text (to caret position)/(of selection)
				obj.value = obj.value.substring(0, ss) + obj.value.substring(se);

				// move caret to new position
				obj.setSelectionRange(ss, ss);
			} 
			else if (is_ie) 
			{
				obj.focus();

				r = document.selection.createRange();
				if (r.text == '') 
				{
					// if don't selected, expand selection for 1 char left
					r.moveStart('character', -1);
				}
				r.text = '';
				r.moveStart('character', 0);
				r.select();
			} 
			else 
			{ // default action
				obj.value = obj.value.substr(0, obj.value.length - 1);
			}
			break;
		}
		default:
		{
			if(isOpera && parseFloat(verOpera) > 8) 
			{ // Opera
				obj.value += text;
			}
			else if (is_moz) 
			{
				// get caret/selection position
				ss = obj.selectionStart;
				se = obj.selectionEnd;

				// insert/replace text (to caret position)/(of selection)
				obj.value = obj.value.substring(0, ss) + text + obj.value.substring(se);

				// new caret position
				ss += text.length;

				// move caret to new position
				obj.setSelectionRange(ss, ss);
			} 
			else if (is_ie) 
			{ // IEi
				obj.focus();
				r = document.selection.createRange();
				r.text = text;
				r.moveStart('character', 0);
				r.select();
			}
			else 
			{ // default action
				obj.value += text;
			}
		}
	}
}

Keyboard.prototype.PerformKeyClick = function(Event, Elem)
{
	var targ_id;
	if(!Event)
		var Event = window.event;
	
	if(Event.target) 
		targ = Event.target;
	else if(Event.srcElement) 
		targ = Event.srcElement;
	
	//var targ = Event.element();	

	if(targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	
	if(targ.id == "4_01")
		targ_id = "4_12";
	else
		targ_id = targ.id;

	for(var key in keys)
	{
		if(keys[key] && keys[key]["id"] == targ_id)
		{
			if(keys[key]["val_u"] == "")
			{
				switch(key)
				{
					case "16":
						if(!shiftOn)
						{
							shiftOn = true;
							this.OnFunc("4_01");
							this.OnFunc("4_12");
						}
						else
						{
							shiftOn = false;
							this.OffFunc("4_01");
							this.OffFunc("4_12");
						}
						break;
					case "20":
						if(!capsOn)
						{
							capsOn = true;
							this.OnFunc("3_01");
						}
						else
						{
							capsOn = false;
							this.OffFunc("3_01");
						}
						break;
					case "2F":
						Elem.value = "";
						break;
					case "1F":
						Form.Element.select(Elem);
						break;

				}
			}
			else
			{
				sym = (shiftOn || capsOn)?keys[key]["val_u"]:keys[key]["val_l"];
				if(altOn)
					sym = keys[key]["val_a"];
				
				InsertText(Elem, sym);
				shiftOn = false;
				altOn = false;
				this.OffFunc("4_01");
				this.OffFunc("4_12");
				this.OffFunc("5_06");
			}
		}
	}
	
	//Elem.blur();
	//Elem.focus();
}

Keyboard.prototype.AddEvent = function(Elem, EventName, Func, Capture)
{
/*
	if(Elem.addEventListener)
		Elem.addEventListener(Event, Func, Capture);
	else if(Elem.attachEvent)
		Elem.attachEvent("on" + Event, Func);
*/	
	Event.observe(Elem, EventName, Func, Capture);
}

Keyboard.prototype.Register = function(Elem)
{
	var self = this;
	var func = function (Event)
	{
		self.PerformKeyDown(Event);
	}

	this.AddEvent(Elem, "keydown", func, true);
	
	var func = function (Event)
	{
		self.PerformKeyUp(Event);
	}
	
	this.AddEvent(Elem, "keyup", func, true);
	
	var func = function (Event) 
	{
		self.PerformKeyClick(Event, Elem);
	}
	
	for(var key in keys)
	{
		var img = document.getElementById(keys[key]["id"]);
		this.AddEvent(img, "click", func, true); 
		//var img = new Image(key["w"], key["h"]);
	}

	//Elem.onkeyup = this.PerformKeyUp;
	window.oncapslock = function (on1) 
	{
		if(on1)
		{
			self.OnFunc("3_01"); //keys[20]["id"]);
		}
		else
		{
			self.OffFunc("3_01"); //keys[20]["id"]);
		}

		capsOn = on1;
	};
	//Elem.addEventListener("focus", this.PerformKeyDown, false);
}

Keyboard.prototype.PerformKeyDown = function(Event)
{

	if(!Event)
		var Event = window.event;
	
	if(this.lastKey == 18 || this.lastKey == 16)
	{
		this.lastKey = Event.keyCode;
		return;
	}

	if(Event.keyCode != 20 && keys[Event.keyCode])
		this.OnFunc(keys[Event.keyCode]["id"]);

	if(Event.keyCode == 16)
	{
		this.OnFunc("4_12");
		shiftOn = true;
	}
	
				//document.send_sms.code.value = Event.keyCode;
	//document.send_sms.code.value = keys[Event.keyCode]["id"];
	//document.send_sms.code.value = Event.keyCode;
	
	this.lastKey = Event.keyCode;
}

Keyboard.prototype.PerformKeyUp = function(Event)
{
	if(!Event)
		var Event = window.event;
	
	if(Event.keyCode != 20 && keys[Event.keyCode])
		this.OffFunc(keys[Event.keyCode]["id"]);
	
	this.OffFunc("4_01");
	this.OffFunc("4_12");
	shiftOn = false;
}
