// Written by Wordman http://www.divnull.com/lward/
// Version 1.0 27 July 2004

gTransMap = null;
gVowel = "aeiou";
gLastVowel = "a";

function transScript(aForm)
{
	msg = aForm.msg.value.toLowerCase();
	transText(msg)
}

function transText(msg)
{
	// Break the text into chunks that end with vowels, spaces or end of line
	var syllables = tokenize(msg);
	
	// Turn these chunks into glyph sounds. Note that there may not be a one-to-one
	// correspondance between syllables and glyphs.
	var len = msg.length;
	var glyphs = new Array();
	gLastVowel = "a";
	var sylCount = 0;
	var syllable = syllables[sylCount];
	while (syllable.length > 0)
	{
		var glyph = mutate(syllable);
		if (glyph)
		{
			glyphs.push(glyph);
		}
		else
		{
			// Odd syllable
			mutateOdd(syllable,glyphs)
		}
		++sylCount;
		syllable = syllables[sylCount];
	}
	
	display(glyphs);
}

function tokenize(aMsg)
{
	var msg = aMsg.toLowerCase();
	var len = msg.length;
	var syllables = new Array(len + 1);
	var index = 0;
	var syllable = "";
	var breakon = gVowel + " "
	var legal = breakon + "bcdfghjklmnpqrstvwxyz"
	for (var i = 0; i < len ; i++)
	{
		var curChar = msg.charAt(i);
		var pos = legal.indexOf(curChar);
		if (pos >= 0)
		{
			if (curChar == ' ')
			{
				if (syllable.length)
				{
					syllables[index] = syllable;
					++index;
				}
				syllables[index] = " ";
				++index;
				syllable = "";
			}
			else
			{
				syllable += curChar;
				pos = breakon.indexOf(curChar);
				if (pos >= 0)
				{
					// Have a vowel, end in the syllable. Simplistic, but true in this script system
					syllables[index] = syllable;
					++index;
					syllable = "";
				}
			}
		}
	}
	syllables[index] = syllable;
	++index;
	syllables[index] = "";
	return syllables;
}

function mutate(syllable)
{
	// See if last character is a vowel
	var glyph = null;
	var len = syllable.length;
	var lastChar = syllable.charAt(len - 1);
	var pos = gVowel.indexOf(lastChar);
	var work = syllable;
	var sawVowel = gLastVowel;
	
	if (syllable == " ")
	{
		glyph = syllable;
	}
	else if (pos >= 0)
	{
		// Ends with vowel
		if (len == 1)
		{
			// The syllable is a single vowel. Return it
			glyph = syllable;
			sawVowel = lastChar;
		}
		else
		{
			sawVowel = lastChar;
			work = syllable.substr(0,len-1);
		}
	}
	
	if (!glyph)
	{
		// Work now contains only legal consonants
	
		// Check for a translation
		var map = getTransMap();
		if (work in map)
		{
			glyph = map[work] + sawVowel;
		}
		else if (work.length == 1)
		{
			// If the consonant part is a single character, easy.
			glyph = work + sawVowel;
		}
		else
		{
			// We have a weird character combination, return null
			// so it can be analyzed.
		}
	}
	
	if (glyph && sawVowel != gLastVowel)
	{
		gLastVowel = sawVowel;
	}
	//alert("mutating " + syllable + " returns " + glyph + " lastChar = " + lastChar);
	return glyph;
}

function mutateOdd(syllable,glyphs)
{
	var len = syllable.length
	var endPos = len-1;
	var work = syllable;
	
	// Try to mutate smaller and smaller substrings	
	while (endPos > 0)
	{
		test = work.substr(0,endPos)
		//alert("test=" + test + " endPos=" + endPos);
		var glyph = mutate(test)
		if (glyph)
		{
			glyphs.push(glyph);
			if (endPos < work.length)
			{
				work = work.substr(endPos,len);
				endPos = work.length;
			}
			else
			{
				endPos = -1;
			}
		}
		else
		{
			--endPos;
		}
	}
}

function display(glyphs)
{
	var p = document.createElement("p");
	var numImages = glyphs.length;
	for (i = 0; i < numImages; i++)
	{
		var height = "100"
		var fileName = glyphs[i]
		if (fileName == ' ')
		{
			fileName = "space";
			height = "50";
		}
		var img = document.createElement("img");
		var imgName = "script/high/" + fileName + ".png"
		img.setAttribute("src",imgName);
		img.setAttribute("height",height);
		img.setAttribute("width","156");
		img.setAttribute("border","0");
		img.setAttribute("alt",glyphs[i]);
		p.appendChild( img );
		p.appendChild( document.createTextNode(glyphs[i]) );
		p.appendChild( document.createElement("br") );
	}
	var displayNode = document.getElementById('display');
	nodes = displayNode.childNodes.length;
	for (var i=0; i < nodes; i++)
		displayNode.removeChild(displayNode.childNodes[0]);
	displayNode.appendChild( p );
}

function getTransMap()
{
	if (!gTransMap)
	{
		gTransMap = new Array();
		gTransMap["c"] = "k";
		gTransMap["ch"] = "ch";	// Return self to stop processing of this syllable
		gTransMap["j"] = "g";
		gTransMap["q"] = "k";
		gTransMap["sh"] = "ch";
		gTransMap["th"] = "d";
		gTransMap["v"] = "f";
		gTransMap["z"] = "x";
	}
	return gTransMap;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	} 
	else
	{
    	window.onload = function()
		{
      		oldonload();
      		func();
		}
	}
}
