/*
*	OSS Javascript Framework
*	Copyright 2009 Other Side Web Solutions
*
*	Plugin: Keyboard
*	Languages: EN,HE
*/
if(typeof(OSS) == "undefined")
	var OSS = { $: function(id) { return document.getElementById(id); } };

OSS.KeyboardPopup = function(inputId,divId)
{
	this.version = '1.0';
	this.isHebrewOn = false;
	this.inputId = inputId;
	this.divId = divId;
	this.rtl = false;
}
OSS.KeyboardPopup.prototype.keyPress = function(event)
{
	this.show();
	if(! this.isHebrewOn)
		return;
	
	var e = event || window.event;
	var keycode = e.charCode || e.keyCode;
	if(e.charCode == 0 || e.ctrlKey || e.altKey || keycode < 32)
		return true;
	var elem = OSS.$(this.inputId);
	var english_ascii = "tcdsvuzjyhlfkonibxg;p.mera,`qw'/";
	var hebrew_unicode = ['\u05D0','\u05D1','\u05D2','\u05D3','\u05D4','\u05D5','\u05D6',
							'\u05D7','\u05D8','\u05D9','\u05DA','\u05DB','\u05DC','\u05DD',
							'\u05DE','\u05DF','\u05E0','\u05E1','\u05E2','\u05E3','\u05E4',
							'\u05E5','\u05E6','\u05E7','\u05E8','\u05E9','\u05EA',
							'\u003B','\u002F','\u0027','\u002C','\u002E'];
	
	// get the position of the character from the english list
	var pos = english_ascii.indexOf(String.fromCharCode(keycode));
	// if character is to be switched for dhivehi equivalent
	if (pos != -1)
	{
		// update the text
		if(OSS.browser.isIE)
		{
			sel = document.selection.createRange();
			sel.text = hebrew_unicode[pos];
		}
		else
		{
			var selstart = elem.selectionStart;
			elem.value = elem.value.substring(0, selstart) + hebrew_unicode[pos] + elem.value.substring(elem.selectionEnd);
		}
		return false;
	}
	return true;
}
OSS.KeyboardPopup.prototype.hebrewOn = function()
{
	this.isHebrewOn = true;
	OSS.$(this.divId+'_en').style.display = 'none';
	OSS.$(this.divId+'_he').style.display = 'block';
	if(this.rtl)
		OSS.$(this.inputId).style.direction='rtl';
}
OSS.KeyboardPopup.prototype.hebrewOff = function()
{
	this.isHebrewOn = false;
	OSS.$(this.divId+'_en').style.display = 'block';
	OSS.$(this.divId+'_he').style.display = 'none';
	if(this.rtl)
		OSS.$(this.inputId).style.direction='';
}
OSS.KeyboardPopup.prototype.toggleHebrew = function(obj)
{
	if(this.isHebrewOn)
	{
		this.hebrewOff();
		obj.value = 'HE';
	}
	else
	{
		this.hebrewOn();
		obj.value = 'EN';
	}
	OSS.$(this.inputId).focus();
}
OSS.KeyboardPopup.prototype.sendKey = function(obj)
{
	OSS.$(this.inputId).value += obj.title;
	OSS.$(this.inputId).focus();
}
OSS.KeyboardPopup.prototype.show = function()
{
	OSS.$(this.inputId).focus();
	OSS.$(this.divId).style.display = 'block';
}
OSS.KeyboardPopup.prototype.hide = function()
{
	OSS.$(this.divId).style.display = 'none';
	OSS.$(this.inputId).focus();
}
OSS.KeyboardPopup.prototype.toggleHide = function(obj)
{
	if(OSS.$(this.divId).style.display != 'block')
	{
		this.show();
		obj.className='oss-keyboard-selected';
	}
	else
	{
		this.hide();
		obj.className='';
	}
}