function setInits() {
	sizWindowX = document.documentElement.clientWidth;

	if (navigator.appName == "Opera") {
		sizWindowY = document.body.clientHeight;
	} else {
		sizWindowY = document.documentElement.clientHeight;
	}

	setSize(divSite,sizWindowX,sizWindowY);
	setPosition(divContainer,"center","center");
}
function setGlobals() {
	divSite = document.getElementById("site");
	divContainer = document.getElementById("container");
}

function setKeyboard() { /* set behaviour of the keyboard */

	$(".key").hover(function(){
		$(this).css("background-color","#FF0000");
	},function(){
		$(this).css("background-color","transparent");	
	}).click(function(event){
		if (this.id.substr(0,8) != "keyShift") {
			enterChar(this.id.substr(3),event);
		}
	})
	
	$("#typefield").keydown(function(event) {
		if (event.keyCode != 46 && event.ctrlKey != true) {
			enterChar(event.keyCode,event);
			return false;
		}
	})
	.keyup(function(event){
		if (event.keyCode == 16) {
			$(".shift").css("background-color","transparent");
			$(".hiragana").css("color","black");
			$(".katakana").css("color","black");
			$(".small").css("color","#888888");
		} else {
			temp = event.keyCode;
			if (temp == 46) { temp = 8; }
			$("#key" + temp).css("background-color","transparent");
		}
	})

}

function enterChar(KEY,event){
	if (KEY == 16) { // shift keys
		$(".shift").css("background-color","#FF0000");		
		$(".hiragana").css("color","#888888");
		$(".katakana").css("color","#888888");
		$(".small").css("color","black");
	} else {
		$("#key" + KEY).css("background-color","#FF0000");

		if (!document.getElementById("key" + KEY)) { return false; } // drop out if the pressed key isn't assigned to a kana
		if (KEY == 18) { // alt
		
			if (charSet == "hiragana") {
				$(".hiragana").css("display","none");
				$(".katakana").css("display","block");
				$(".kanakata").css("color","black");
				$(".kanahira").css("color","#888888");
				charSet = "katakana";
			} else {
				$(".katakana").css("display","none");
				$(".hiragana").css("display","block");
				$(".kanakata").css("color","#888888");
				$(".kanahira").css("color","black");
				charSet = "hiragana";
			}
		} else if (KEY == 221 || KEY == 186) {
			if (KEY == 221) {
				if (voiced != 1) { voiced = 1; } else { voiced = 0; }
			} else {
				if (voiced != 2) { voiced = 2; } else { voiced = 0; }
			}

			voice();

		} else if (KEY == 32) { // spacebar
			$("#typefield").val($("#typefield").val() + " ");
			$("#transfield").val($("#transfield").val() + " ");
		} else if (KEY == 8) { // backspace (in case it was clicked with the mouse
			$("#typefield").val($("#typefield").val().substr(0,$("#typefield").val().length-1));
			$("#transfield").val(transcribe($("#typefield").val()));
		} else {
			var grabChar = charSet;
			
			switch (voiced) {
				case 1 : grabChar = charSet + "Voiced"; break;
				case 2 : grabChar = charSet + "SemiVoiced"; break;
			}
			
			if (!document.getElementById(grabChar + KEY)) { grabChar = charSet; } // reset the grabChar if voiced element doesn't exists.
			
			if (event.shiftKey == true) {
				switch (grabChar) {
					case "hiragana" : grabChar = "hirasmal"; break;
					case "katakana" : grabChar = "katasmal"; break;
				}
			}

			$("#typefield").val($("#typefield").val() + $("#" + grabChar + KEY).html()); // outputs the new string to the kana-field.
			$("#transfield").val(transcribe($("#typefield").val()));

			/* Reset the "voiced"-variable */
			if (voiced != 0) {
				voiced = 0;
				voice();
			}
		}
	}
}

var lastKana = "";
var currKana = "";

var currType = "";
var lastType = "";

/* TRANSCRIBE
The transcribe()-function romanizes any string sent in and returns the result as a new string.
*/
function transcribe(KANA) {
	var code
	var roman = "";
	var lastKana = "";
	var lastType = "";

	var kanas = KANA.split("");	// Split the received string into an array with one character in each position.

	// Loop through all characters in the array ...
	for (var i=0; i < kanas.length; i++) {
		code = kanas[i].charCodeAt(0);	// Grab the decimal unicode character number.

		window.status = code;
		
		code != 32 ? temp = getRoman(code) : temp=" ";

		if (getTrue(lastKana,"12483|12387")) {
			temp = temp.substr(0,1) + temp;
		} else if (currType == "yon" && lastType == "voi") {
			if (getTrue(lastKana,"12471|12375|12481|12385|12472|12376|12482|12386")) {
				temp = temp.substr(1); // If the last kana was any of the given, drop all charachters except the first one.
			}
			roman = roman.substr(0,roman.length-1);
		} else if (currType == "lng" || roman.substr(roman.length-1) == temp) {	// Is the current character the long vowel kana?
			
			if (getTrue(roman.substr(roman.length-1),"a|o|i|e|u")) {
				if (document.getElementById("transMethod").checked == true) {

					switch (roman.substr(roman.length-1)) {
						case "a" : temp = String.fromCharCode(257); break;
						case "e" : temp = String.fromCharCode(275); break;
						case "i" : temp = String.fromCharCode(299); break;
						case "o" : temp = String.fromCharCode(333); break;
						case "u" : temp = String.fromCharCode(363); break;
					}

					roman = roman.substr(0,roman.length-1);
				} else {
					temp = roman.substr(roman.length-1);
				}
			}
		} else if (getTrue(code,"12420|12422|12424|12516|12518|12520")) {
			if (getTrue(lastKana,"12531|12435") && document.getElementById("transMethod").checked == true) {
				temp = "'" + temp;
			}
		}

		roman += temp;

		lastKana = code;
		lastType = currType;
	}

	return roman;
}



function getRoman(CODE) {
	var t;

	/* comments on charType ...
		"con" = Double consonant. Output depends on the following kana. 
		"voi" = Voiced Kana that will be affected by a following Youon;
		"yon" = Youon Kana that will have an impact on the previous Kana in case this was a voiced Kana ending with an "i".
		"lng" = Long vowel. Output is dependent on previous kana.
	*/
	var charType = new Array("con",""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,"voi","voi",""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,"voi","voi",""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,"voi","voi",""   ,""   ,""   ,""   ,""   ,""   ,""   ,"voi",""   ,""   ,""   ,""   ,""   ,""   ,"voi","voi","voi",""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,""   ,"voi",""   ,""   ,""   ,"yon",""   ,"yon",""   ,"yon",""   ,""   ,"voi",""   ,""   ,""   ,""   ,""   ,"lng");
	var katakana = new Array(12483,12450,""   ,12452,12454,12456,12458,12459,12460,12461,12462,12463,12464,12465,12466,12467,12468,12469,12470,12471,12472,12473,12474,12475,12476,12477,12478,12479,12480,12481,12482,12484,12485,12486,12487,12488,12489,12490,12491,12492,12493,12494,12495,12496,12497,12498,12499,12500,12501,12502,12503,12504,12505,12506,12507,12508,12509,12510,12511,12512,12513,12514,12515,12516,12517,12518,12519,12520,12521,12522,12523,12524,12525,12527,12531,12540);
	var hiragana = new Array(12387,12354,12355,12356,12358,12360,12362,12363,12364,12365,12366,12367,12368,12369,12370,12371,12372,12373,12374,12375,12376,12377,12378,12379,12380,12381,12382,12383,12384,12385,12386,12388,12389,12390,12391,12392,12393,12394,12395,12396,12397,12398,12399,12400,12401,12402,12403,12404,12405,12406,12407,12408,12409,12410,12411,12412,12413,12414,12415,12416,12417,12418,12419,12420,12421,12422,12423,12424,12425,12426,12427,12428,12429,12431,12435,12540);
	var romaji   = new Array(""   ,"a"  ,""   ,"i"  ,"u"  ,"e"  ,"o"  ,"ka" ,"ga" ,"ki" ,"gi" ,"ku" ,"gu" ,"ke" ,"ge" ,"ko" ,"go" ,"sa" ,"za" ,"shi","ji" ,"su" ,"zu" ,"se" ,"ze" ,"so" ,"zo" ,"ta" ,"da" ,"chi","ji" ,"tsu","zu" ,"te" ,"de" ,"to" ,"do" ,"na" ,"ni" ,"nu" ,"ne" ,"no" ,"ha" ,"ba" ,"pa" ,"hi" ,"bi" ,"pi" ,"fu" ,"bu" ,"pu" ,"he" ,"be" ,"pe" ,"ho" ,"bo" ,"po" ,"ma" ,"mi" ,"mu" ,"me" ,"mo" ,"ya" ,"ya" ,"yu" ,"yu" ,"yo" ,"yo" ,"ra" ,"ri" ,"ru" ,"re" ,"ro" ,"wa" ,"n"  ,"");

	for (var i=0; i<romaji.length; i++) {
		if (CODE == hiragana[i] || CODE == katakana[i]) {
			currType = charType[i];
			return romaji[i];
		}
	}

}


/* VOICE
The voice()-function is used to control the keyboard highlights depending on whether the voiced or unvoiced keys has been pressed.
The function only hides and displays DIVs and neither takes or returns any values. The schema of how the keys are highlighted is
solely based on the value of the global "voiced"-variable.
*/
function voice() {
	if (voiced == 1) {
		$("." + charSet + "unvoiced").css("display","none");
		$("." + charSet + "voiced").css("display","block");
		$("." + charSet + "semivoiced").css("display","none");			
	} else if (voiced == 2) {
		$("." + charSet + "unvoiced").css("display","block");
		$("." + charSet + "unsemivoiced").css("display","none");
		$("." + charSet + "semivoiced").css("display","block");			
		$("." + charSet + "voiced").css("display","none");
	} else {
		$("." + charSet + "voiced").css("display","none");
		$("." + charSet + "semivoiced").css("display","none");			
		$("." + charSet + "unvoiced").css("display","block");
	}
}



































