var oCoord  = new Array();
var oFont   = new Array();
var oImages = new Array();
var oStatusCaps  = false;
var oStatusShift = false;
var oInputFocus  = "";
var temp         = "";

function definir_teclas()
{
	oFont[0]  = new Array("'", unescape("%22"));
	oFont[1]  = new Array("1", "!");
	oFont[2]  = new Array("2", "@");
	oFont[3]  = new Array("3", "#");
	oFont[4]  = new Array("4", "$");
	oFont[5]  = new Array("5", "%");
	oFont[6]  = new Array("6", "¨");
	oFont[7]  = new Array("7", "&");
	oFont[8]  = new Array("8", "*");
	oFont[9]  = new Array("9", "(");
	oFont[10] = new Array("0", ")");
	oFont[11] = new Array("-", "_");
	oFont[12] = new Array("=", "+");
	oFont[13] = new Array("q", "");
	oFont[14] = new Array("w", "");
	oFont[15] = new Array("e", "");
	oFont[16] = new Array("r", "");
	oFont[17] = new Array("t", "");
	oFont[18] = new Array("y", "");
	oFont[19] = new Array("u", "");
	oFont[20] = new Array("i", "");
	oFont[21] = new Array("o", "");
	oFont[22] = new Array("p", "");
	oFont[23] = new Array("´", "`");
	oFont[24] = new Array("[", "{");
	oFont[25] = new Array("<-", "");
	oFont[26] = new Array("a", "");
	oFont[27] = new Array("s", "");
	oFont[28] = new Array("d", "");
	oFont[29] = new Array("f", "");
	oFont[30] = new Array("g", "");
	oFont[31] = new Array("h", "");
	oFont[32] = new Array("j", "");
	oFont[33] = new Array("k", "");
	oFont[34] = new Array("l", "");
	oFont[35] = new Array("ç", "");
	oFont[36] = new Array("~", "^");
	oFont[37] = new Array("]", "}");
	oFont[38] = new Array("RET", "");
	oFont[39] = new Array("\\", "|");
	oFont[40] = new Array("z", "");
	oFont[41] = new Array("x", "");
	oFont[42] = new Array("c", "");
	oFont[43] = new Array("v", "");
	oFont[44] = new Array("b", "");
	oFont[45] = new Array("n", "");
	oFont[46] = new Array("m", "");
	oFont[47] = new Array(",", "<");
	oFont[48] = new Array(".", ">");
	oFont[49] = new Array(";", ":");
	oFont[50] = new Array("/", "?");
	oFont[51] = new Array("RET", "");
	oFont[52] = new Array("CAPS", "");
	oFont[53] = new Array("CAPS", "");
	oFont[54] = new Array("CAPS", "");
	oFont[55] = new Array("SHIFT", "");
	oFont[56] = new Array("SHIFT", "");
	oFont[57] = new Array("SHIFT", "");
	oFont[58] = new Array(" ", "");
	oFont[59] = new Array(" ", "");
	oFont[60] = new Array(" ", "");
	oFont[61] = new Array(" ", "");
	oFont[62] = new Array(" ", "");
	oFont[63] = new Array(" ", "");
	oFont[64] = new Array(" ", "");
}

function posicao_teclas()
{
	var intLargura = 20;
	var intAltura  = 17;

	var intX1 = 0;
	var intX2 = 0;
	var intY1 = 0;
	var intY2 = 0;
	
	for (var iIndice = 0; iIndice < 65; iIndice++)
	{
		intX2 = intX1 + intLargura;
		intY2 = intY1 + intAltura;

		oCoord[iIndice] = new Array(intX1, intX2, intY1, intY2, oFont[iIndice][0], oFont[iIndice][1]);

		intX1 += intLargura + 2;

		if ((parseInt(iIndice + 1) % 13) == 0 && iIndice != 0)
		{
			intX1  = 0;
			intY1 += intAltura + 2;
		}
	}

	oCoord[38] = Array(264, 284, 38, 74, oFont[38][0], oFont[38][1]);  // ENTER
	oCoord[52] = Array(0, 64, 76, 93, oFont[52][0], oFont[52][1]);     // CAPS LOCK
	oCoord[55] = Array(66, 130, 76, 93, oFont[55][0], oFont[55][1]);   // SHIFT
	oCoord[58] = Array(132, 284, 76, 93, oFont[58][0], oFont[58][1]);  // ESPAÇO

	oCoord[51] = Array(-5, -5, -5, -5, "");
	oCoord[53] = Array(-5, -5, -5, -5, "");
	oCoord[54] = Array(-5, -5, -5, -5, "");
	oCoord[56] = Array(-5, -5, -5, -5, "");
	oCoord[57] = Array(-5, -5, -5, -5, "");
	oCoord[59] = Array(-5, -5, -5, -5, "");
	oCoord[60] = Array(-5, -5, -5, -5, "");
	oCoord[61] = Array(-5, -5, -5, -5, "");
	oCoord[62] = Array(-5, -5, -5, -5, "");
	oCoord[63] = Array(-5, -5, -5, -5, "");
	oCoord[64] = Array(-5, -5, -5, -5, "");
}

function alterar_status(tecla)
{
	switch(tecla)
	{
		case "CAPS":
			if ((oStatusCaps == false) && (oStatusShift == false))
				jQuery("#keyboard img").attr("src", oImages[1].src);
			else if ((oStatusCaps == false) && (oStatusShift == true))
				jQuery("#keyboard img").attr("src", oImages[3].src);
			else if ((oStatusCaps == true) && (oStatusShift == false))
				jQuery("#keyboard img").attr("src", oImages[0].src);
			else if ((oStatusCaps == true) && (oStatusShift == true))
				jQuery("#keyboard img").attr("src", oImages[2].src);
			oStatusCaps = !oStatusCaps;
			break;
		case "SHIFT":
			if ((oStatusCaps == true) && (oStatusShift == true))
				jQuery("#keyboard img").attr("src", oImages[1].src);
			else if ((oStatusCaps == true) && (oStatusShift == false))
				jQuery("#keyboard img").attr("src", oImages[3].src);
			else if ((oStatusCaps == false) && (oStatusShift == true))
				jQuery("#keyboard img").attr("src", oImages[0].src);
			else if ((oStatusCaps == false) && (oStatusShift == false))
				jQuery("#keyboard img").attr("src", oImages[2].src);
			oStatusShift = !oStatusShift;
			break;
	}
}

function atualiza_valor(caracter)
{
	if (parseInt(jQuery("input[name="+ oInputFocus +"]").attr("maxlength")) > parseInt(jQuery("input[name="+ oInputFocus +"]").val().length))
	{
		temp = jQuery("input[name="+ oInputFocus +"]").val();
		temp += caracter;
		jQuery("input[name="+ oInputFocus +"]").val( temp );
	}
	else
		return false;
}

function manipular_teclado(x, y)
{
	if (oInputFocus == "")
		return false;
	
	for (var iIndice = 0; iIndice < oCoord.length; iIndice++)
	{
		if((x >= oCoord[iIndice][0] && x <= oCoord[iIndice][1]) && (y >= oCoord[iIndice][2] && y <= oCoord[iIndice][3]))
		{
			switch (oCoord[iIndice][4])
			{
				case "<-":
					temp = jQuery("input[name="+ oInputFocus +"]").val();
					jQuery("input[name="+ oInputFocus +"]").val( temp.substring(0, parseInt(temp.length)-1) );
					break;
				case "RET":
					jQuery("input.campo").not("input[name="+ oInputFocus +"]").focus();
					break;
				case "CAPS":
					alterar_status("CAPS");
					break;
				case "SHIFT":
					alterar_status("SHIFT");
					break;
				default:
					if ((oStatusCaps == false) && (oStatusShift == false))
					{
						atualiza_valor( oCoord[iIndice][4] );
					}
					else if ((oStatusCaps == true) && (oStatusShift == false))
					{
						atualiza_valor( oCoord[iIndice][4].toUpperCase() );
					}
					else if (((oStatusCaps == true) && (oStatusShift == true)) || ((oStatusCaps == false) && (oStatusShift == true)))
					{
						if (oCoord[iIndice][5] == "")
							atualiza_valor( oCoord[iIndice][4].toUpperCase() );
						else
							atualiza_valor( oCoord[iIndice][5] );
						
						if (oStatusShift)
							alterar_status("SHIFT");
					}
					break;
			}
		}
	}
	jQuery("input[name="+ oInputFocus +"]").focus();
}

jQuery(document).ready(function(){

	oImages[0] = new Image();
	oImages[0].src = "/painel/imagens/teclado/kb.gif";

	oImages[1] = new Image();
	oImages[1].src = "/painel/imagens/teclado/kb-caps.gif";

	oImages[2] = new Image();
	oImages[2].src = "/painel/imagens/teclado/kb-shift.gif";

	oImages[3] = new Image();
	oImages[3].src = "/painel/imagens/teclado/kb-shift-caps.gif";
	
	definir_teclas();
	posicao_teclas();

	var key_top  = parseInt(Math.round(Math.random() * 10));
	var key_left = parseInt(Math.round(Math.random() * 10));

	jQuery("#keyboard").html('<img src="'+ oImages[0].src +'" alt="" style="margin: '+ key_top +'px 0px 0px '+ key_left +'px;" />');
	
	jQuery("#keyboard img").click(function(e){
		var p_x = e.pageX - jQuery(this).offset().left;
		var p_y = e.pageY - jQuery(this).offset().top;
		manipular_teclado(p_x, p_y);
	});

	jQuery("input").focus(function(){
		oInputFocus = jQuery(this).attr("name");
	});
	
	jQuery("input:eq(0)").focus();
});