﻿/**************************************************
 * Placeholder:	
 * <div id="keyboard_placeholder"></div>
 *
 * var keyboard = new KeyBoard();
 * [keyboard.layout = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"]
 *
 * keyboard.PressKey("]");
**************************************************/
/*
.active{background-color:blue;color:white}
*/
function KeyBoard(placeholder,keyboardlayout)
{
	this.layout = keyboardlayout;
	this.defaultkeys = this.layout.substring(0,47);
	this.shiftkeys = this.layout.substring(47,94);
	this.placeholder = placeholder;
	this.keys = new Array();
	this.enabledKeys = "";
	
	
	/**************************************************	
	 * Initialize keyboard:
	 **************************************************/

	// Add numbersrow:
	newRow = document.createElement("div");
	newRow.id = "numbersrow";
	this.placeholder.appendChild(newRow);
	
	for(i=0; i<13; i++)
	{
		newRow.appendChild(this._createKey(i));
	}
	
	// Add toprow:
	newRow = document.createElement("div");
	newRow.id = "toprow";

	this.placeholder.appendChild(newRow);
	
	for(i=13; i<26; i++)
	{
		newRow.appendChild(this._createKey(i));
	}
	
	// Add baserow:
	newRow = document.createElement("div");
	newRow.id = "baserow";

	this.placeholder.appendChild(newRow);
	
	for(i=26; i<37; i++)
	{
		newRow.appendChild(this._createKey(i));
	}
	
	// Add bottomrow:
	newRow = document.createElement("div");
	newRow.id = "bottomrow";
	this.placeholder.appendChild(newRow);
	
	// Add left shift key:
	var newShiftKey = document.createElement("input");
	newShiftKey.type = "button";
	newShiftKey.id = "shift1";
	newShiftKey.value = "shift";
	newShiftKey.className = "shift-left";
	newRow.appendChild(newShiftKey);
		
	for(i=37; i<47; i++)
	{
		newRow.appendChild(this._createKey(i));
	}

	// Add right shift key:
	newShiftKey = document.createElement("input");
	newShiftKey.type = "button";
	newShiftKey.id = "shift2";
	newShiftKey.value = "shift";
	newShiftKey.className = "shift-right";
	newRow.appendChild(newShiftKey);
	
	// Add spacebar:
	newRow = document.createElement("div");
	newRow.id = "spacebarrow";
	this.placeholder.appendChild(newRow);
	
	var newSpaceKey = document.createElement("input");
	newSpaceKey.type = "button";
	newSpaceKey.id = "space";
	newSpaceKey.className = "spacebar";
	newRow.appendChild(newSpaceKey);
	
	// Apply layout:
	this._applyLayout(this.defaultkeys);
}


/**************************************************	
 * Create and return new key:
 **************************************************/

KeyBoard.prototype._createKey = function(character)
{
	var newKey = document.createElement("input");
	newKey.type = "button";
	newKey.id = character;
	newKey.value = this._htmlEscape(character);
	newKey.className = "key";
	
	// Add reference to key:
	this.keys.push(newKey);
	
	return newKey;
}

/**************************************************	
 * Escape HTML characters:
 **************************************************/

KeyBoard.prototype._htmlEscape = function(str)
{
	return str;
}

/**************************************************	
 * Press Key:
 **************************************************/

KeyBoard.prototype.PressKey = function(character)
{
	if(this.layout.indexOf(character) > 47)
	{
		this._applyLayout(this.shiftkeys);
		document.getElementById("shift1").className += " key-active";
		document.getElementById("shift2").className += " key-active";
	}
	else
	{
		this._applyLayout(this.defaultkeys);
	}
	
	var activeKey;
	
	if(character == " ")
	{
		activeKey = document.getElementById("space");
	}
	else
	{
		activeKey = document.getElementById(character);
	}
	
	if(activeKey != null)
	{
		activeKey.className += " key-active";
	}
}

/**************************************************	
 * Apply keyboard layout:
 **************************************************/

KeyBoard.prototype._applyLayout = function(str)
{
	for(i=0;i<this.keys.length;i++)
	{
		this.keys[i].id = str.charAt(i);
		this.keys[i].value = this._htmlEscape(str.charAt(i));
		if(this.IsEnabledKey(str.charAt(i)))
		{
			this.keys[i].className = "key";
		}
		else
		{
			this.keys[i].className = "key key-disabled";
		}
	}
	
	document.getElementById("shift1").className = "shift-left";
	document.getElementById("shift2").className = "shift-right";
	document.getElementById("space").className = "spacebar";
}

/**************************************************	
 * Highlight keys:
 **************************************************/

KeyBoard.prototype.HighlightKeys = function(keylist,classname)
{
	for(i=0;i<keylist.length;i++)
	{
		var index = keylist[i];
		this.keys[index].className += " finger " + classname;
	}
}

/**************************************************	
 * Highlight keys:
 **************************************************/

KeyBoard.prototype.ShowFingerPositions = function()
{
	this.HighlightKeys(Array(0,1,13,26,37),"finger1");
	this.HighlightKeys(Array(2,14,27,38),"finger2");
	this.HighlightKeys(Array(3,15,28,39),"finger3");
	this.HighlightKeys(Array(4,16,29,40,5,17,30,41),"finger4");
	
	this.HighlightKeys(Array(6,18,31,42,7,19,32,43),"finger7");
	this.HighlightKeys(Array(8,20,33,44),"finger8");
	this.HighlightKeys(Array(9,21,34,45),"finger9");
	this.HighlightKeys(Array(10,22,35,46,11,23,36,12,24,25),"finger10");
	
	document.getElementById("shift1").className += " finger finger1";
	document.getElementById("shift2").className += " finger finger10";
	document.getElementById("space").className += " finger finger5 finger6";

	// Base finger positions:
	this.HighlightBaseKeys(Array(26,27,28,29,32,33,34,35),"baseposition")
	
}

/**************************************************	
 * Highlight Base keys:
 **************************************************/

KeyBoard.prototype.HighlightBaseKeys = function(keylist,classname)
{
	for(i=0;i<keylist.length;i++)
	{
		var index = keylist[i];
		this.keys[index].className += " " + classname;
	}
}

/**************************************************	
 * Function IsEnabledKey:
 **************************************************/

KeyBoard.prototype.IsEnabledKey = function(character)
{
	if(this.enabledKeys.length > 0)
	{
		if(this.enabledKeys.indexOf(character) < 0)
		{
			return false;
		}
		else
		{
			return true;
		}
		return true;
	}
	else
	{
		return true;
	}
}