/*
jsKeyboard code for QL configgenerator
made by Lashknife <lashknife@gmail.com>
"Learn from this document, do not copy it!"
*/

$(document).ready(function() {
	if (typeof myBezier == "undefined") {
		myBezier = new Bezier();
	}
	// top row
	jskeys['esc'] = new jsKey('esc','Esc',0,0);
	jskeys['f1'] = new jsKey('f1','F1',68,0);
	jskeys['f2'] = new jsKey('f2','F2',102,0);
	jskeys['f3'] = new jsKey('f3','F3',136,0);
	jskeys['f4'] = new jsKey('f4','F4',170,0);
	jskeys['f5'] = new jsKey('f5','F5',220,0);
	jskeys['f6'] = new jsKey('f6','F6',254,0);
	jskeys['f7'] = new jsKey('f7','F7',288,0);
	jskeys['f8'] = new jsKey('f8','F8',322,0);
	jskeys['f9'] = new jsKey('f9','F9',374,0);
	jskeys['f10'] = new jsKey('f10','F10',408,0);
	jskeys['f11'] = new jsKey('f11','F11',442,0);
	jskeys['f12'] = new jsKey('f12','F12',476,0);
	// number row
	jskeys['backquote'] = new jsKey('backquote','`',0,44);
	jskeys['one'] = new jsKey('one','1',34,44);
	jskeys['two'] = new jsKey('two','2',68,44);
	jskeys['three'] = new jsKey('three','3',102,44);
	jskeys['four'] = new jsKey('four','4',136,44);
	jskeys['five'] = new jsKey('five','5',170,44);
	jskeys['six'] = new jsKey('six','6',204,44);
	jskeys['seven'] = new jsKey('seven','7',238,44);
	jskeys['eight'] = new jsKey('eight','8',272,44);
	jskeys['nine'] = new jsKey('nine','9',306,44);
	jskeys['zero'] = new jsKey('zero','0',340,44);
	jskeys['minus'] = new jsKey('minus','-',374,44);
	jskeys['equals'] = new jsKey('equals','=',408,44);
	jskeys['backspace'] = new jsKey('backspace','Backspace',442,44,34,0);
	// tab row
	jskeys['tab'] = new jsKey('tab','Tab',0,78,17,0);
	jskeys['q'] = new jsKey('q','Q',51,78);
	jskeys['w'] = new jsKey('w','W',85,78);
	jskeys['e'] = new jsKey('e','E',119,78);
	jskeys['r'] = new jsKey('r','R',153,78);
	jskeys['t'] = new jsKey('t','T',187,78);
	jskeys['y'] = new jsKey('y','Y',221,78);
	jskeys['u'] = new jsKey('u','U',255,78);
	jskeys['i'] = new jsKey('i','I',289,78);
	jskeys['o'] = new jsKey('o','O',323,78);
	jskeys['p'] = new jsKey('p','P',357,78);
	jskeys['lbracket'] = new jsKey('lbracket','[',391,78);
	jskeys['rbracket'] = new jsKey('rbracket',']',425,78);
	jskeys['backslash'] = new jsKey('backslash','\\',459,78,17,0);
	// capslock row
	jskeys['capslock'] = new jsKey('capslock','Caps Lock',0,112,26,0);
	$("#jskeycapslock").css({lineHeight:"15px"});
	jskeys['a'] = new jsKey('a','A',60,112);
	jskeys['s'] = new jsKey('s','S',94,112);
	jskeys['d'] = new jsKey('d','D',128,112);
	jskeys['f'] = new jsKey('f','F',162,112);
	jskeys['g'] = new jsKey('g','G',196,112);
	jskeys['h'] = new jsKey('h','H',230,112);
	jskeys['j'] = new jsKey('j','J',264,112);
	jskeys['k'] = new jsKey('k','K',298,112);
	jskeys['l'] = new jsKey('l','L',332,112);
	jskeys['semicolon'] = new jsKey('semicolon',';',366,112);
	jskeys['quote'] = new jsKey('quote','\'',400,112);
	jskeys['enter'] = new jsKey('enter','Enter',434,112,42,0);
	// shift row
	jskeys['lshift'] = new jsKey('lshift','Shift',0,146,42,0);
	jskeys['z'] = new jsKey('z','Z',76,146);
	jskeys['x'] = new jsKey('x','X',110,146);
	jskeys['c'] = new jsKey('c','C',144,146);
	jskeys['v'] = new jsKey('v','V',178,146);
	jskeys['b'] = new jsKey('b','B',212,146);
	jskeys['n'] = new jsKey('n','N',246,146);
	jskeys['m'] = new jsKey('m','M',280,146);
	jskeys['comma'] = new jsKey('comma',',',314,146);
	jskeys['period'] = new jsKey('period','.',348,146);
	jskeys['slash'] = new jsKey('slash','/',382,146);
	jskeys['rshift'] = new jsKey('rshift','Shift',416,146,60,0);
	// ctrl row
	jskeys['lctrl'] = new jsKey('lctrl','Ctrl',0,180,17,0);
	jskeys['lwin'] = new jsKey('lwin','Win',51,180,9,0);
	jskeys['alt'] = new jsKey('alt','Alt',94,180,9,0);
	jskeys['space'] = new jsKey('space','Space',137,180,159,0);
	jskeys['altgr'] = new jsKey('altgr','Alt Gr',330,180,9,0);
	$("#jskeyaltgr").css({lineHeight:"15px"});
	jskeys['rwin'] = new jsKey('rwin','Win',373,180,9,0);
	jskeys['menu'] = new jsKey('menu','Menu',416,180,9,0);
	jskeys['rctrl'] = new jsKey('rctrl','Ctrl',459,180,17,0);
	// special block
	jskeys['printscreen'] = new jsKey('printscreen','Prnt Scrn',528,0);
	$("#jskeyprintscreen").css({fontSize:"10px", lineHeight:"15px"});
	jskeys['scrolllock'] = new jsKey('scrolllock','Scroll Lock',562,0);
	$("#jskeyscrolllock").css({fontSize:"9px", lineHeight:"15px"});
	jskeys['pause'] = new jsKey('pause','Pause',596,0);
	$("#jskeypause").css({fontSize:"10px"});
	// nav block
	jskeys['insert'] = new jsKey('insert','Ins',528,44);
	$("#jskeyinsert").css({fontSize:"10px"});
	jskeys['home'] = new jsKey('home','Home',562,44);
	$("#jskeyhome").css({fontSize:"10px"});
	jskeys['pgup'] = new jsKey('pgup','PgUp',596,44);
	$("#jskeypgup").css({fontSize:"10px"});
	jskeys['del'] = new jsKey('del','Del',528,78);
	$("#jskeydelete").css({fontSize:"10px"});
	jskeys['end'] = new jsKey('end','End',562,78);
	$("#jskeyend").css({fontSize:"10px"});
	jskeys['pgdn'] = new jsKey('pgdn','PgDn',596,78);
	$("#jskeypgdn").css({fontSize:"10px"});
	// arrow block
	jskeys['arrowup'] = new jsKey('arrowup','Up',562,146);
	$("#jskeyarrowup").css({fontSize:"10px"});
	jskeys['arrowleft'] = new jsKey('arrowleft','Left',528,180);
	$("#jskeyarrowleft").css({fontSize:"10px"});
	jskeys['arrowdown'] = new jsKey('arrowdown','Down',562,180);
	$("#jskeyarrowdown").css({fontSize:"10px"});
	jskeys['arrowright'] = new jsKey('arrowright','Right',598,180);
	$("#jskeyarrowright").css({fontSize:"10px"});
	// numpad
	jskeys['numlock'] = new jsKey('numlock','Num Lock',646,44);
	$("#jskeynumlock").addClass("keypadkey").css({fontSize:"10px", lineHeight:"15px"});
	jskeys['kpslash'] = new jsKey('kpslash','/',680,44);
	$("#jskeykpslash").addClass("keypadkey");
	jskeys['kpasterisk'] = new jsKey('kpasterisk','*',714,44);
	$("#jskeykpasterisk").addClass("keypadkey");
	jskeys['kpminus'] = new jsKey('kpminus','-',748,44);
	$("#jskeykpminus").addClass("keypadkey");
	jskeys['kp7'] = new jsKey('kp7','7',646,78);
	$("#jskeykp7").addClass("keypadkey");
	jskeys['kp8'] = new jsKey('kp8','8',680,78);
	$("#jskeykp8").addClass("keypadkey");
	jskeys['kp9'] = new jsKey('kp9','9',714,78);
	$("#jskeykp9").addClass("keypadkey");
	jskeys['kpplus'] = new jsKey('kpplus','+',748,78,0,34);
	$("#jskeykpplus").addClass("keypadkey").css({lineHeight:"64px"});
	jskeys['kp4'] = new jsKey('kp4','4',646,112);
	$("#jskeykp4").addClass("keypadkey");
	jskeys['kp5'] = new jsKey('kp5','5',680,112);
	$("#jskeykp5").addClass("keypadkey");
	jskeys['kp6'] = new jsKey('kp6','6',714,112);
	$("#jskeykp6").addClass("keypadkey");
	jskeys['kp1'] = new jsKey('kp1','1',646,146);
	$("#jskeykp1").addClass("keypadkey");
	jskeys['kp2'] = new jsKey('kp2','2',680,146);
	$("#jskeykp2").addClass("keypadkey");
	jskeys['kp3'] = new jsKey('kp3','3',714,146);
	$("#jskeykp3").addClass("keypadkey");
	jskeys['kpenter'] = new jsKey('kpenter','Enter',748,146,0,34);
	$("#jskeykpenter").addClass("keypadkey").css({fontSize:"10px", lineHeight:"62px"});
	jskeys['kp0'] = new jsKey('kp0','0',646,180,34,0);
	$("#jskeykp0").addClass("keypadkey");
	jskeys['kpdel'] = new jsKey('kpdel','Del',714,180);
	$("#jskeykpdel").addClass("keypadkey");
	// mouse
	jskeys['mouse1'] = new jsKey('mouse1','M1',667,54,0,34);
	$("#jskeymouse1").addClass("mousebtn").css({ lineHeight:"62px" });
	jskeys['mouse2'] = new jsKey('mouse2','M2',727,54,0,34);
	$("#jskeymouse2").addClass("mousebtn").css({ lineHeight:"62px" });
	jskeys['mwheelup'] = new jsKey('mwheelup','Up',701,44,-8,0);
	$("#jskeymwheelup").addClass("mousebtn");
	jskeys['mouse3'] = new jsKey('mouse3','M3',701,76,-8,-8);
	$("#jskeymouse3").addClass("mousebtn").css({ lineHeight:"24px" });
	jskeys['mwheeldown'] = new jsKey('mwheeldown','Dn',701,100,-8,0);
	$("#jskeymwheeldown").addClass("mousebtn");
	jskeys['mouse4'] = new jsKey('mouse4','M4',656,122,-8,-4);
	$("#jskeymouse4").addClass("mousebtn").css({ lineHeight:"28px" });;
	jskeys['mouse5'] = new jsKey('mouse5','M5',656,152,-8,-4);
	$("#jskeymouse5").addClass("mousebtn").css({ lineHeight:"28px" });;
	jskeys['mouse6'] = new jsKey('mouse6','M6',744,122,-8,-4);
	$("#jskeymouse6").addClass("mousebtn").css({ lineHeight:"28px" });;
	jskeys['mouse7'] = new jsKey('mouse7','M7',744,152,-8,-4);
	$("#jskeymouse7").addClass("mousebtn").css({ lineHeight:"28px" });;
	
	// keypad vs mouse selector
	$(document.createElement("div")).attr("id","jskeypadvsmouse").appendTo($("#jskeyboard"));
	$("#jskeypadvsmouse")[0].innerHTML = '<input type="radio" name="keypadvsmouse" id="togglekeypad" onclick="toggleKeypadMouse(\'keypad\')" /><input type="radio" name="keypadvsmouse" id="togglemouse" onclick="toggleKeypadMouse(\'mouse\')" /><br /><label for="togglekeypad">Keypad</label><label for="togglemouse">Mouse</label>';
	
	for (var key in jskeys) {
		if (key != "rctrl" && key != "rshift" && key != "rwin") {
			$("#jskey" + key).draggable({
				opacity : 0.80,
				zIndex : 2,
				start : function (event, ui) {
					var objid = $(this).attr("id").substr(5);
					var offset = getOffset($("#" + $(this).attr("id"))[0]);
					jskeys[objid].x = offset.x;
					jskeys[objid].y = offset.y;
				},					
				stop : function(event, ui) {
					var objid = $(this).attr("id").substr(5);
					var offset = getOffset($("#" + $(this).attr("id"))[0]);
					var xyslider = new DivSliderXY($(this).attr("id"),jskeys[objid].l,offset.x-jskeys[objid].x+jskeys[objid].l,jskeys[objid].t,offset.y-jskeys[objid].y+jskeys[objid].t);
					xyslider.slide();
				}
			});
			$("#jskey" + key).attr("title","currently not bound");
		} else {
			$("#jskey" + key).css({ borderColor:"#999999", color:"#999999", cursor:"auto" });
		}
	}
	$(".keybind").droppable({
		accept: ".jskey",
		hoverClass: "dropHover",
		tolerance: "pointer",
		drop: function(ev, ui) {
			doBind($(".ui-draggable-dragging"),$(this));
		}
	});

	jskeyboardfader = new DivFader("jskeyboardcontainer");
	jskeyboardfader.useIEalpha = false;
	jskeyboardslider = new DivSlider("jskeyboardcontainer",232,-232);
	
	toggleKeyboard();
	$("#togglemouse")[0].click();
});
function doBind(dragger,dropper) {
	if (!dropper.attr("disabled")) {
		draggerid = dragger.attr("id").substr(5); // cut "jskey" from key id
		dropperid = dropper.attr("id").substr(5); // cut "menu_" from input id
		// if key was bound to something, clear it
		if (jskeys[draggerid].boundto != "") {
			$("#bind_" + jskeys[draggerid].boundto).val("");
			qlbinds[jskeys[draggerid].boundto].boundto = "";
		}
		// if bind was set by a key before, reset that key
		if (qlbinds[dropperid].boundto != "") {
			$("#jskey" + qlbinds[dropperid].boundto).css({ borderColor:"#000000", color:"#000000" }).attr("title","Currently not bound");
			jskeys[qlbinds[dropperid].boundto].boundto = "";
		}
		// bind new key
		dropper.val(gamekeys[jskeys[draggerid].id]);
		catcolor = bindCategories[qlbinds[dropperid].cat];
		dragger.css({ color: catcolor, borderColor : catcolor });
		dragger.attr("title","bind \"" + gamekeys[jskeys[draggerid].id] + "\" \"" + qlbinds[dropperid].v + "\"");
		jskeys[draggerid].boundto = dropperid;
		qlbinds[dropperid].boundto = draggerid;
	}
}
function doManualBind(draggerid,dropperid) {
	// if key was bound to something, clear it
	if (jskeys[draggerid].boundto != "") {
		$("#bind_" + jskeys[draggerid].boundto).val("");
		qlbinds[jskeys[draggerid].boundto].boundto = "";
	}
	// if bind was set by a key before, reset that key
	if (qlbinds[dropperid].boundto != "") {
		$("#jskey" + qlbinds[dropperid].boundto).css({ borderColor:"#000000", color:"#000000" }).attr("title","currently not bound");
		jskeys[qlbinds[dropperid].boundto].boundto = "";
	}
	// bind new key
	$("#bind_" + dropperid).val(gamekeys[jskeys[draggerid].id]);
	catcolor = bindCategories[qlbinds[dropperid].cat];
	$("#jskey" + draggerid).css({ color: catcolor, borderColor : catcolor });
	document.getElementById("jskey" + draggerid).title = "bind \"" + gamekeys[jskeys[draggerid].id] + "\" \"" + qlbinds[dropperid].v + "\"";
	//$("#jskey" + draggerid).attr("title","bind \"" + gamekeys[jskeys[draggerid].id] + "\" \"" + qlbinds[dropperid].v + "\"");
	jskeys[draggerid].boundto = dropperid;
	qlbinds[dropperid].boundto = draggerid;
}
var gamekeys = {
	esc:"ESCAPE", f1:"F1", f2:"F2", f3:"F3", f4:"F4", f5:"F5", f6:"F6", f7:"F7", f8:"F8", f9:"F9", f10:"F10", f11:"F11", f12:"F12",
	backquote:"`", one:"1", two:"2", three:"3", four:"4", five:"5", six:"6", seven:"7", eight:"8", nine:"9", zero:"0", minus:"-", equals:"=", backspace:"BACKSPACE",
	tab:"TAB", q:"Q", w:"W", e:"E", r:"R", t:"T", y:"Y", u:"U", i:"I", o:"O", p:"P", lbracket:"[", rbracket:"]", backslash:"\\",
	capslock:"CAPSLOCK", a:"A", s:"S", d:"D", f:"F", g:"G", h:"H", j:"J", k:"K", l:"L", semicolon:";", quote:"'", enter:"ENTER",
	lshift:"SHIFT", z:"Z", x:"X", c:"C", v:"V", b:"B", n:"N", m:"M", comma:",", period:".", slash:"/", rshift:"SHIFT",
	lctrl:"CTRL", lwin:"WIN", alt:"ALT", space:"SPACE", altgr:"ALTGR", rwin:"WIN", menu:"MENU", rctrl:"CTRL",
	printscreen:"PRINTSCREEN", scrolllock:"SCROLLLOCK", pause:"PAUSE",
	insert:"INS", del:"DELETE", home:"HOME", end:"END", pgup:"PGUP", pgdn:"PGDN",
	arrowup:"UPARROW", arrowleft:"LEFTARROW", arrowdown:"DOWNARROW", arrowright:"RIGHTARROW",
	numlock:"NUMLOCK", kpslash:"KP_SLASH", kpasterisk:"*", kpminus:"KP_MINUS", kpplus:"KP_PLUS", kpenter:"KP_ENTER", kpdel:"KP_DEL", kp0:"KP_INS", kp1:"KP_END", kp2:"KP_DOWNARROW", kp3:"KP_PGDN", kp4:"KP_LEFTARROW", kp5:"KP_5", kp6:"KP_RIGHTARROW", kp7:"KP_HOME", kp8:"KP_UPARROW", kp9:"KP_PGUP",
	mouse1:"MOUSE1", mouse2:"MOUSE2", mouse3:"MOUSE3", mouse4:"MOUSE4", mouse5:"MOUSE5", mouse6:"MOUSE6", mouse7:"MOUSE7", mwheelup:"MWHEELUP", mwheeldown:"MWHEELDOWN"
}
var qlbinds = {
	// general
	menu : new KeyBind('togglemenu','general'),
	console : new KeyBind('toggleconsole','general'),
	scores : new KeyBind('+scores','general'),
	vote_yes : new KeyBind('vote yes','general'),
	vote_no : new KeyBind('vote no','general'),
	readyup : new KeyBind('readyup','general'),
	// screenshot/demo
	screenshot : new KeyBind('screenshot','ssdd'),
	toggle_record : new KeyBind('SCRIPT','ssdd'),
	start_record : new KeyBind('record','ssdd'),
	stop_record : new KeyBind('stoprecord','ssdd'),
	// movement
	forward : new KeyBind('+forward','movement'),
	back : new KeyBind('+back','movement'),
	moveleft : new KeyBind('+moveleft','movement'),
	moveright : new KeyBind('+moveright','movement'),
	moveup : new KeyBind('+moveup','movement'),
	movedown : new KeyBind('+movedown','movement'),
	speed : new KeyBind('+speed','movement'),
	// action
	fire : new KeyBind('+attack','action'),
	zoom : new KeyBind('+zoom','action'),
	previous_weapon : new KeyBind('weapprev','action'),
	next_weapon : new KeyBind('weapnext','action'),
	use : new KeyBind('+button2','action'),
	dropweapon : new KeyBind('','action'),
	dropflag : new KeyBind('dropflag','action'),
	timein : new KeyBind('timein','competition'),
	timeout : new KeyBind('timeout','competition'),
	// other
	say : new KeyBind('messagemode','other'),
	say_team : new KeyBind('messagemode2','other'),
	chat : new KeyBind('+chat','other'),
	taunt : new KeyBind('+button3','other'),
	team_red : new KeyBind('team r','other'),
	team_blue : new KeyBind('team b','other'),
	spectate : new KeyBind('team spectate','other'),
	sizedown : new KeyBind('sizedown','other'),
	sizeup : new KeyBind('sizeup','other'),
	quit : new KeyBind('quit','other'),
	ragequit : new KeyBind('ragequit','other'),
	// weapons
	weapon1_bind1 : new KeyBind('weapon 1','weapon'),
	weapon1_bind2 : new KeyBind('weapon 1','weapon2'),
	weapon2_bind1 : new KeyBind('weapon 2','weapon'),
	weapon2_bind2 : new KeyBind('weapon 2','weapon2'),
	weapon3_bind1 : new KeyBind('weapon 3','weapon'),
	weapon3_bind2 : new KeyBind('weapon 3','weapon2'),
	weapon4_bind1 : new KeyBind('weapon 4','weapon'),
	weapon4_bind2 : new KeyBind('weapon 4','weapon2'),
	weapon5_bind1 : new KeyBind('weapon 5','weapon'),
	weapon5_bind2 : new KeyBind('weapon 5','weapon2'),
	weapon6_bind1 : new KeyBind('weapon 6','weapon'),
	weapon6_bind2 : new KeyBind('weapon 6','weapon2'),
	weapon7_bind1 : new KeyBind('weapon 7','weapon'),
	weapon7_bind2 : new KeyBind('weapon 7','weapon2'),
	weapon8_bind1 : new KeyBind('weapon 8','weapon'),
	weapon8_bind2 : new KeyBind('weapon 8','weapon2'),
	weapon9_bind1 : new KeyBind('weapon 9','weapon'),
	weapon9_bind2 : new KeyBind('weapon 9','weapon2'),
	weapon11_bind1 : new KeyBind('weapon 11','weapon'),
	weapon11_bind2 : new KeyBind('weapon 11','weapon2'),
	weapon12_bind1 : new KeyBind('weapon 12','weapon'),
	weapon12_bind2 : new KeyBind('weapon 12','weapon2'),
	weapon13_bind1 : new KeyBind('weapon 13','weapon'),
	weapon13_bind2 : new KeyBind('weapon 13','weapon2'),
	// custom chat
	chat1_bind : new KeyBind('say(team) your-message','chat'),
	chat2_bind : new KeyBind('say(team) your-message','chat'),
	chat3_bind : new KeyBind('say(team) your-message','chat'),
	chat4_bind : new KeyBind('say(team) your-message','chat'),
	chat5_bind : new KeyBind('say(team) your-message','chat'),
	chat6_bind : new KeyBind('say(team) your-message','chat'),
	chat7_bind : new KeyBind('say(team) your-message','chat'),
	chat8_bind : new KeyBind('say(team) your-message','chat'),
	chat9_bind : new KeyBind('say(team) your-message','chat'),
	chat10_bind : new KeyBind('say(team) your-message','chat'),
	chat11_bind : new KeyBind('say(team) your-message','chat'),
	chat12_bind : new KeyBind('say(team) your-message','chat'),
	chat13_bind : new KeyBind('say(team) your-message','chat'),
	chat14_bind : new KeyBind('say(team) your-message','chat'),
	chat15_bind : new KeyBind('say(team) your-message','chat'),
	chat16_bind : new KeyBind('say(team) your-message','chat'),
	chat17_bind : new KeyBind('say(team) your-message','chat'),
	chat18_bind : new KeyBind('say(team) your-message','chat'),
	chat19_bind : new KeyBind('say(team) your-message','chat'),
	chat20_bind : new KeyBind('say(team) your-message','chat')
}

var bindCategories = { general : "lime", ssdd : "blue", movement : "orange", action : "red", weapon : "teal", weapon2 : "cyan", other : "magenta", chat : "gold", competition : "purple" }

function KeyBind (v,cat) {
	this.v = v;
	this.cat = cat;
	this.boundto = ""; // what key bound this action (I allow 1on1 binding)
}

var jskeys = new Object();
function jsKey (id,label,l,t,extraW,extraH) {
	this.id = id;
	this.label = label;
	this.w = 30 + (extraW || 0);
	this.h = 30 + (extraH || 0);
	this.l = l;
	this.t = t;
	//immediate creation upon creating instance
	$(document.createElement("div")).attr("id","jskey"+this.id).text(this.label).addClass("jskey").css({ position:"absolute", width : this.w + "px", height : this.h + "px", left : this.l + "px", top : this.t + "px" }).appendTo($("#jskeyboard"));
	// assign original offset values
	//alert($("#jskey" + this.id)[0].style.position);
	this.x = getOffset($("#jskey" + this.id)[0]).x;
	this.y = getOffset($("#jskey" + this.id)[0]).y;
	this.boundto = ""; // id of the action that this key is bound to. A key can be bound to 1 action at the same time.
	
}
function getOffset(o){
	for(var r = {x: o.offsetLeft, y: o.offsetTop, h: o.offsetHeight, w: o.offsetWidth}; o = o.offsetParent; r.x += o.offsetLeft, r.y += o.offsetTop);
	return r;
}

jskeyboardopen = true;
var jskeyboardfader,jskeyboardslider;
function toggleKeyboard() {
	if (jskeyboardopen) { // keyboard open: hide it
		jskeyboardfader.fadeOut();
	} else { // keyboard closed: unhide it
		jskeyboardfader.fadeIn();
	}
	jskeyboardslider.slide();
	jskeyboardopen = !jskeyboardopen; // switch status
}
function toggleKeypadMouse(what) {
	if (what == "mouse") {
		$(".keypadkey").css({ visibility:"hidden" });
		$(".mousebtn").css({ visibility:"visible" });
	} else if (what == "keypad") {
		$(".keypadkey").css({ visibility:"visible" });
		$(".mousebtn").css({ visibility:"hidden" });
	}
}

function checkBind(inputfield) {
	wasboundto = qlbinds[inputfield.id.substr(5)].boundto;
	// not empty, find a match?
	matchfound = false;
	for (var key in gamekeys) {
		if (inputfield.value.toUpperCase() == gamekeys[key]) {
			doManualBind(key,inputfield.id.substr(5));
			matchfound = true;
			break;
		}
	}
	if (!matchfound) {
		if (inputfield.value != "") {
			alert("Invalid bind!");
		}
		inputfield.value = ""; // clearing the invalid entry
		unbindKey(wasboundto); // unbinding the previously bound key
	}
}
function unbindKey(key) {
	if (key != "") {
		$("#jskey" + key).css({ color:"#000000", borderColor:"#000000" }).attr("title","Currently not bound");
		jskeys[key].boundto = "";
	}
}