var nLetters = 0;
var inputBuf = new Array(30);
var maxInput = 0;

var hasTonos, hasDiaerasis, hasTD;
var cursorPos = 0;
var cursorState = false;
var blinkTimer = null;
var input_start = 0;

var grkAlpha=
    "αβψδεφγηιξκλμνοπ;ρστθωςχυζ";

var remapKey = new Array();

remapKey[222] = 0x84;
remapKey[39] = 2;
remapKey[37] = 1;

// entries are normal, tonos, TONOS
var tonosMap = new Array(
    0xb1, 0xac, 0x86, // alpha
    0xb5, 0xad, 0x88, // epsilon
    0xb7, 0xae, 0x89, // ita
    0xb9, 0xaf, 0x8a, // iota
    0xbf, 0xcc, 0x8c, // omicron
    0xc5, 0xcd, 0x8e, // ypsilon
    0xc9, 0xce, 0x8f, // omega
    0x83, 0x85, 0x85, // diaerasis
    0x84, 0x00, 0x00, // tonos
    0x85, 0x00, 0x00  // tonos-diaearasis
);

var diaerasisMap = new Array(
    0xb9, 0xca, 0xaa, // iota
    0xc5, 0xcb, 0xab, // ypsilon
    0x83, 0x00, 0x00, // diaerasis
    0x84, 0x85, 0x85, // tonos
    0x85, 0x00, 0x00  // tonos-diaerasis
);

var tdMap = new Array(
    0xb9, 0x90, 0x90, // iota
    0xc5, 0xb0, 0xb0, // ypsilon
    0x83, 0x00, 0x00, // diaerasis
    0x84, 0x00, 0x00, // tonos
    0x85, 0x00, 0x00  // tonos-diaearasis
);

function showCursor( turnOn )
{
    if (!turnOn)
    {
        if (blinkTimer)
        {
            window.clearTimeout( blinkTimer );
        }
    }

    document.images["c" + cursorPos].src = pic_dir +
        ((turnOn) ? "c_on.png" : "coff.png");
    if (turnOn)
    {
        cursorState = false;
        blink();
    }
}

function blink()
{
    cursorState = !cursorState;
    document.images["c" + cursorPos].src = pic_dir +
        ((cursorState) ? "c_on.png" : "coff.png");
    blinkTimer = window.setTimeout( "blink()", 750 );
}

function refocus()
{
	// do nothing for now
}

function clearInput( initStr )
{
    var i;

    showCursor( false );
    nLetters = 0;
    /*
    if (initStr)
    {
        showGreek( initStr, initStr.length, "");
        input_offset = initStr.length;
    }
    else
    {
        input_offset = 0;
    }
    */
    input_offset = 0;
    cursorPos = input_offset;
    for (i=input_offset; i<maxInput; i++)
    {
        document.images["l" + i].src = pic_dir + "20.png";
		if (document.images["g" + i])
		{
        	document.images["g" + i].src = pic_dir + "20.png";
		}
        inputBuf[i] = 0;
    }
    showCursor( true );
	refocus();
}

function cleanupInput( )
{
    showCursor(false);
}

function setInput( area, nMax )
{
	area.onkeypress = kpress;
	maxInput = nMax;
    clearInput( "" );
	refocus();
}

function mapAccent( n, mapName )
{
    var i;

    for (i=0; i<mapName.length; i+=3)
    {
        if (n == mapName[i])
        {
            n = mapName[i+1];
            break;
        }
    }
    return n;
}

function handleAccent( n )
{
    /* see if we have a tonos in this position */
    if (cursorPos > input_offset)
    {
        hasTonos = (inputBuf[cursorPos-1] == 0x84);
        hasDiaerasis = (inputBuf[cursorPos-1] == 0x83);
        hasTD = (inputBuf[cursorPos-1] == 0x85);
    }
    else
    {
        hasTonos = hasDiaerasis = hasTD = false;
    }

    /* if no accent here, it's a regular char */
    if (!hasTonos && !hasDiaerasis && !hasTD)
    {
        return n;
    }

    if (hasTonos) 
    {
        n = mapAccent( n, tonosMap );
    }
    else if (hasDiaerasis)
    {
        n = mapAccent( n, diaerasisMap );
    }
    else if (hasTD)
    {
        n = mapAccent( n, tdMap );
    }
    if (n == 0)
    {
        return 0;
    }

    inputBuf[cursorPos-1] = n;
    document.images["l" + (cursorPos-1)].src =
        pic_dir + n.toString(16) + ".png";
    showCursor(true);
    return 0;
}

function kclick(n)
{
    var i, valid;
    if (nLetters <  maxInput)
    {
    
		if (validMap[0] == -999)
		{
			valid=true;
		}
		else
		{
			valid = false;
			i = 0;
			while ( validMap[i] != -999 && validMap[i] != n )
			{
				i++;
			}
			valid = (i = validMap[i] != -999); 
		}
	
		if (!valid)
		{
			refocus();
			return;
		}
	
		showCursor( false );
	
		if (n == 1 && cursorPos > input_offset) // cursor left
		{
			cursorPos--;
		}
		else if (n == 2 && cursorPos < input_offset + nLetters) // cursor right
		{
			cursorPos++;
		}
		
		if (n < 0x20)
		{
			showCursor(true);
			refocus();
			return;
		}
		n = handleAccent( n );
		if (n == 0)
		{
			refocus();
			return; //  false;
		}
	
		/* move everyone over one space */
		for (i= maxInput-2; i >= cursorPos; i--)
		{
			inputBuf[i+1] = inputBuf[i];
			if (inputBuf[i+1] != 0)
			{
				document.images["l" + (i+1)].src =
					document.images["l" + i].src;
			}
		}
		inputBuf[cursorPos] = n;
		document.images["l" + cursorPos].src =
			pic_dir + n.toString(16) + ".png";
		nLetters++;
		cursorPos++;
		showCursor( true );
	}
    refocus();
}

function kpress( theEvent )
{
    var charCode = (theEvent) ? theEvent.which : window.event.keyCode;
	var result;

    /* handle control codes */
    if (charCode == 13 || charCode == 10)
    {
        showCursor( false );
        entered();
        refocus();
        result = true;
    }
    else if (charCode == 8 || charCode == 127)
    {
        bksp();
        refocus();
        result = false;
    }
    else
    {
		/* now handle everything "normal" */
		n = 0;
		if (charCode >= 97 && charCode <= 122) // a-z
		{
			charCode -= 32;
		}
		if (charCode >= 65 && charCode <= 90)
		{
			n = (grkAlpha.charCodeAt(charCode - 65) & 0xff);
		}
		else if (charCode == 58)
		{
			n = 0x83;
		}
		else if (charCode == 39)
		{
			n = 0x84;
		}
		else if (charCode == 32)
		{
			n = 0x20;
		}
		else if (remapKey[charCode])
		{
			n = remapKey[charCode];
		}
		
		if (n != 0)
		{
			kclick(n);
		}
		refocus();
		result = false;
	}
    return false; // was return result;
}


function bksp()
{
    var i;

    if (cursorPos > input_offset)
    {
        showCursor(false);
        /* pull everyone in */
        for (i=cursorPos; i<nLetters && inputBuf[i] != 0; i++)
        {
            document.images["l" + (i-1)].src = 
                document.images["l" + i].src;
            inputBuf[i-1] = inputBuf[i];
            /*if (inputBuf[i] == 0)
            {
                break;
            }*/
        }
        nLetters--;
        cursorPos--;
        inputBuf[nLetters] = 0;
        document.images["l" + nLetters].src = pic_dir + "20.png";
        showCursor(true);
    }
}

function collectGreekAnswer()
{
    var str, i, j;

    i = 0;
    str = "";
    while (i < maxInput && inputBuf[i] != 0)
    {
        if (inputBuf[i] == 0x20)
        {
            str += " ";
            i++;
            while (i < maxInput && inputBuf[i] == 0x20)
            {
                i++;
            }
        }
        else
        {
            j = inputBuf[i++];
            if (j > 128)
            {
               j += 0x300;
            }
            str += String.fromCharCode(j);
        }
    }
    return str;
}


