//All lexiconCallBack.Callback calls are done here.
function doAsyncCall(text,exactMatch)
{
	window[IDPREFIX+'LexiconCallBack'].Callback(text,exactMatch);
}
//Asks the server for definitions.
function loadPhrase(e)
{
    var evt,originalHref,exactMatch,phrase;
    if(null!==e)
    {
		evt=new CommonEvent(e);
		evt.cancel();
		originalHref=evt.sender.href;
		phrase=decodeURI(evt.sender.search).replace('Phrase=','').replace('Sug=2','').replace('&','').replace('?','');
		exactMatch=-1!==originalHref.indexOf('Sug=2');
		evt.sender.href='';
		evt.sender.removeAttribute('href');
		setTimeout(function(){evt.sender.href=originalHref;},0);
	}
	else if(3===loadPhrase.arguments.length)
	{
		phrase=loadPhrase.arguments[1];
		exactMatch=loadPhrase.arguments[2];
	}
	else
	{
		return false;
	}
	window.didAsyncCall=true;
	//window[IDPREFIX+'LexiconCallBack'].Callback(phrase,exactMatch);
	doAsyncCall(phrase,exactMatch);
    return false;
}
//Asks the server for a list of phrases begining with a certain character
function loadCharIndex(e)
{
    var evt,originalHref,indexChar;
    if(null!==e)
    {
		evt=new CommonEvent(e);
		evt.cancel();
		originalHref=evt.sender.href;
		indexChar=evt.sender.firstChild.nodeValue;
		evt.sender.href='';
		evt.sender.removeAttribute('href');
		setTimeout(function(){evt.sender.href=originalHref;},0);
	}
	else if(2===loadCharIndex.arguments.length)
	{
		indexChar=loadCharIndex.arguments[1];
	}
	else
	{
		return false;
	}
    window.didAsyncCall=true;
    //window[IDPREFIX+'LexiconCallBack'].Callback(indexChar,true);
    doAsyncCall(indexChar,true);
    return false;
}
//form.submit event handler - does async calls that fetch search results from the server
function formSubmit(e)
{
	var evt,value;
	evt=new CommonEvent(e);
	value=document.forms[0][NAMEPREFIX+'SearchTextBox'].value;
	if(''!==value)
	{
	    //window[IDPREFIX+'LexiconCallBack'].Callback(value,false);
	    doAsyncCall(value,false);
	}
	evt.cancel();
	return false;
}
//Transforms the lexicon callback results / initialy rendered results anchors into scripted anchors
//Anchors with "blank" css class assigned will spawn new windows when clicked
//Anchors referencing the current document will not cause page-reloads but instead fetch new content via async callbacks.
//Additionaly, if the server rendered a new title for the page into the 'HiddenTitle' hidden-input field, the value will be used as a new title for the current document
function updateAnchorsAndTitle()
{
    var anchors,anchorsLength,i,anchor,title,query;
	anchors=document.getElementById(IDPREFIX+'LexiconCallBack_div').getElementsByTagName('a');
	anchorsLength=anchors.length;
	for(i=0;i<anchorsLength;i++)
	{
		anchor=anchors[i];
		if(hasClass('blank',anchor))
		{
			attachEventListener('click',anchor,openExternalLink);
		}
	    if(((('/'===anchor.pathname.charAt(0))?'':'/')+anchor.pathname)===document.location.pathname)
	    {
	        attachEventListener('click',anchor,loadPhrase);
	    }
	}
	title=document.getElementById(IDPREFIX+'HiddenTitle');
	if(null!==title)
	{
	    document.title=title.value;
	}
	if(true===window.didAsyncCall)
	{
		query=document.getElementById(IDPREFIX+'HiddenQuery');
		if(null!==query)
		{
			document.location.hash=window.oldHash='#'+query.value;
		}
	}
}
//Calls server and gets new content depending on the hash data available in document.location.hash
function loadByHash()
{
	if(2<document.location.hash.length)
	{
		switch(document.location.hash.charAt(1))
		{
			case 'T':exactMatch=true;break;
			case 'F':exactMatch=true;break;
			default:return;
		}
		phrase=decodeURI(document.location.hash.substr(2));
		if((true===exactMatch)&&(1===phrase.length))
		{
			loadCharIndex(null,phrase);
		}
		else
		{
			loadPhrase(null,phrase,exactMatch);
		}
	}
	if(0===document.location.hash.length)
	{
		//window[IDPREFIX+'LexiconCallBack'].Callback('',true);
		doAsyncCall('',true);
	}
}
//Monitors for changes in document.location.hash
function monitor()
{
	if(window.oldHash!==document.location.hash)
	{
		window.oldHash=document.location.hash;
		loadByHash();//This reloads (from the cache) a value from the history.
	}
}
//Inits the page, breaks out of frames, assignes async callbacks to the alphabet anchors and the form submit event
function body_Load()
{
	var alphabetAnchors,alphabetAnchorsLength,i,exactMatch,phrase,frame;
	if(location!==top.location)
	{
		top.location.href=location.href;
		return;
	}
	window.didAsyncCall=false;
	if(true===window.IE)
	{
		//TODO: Fix MSIE's broken back button
	}
	else
	{
		window.oldHash=document.location.hash;
		setInterval(monitor,0);//0 interval, prevents MSIE from executing this in case window.IE was somehow false.
	}
	if(''!==document.location.hash)
	{
		loadByHash();
	}
	updateAnchorsAndTitle();
	attachEventListener('submit',document.forms[0],formSubmit);
	alphabetAnchors=document.getElementById('Alphabet').getElementsByTagName('a')
	alphabetAnchorsLength=alphabetAnchors.length;
	for(i=0;i<alphabetAnchorsLength;i++)
	{
	    attachEventListener('click',alphabetAnchors[i],loadCharIndex);
	}
}