var loadOk = false;
var oXmlDom = zXmlDom.createDocument();
var aXmlDom = zXmlDom.createDocument();
    
var dictionaryElements = new Array();
var dictionaryTextDescription = new Array();

    
function dictionaryInit() 
{
	/* Have we already been initialized ? */
	if (!loadOk) {
		loadElements();
		loadalphabet();    

		document.getElementById('words').innerHTML = ""; //emptying keyword field      
		document.getElementById('dictionarydescription').innerHTML = ""; //emptying description field
	}
}


function loadElements()
{
	//loading the dictionary
	oXmlDom.onreadystatechange = function () 
		{
			if (oXmlDom.readyState == 4) {
				loadOk = true;
            
				var nodes = zXPath.selectNodes(oXmlDom.documentElement, "//element");
            
				for (var ix = 0; ix < nodes.length; ix++) {
					var node = nodes[ix];				
					var o = new Object();
								
					o.headline = zXPath.selectSingleNode(node, "headline").text; 
					o.text = zXPath.selectSingleNode(node, "text").text;
					o.imageUrl = zXPath.selectSingleNode(node, "imageurl").text;
					o.imageAltText = zXPath.selectSingleNode(node, "imagealttext").text;
				
					dictionaryElements[dictionaryElements.length] = o;
				}
			}
    		}
	oXmlDom.load("dictionarydata.aspx");
}


function loadalphabet()
{
	//loading the alphabet
	aXmlDom.onreadystatechange = function ()
		{
			if (4 == aXmlDom.readyState) {
			    loadOk = true;
				var aRoot = aXmlDom.documentElement;
				var alfacount = zXPath.selectNodes(aRoot, "//letters/*").length;
				var a = -1;
            
				while (a<alfacount) {
					try {
						var a1 = zXPath.selectSingleNode(aRoot, "//letter["+(a+1)+"]").text;
                    
						var a2 = "<a onclick='addletter(\""+a1+"\")'>" + a1 + "</a>&nbsp;"
						document.getElementById("letters").innerHTML += a2
					}
					catch (e) {}
                
					a=a+1;
				}
	        	}
	    	}
	aXmlDom.load("alphabet.xml");
}


function compare(text, chars)
{
	var len = chars.length;
			
	var a = String(text.substring(0, len)).toLowerCase();
	var b = String(chars).toLowerCase();
    
	if (a == b) {
		return true;
	}	
	
	return false;
}


function keyUp() 
{
	var text = String(document.getElementById("searchfield").value).toLowerCase();

	document.getElementById('words').innerHTML = ""; //emptying keyword field      
	document.getElementById('dictionarydescription').innerHTML = ""; //emptying description field

	if (loadOk && text != "") {      
		dictionaryTextDescription = new Array();  // clear out the old contents
		
		for (var ix = 0; ix < dictionaryElements.length; ix++) {

			var e = dictionaryElements[ix];			
			var headline = e.headline;

			if (compare(headline, text)) {
				var tmpText = e.text;
			
				if (e.imageUrl != "") {
					var imageTag = "<img src=\"" + e.imageUrl + "\" alt=\"" + e.imageAltText + "\" />";
					tmpText += imageTag;
				}
				
				dictionaryPosition = dictionaryTextDescription.length;
				dictionaryTextDescription[dictionaryPosition] = tmpText;

				var tag = "<a onclick='showdesc(\"" + dictionaryPosition + "\")'>" + headline + "</a>"; 
				document.getElementById('words').innerHTML += tag;
			}
		}        
	}
}


function showdesc(ix)
{
	var txt = dictionaryTextDescription[ix];               
	var d = document.getElementById('dictionarydescription');         
	d.innerHTML = txt;
}


function addletter(text)
{
	//Setting the selected letter in the search field
	document.getElementById('searchfield').value = text
	keyUp();
}


/* sometimes the addLoadEvent hangs and never calls our init function .
   we will wait 3 secs and then all the function */
setTimeout("dictionaryInit()", 3000);


/* calls the function after the page is loaded */
addLoadEvent(dictionaryInit);
