/* copyright 2000-2005 Trezorix BV */

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function openWindow(_url, _name, _features) {
	top.window[_name] = window.open(_url, _name, _features);
	setTimeout("if (top.window[\"" + _name + "\"] != null) top.window[\"" + _name + "\"].focus();", 300);
}

function argFromURL(url) {
	var args = String(url).split("#");
	if (args.length > 1) {
		return args[1];
	} 
	else {	
		return ""; 
	}	
}

function changeImage(imgDocID,imgObjName) {
	document.images[imgDocID].src = eval( imgObjName + ".src")
}
	
function showContextPopUp(srcElement) {
	var featuresString = "left=100,top=100,height=500,width=460,scrollbars=yes,resizable=yes";
	_url = srcElement.getAttribute("href");
	openWindow(_url, "contextsearch", featuresString);
}


function showPopUp(srcElement, windowName, features) {
	var featuresString;
	
	if (features != null) {
		featuresString = features;
	}
	else {
		featuresString = "left=100,top=100,height=477,width=607,scrollbars=yes,resizable=yes";
	}
	_url = srcElement.getAttribute("href");
	openWindow(_url, windowName, featuresString);
}

function showZoomImg(fullname) {																																							 
	var featuresString = "left=100,top=100,height=500,width=500,scrollbars=yes,resizable=yes,viewbar=yes";
	var _url = aspLink + "?site=" + viewAlias + "&page_alias=imageview&image=" + fullname;
	openWindow(_url, "imageview", featuresString);
}

// begin trefwoord hi-light //
var lastHiLight = null;	

function hilightTerm(ref) {
	var termItem;
	var name;
	
	if (ref == "") return;
	
	if (typeof ref == "string") {
		name = ref;
	}
	else {
		name = argFromURL(ref.getAttribute("href"));
	}
	
	if (document.getElementById) {
		termItem = document.getElementById(name);
	}
	else if (document.anchors(name) != null) {
		termItem = document.anchors(name);
	} 
	else {
		//?
	}
	if (lastHiLight!=null) lastHiLight.className = "dfn";
	
	if (termItem != null) {
		termItem.className = "dfnHi";
		termItem.scrollIntoView();
		lastHiLight = termItem;
	}
	//document.URL = name;
}
// end trefwoord hi-light //

function hilightFreeTextTerm(ref) {
	Highlight(new Array("" + ref.innerHTML), 'container-center', true, '<span class="lemmahighlight">', '</span>');
}

function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('content').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight	= footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'absolute';
				footerElement.style.top = (windowHeight - footerHeight) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}

function focusAnchor() {
	this.blur();
}

function clickDfn() {
	showContextPopUp(this);
	return false;
}

function updateAnchors() {
	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++)
	{
		anchors[i].onfocus = focusAnchor;
	}
}


function updateDefinitions() {
	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++)
	{
		if (anchors[i].className == "dfn" || anchors[i].className == "lemma") {
			anchors[i].onclick = clickDfn;
		}	
	}
}

function updateHilightedTerm() {
	if (document.location) hilightTerm(argFromURL(document.location));
}

function hilightQWords() {
	if (q != "") {
		Highlight(q.split(' '), 'container-center', true, '<span class="searchhighlight">', '</span>');
	}	
}

function addEvent(obj, evType, fn) { 
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, true); 
		return true; 
	}	
	else if (obj.attachEvent) { 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else { 
		return false; 
	} 
}

addEvent(window, 'load', updateHilightedTerm);
addEvent(window, 'load', hilightQWords);
addEvent(window, 'load', updateAnchors);
addEvent(window, 'load', updateDefinitions);