/**
 * @author venkat
 */
var tbox=null;
var oc="";
var thold='';
var b=0;
var ob=null;
var mo=null;
var ce=-1;
var debug = 0;
var shadowText=null;
var wList;
var bIE=false;

function log(txt)
{
  var elm = document.getElementById("debug");
  if(elm!=null)
     elm.innerHTML=elm.innerHTML+"<br>\n"+txt;
}
function clearlog()
{
  var elm = document.getElementById("debug");
  if(elm!=null)
     elm.innerHTML="";
	
}
function kbInit(t,ws)
{
   if (document.selection && document.selection!='undefined') 
       bIE=true;
  tbox = t;
  
  initPos(tbox);
  //tbox.onkeypress=keyDown;
  tbox.onkeydown=keyDown;
  tbox.onkeyup=keyHandler;
  tbox.onselect = saveCaret;
  tbox.ondblclick=showCurText;
  tbox.onpaste=updateTBox;
  saveCaret();
  wList = new LSState(tbox,document.getElementById("finaltext"),ws);
 
}

function updateTBox(){
	wList.updateText();
}
function showCurText(){
	
	var w = wList.getcw();
	if(w!=null)
	{
		showChars(w.getxlit(),w,false);
	}
}

function saveCaret() {
   if (document.selection && document.selection!='undefined') {
   	   tbox.caretPos = document.selection.createRange().duplicate();
   }
}

function initPos(textBox) {
	    textBox._RowHeight = Math.floor(textBox.clientHeight/textBox.rows);
		if (!bIE) {
			var el = document.getElementById("shadow");
			el.style.visibility="hidden";
			el.style.clientLeft = tbox.clientLeft;
			el.style.clientTop = tbox.clientTop;
			el.style.clientHeight = tbox.clientHeight;
			el.style.clientWidth = tbox.clientWidth;
			el.style.width=tbox.clientWidth + "px";
			el.style.maxWidth = tbox.clientWidth + "px";
			el.style.fontSize = tbox.style.fontSize;
			el.style.textAlign = "left";
			el.style.position="absolute";
           
			tbox.shadowPos = el;
			
		}
}

function keyDown(ev)
{
	var newchar;
	  if(!ev) ev = window.event;
	  if(ev.keyCode==27)
	  { 
	  	if (hideTransliterator("charlist") == true) 
		{
			if (bIE) {
				ev.cancelBubble = true;
				ev.returnValue = false;
				ev.keyCode = 0;
			}
			else 
				ev.stopPropagation();
		}
	  }
}

function keyHandler(e)
{
	  	clearlog();
	  	saveCaret();
	  	if (shadowText != tbox.value) // || curWord != lastWord) 
		{
			wList.save(null);
			lookupChar(e);
			shadowText = tbox.value;
			
		}
 
}

function ed(){
	var s,i;
	var w = wList.curword();
    var s2 = ob.innerHTML;
	s="";
	if(s2.length==0)
	   s2=" ";
	o=ob;
	indx=ce;
	if(w!=null)
	    s = w.updfinal(indx,s2);  
	var elm = document.getElementById("finaltext");
	if(elm!=null)
	   elm.innerHTML=s;
}

function ei(o,indx){
	ce=indx;
	ovmain(o,ed);
}

function ovmain(o,fc){
var bt;

	var pr = document.getElementById("chpreview");
	if (pr != null) {
		pr.style.visibility="visible";
		pr.innerHTML = o.innerHTML;
	}

    if(b==1 && thold!=null && ob!=null && ob!=o)
	{
        ob.className=oc;
	    ob.onmouseover=mo;
	    ob.onclick=null;	
        b=0;ob=null;thold=null;
    }
    if(b==0)
	{
        ob=o;
    	thold=o.innerHTML;
		oc=o.className;
    	mo = o.onmouseover;
		o.className="key";
		o.onmouseover="";
		o.onclick = fc;
        b=1;

	}
}

