/* 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 createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function readCookieGroup(name) {
	var nameRe = new RegExp("\s*(" + name + ")(.*)=(.*)", "gi");
	var ca = document.cookie.split(';');
	var result = new Array();
	for (var i = 0; i < ca.length; i++) {
		matches = ca[i].match(nameRe);
		if (matches) {
			//result[result.length] = RegExp.$1;
			result[result.length] = RegExp.$2;
			result[result.length] = RegExp.$3;
		}
	}
	return result;
}

function linkBack(link) {
				window.opener.location.href = link;
				window.opener.focus()
				window.close()
			}

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, chosenid, refid, refsite, thisalias, thisid) {
	var featuresString = "left=100,top=100,height=500,width=460,scrollbars=yes,resizable=yes";
	_url = srcElement.getAttribute("href");
	openWindow(_url, "contextsearch", featuresString);
}

function showHierarchyPopUp(srcElement, chosenid, refid, refsite, thisalias, thisid) {
	var featuresString = "left=100,top=100,height=240,width=480,scrollbars=yes,resizable=yes";
	_url = srcElement.getAttribute("href");
	openWindow(_url, "hierarchy", featuresString);
}

function showMailPopUp(contentAlias, contentView, contentId, contentUrl) {
	//var contentUrl = applicationFolder + "/" + contentAlias + "/" + contentView + "/" + contentId + ".html";
	var _url = aspLink + "?site=" + viewAlias + "&page_alias=mailafriend&contentAlias=" + contentAlias + "&contentId=" +
		contentId + "&contentView=" + contentView + "&contentVersion=mail" + "&contentUrl=" + contentUrl + "&type=mailafriend&show=form";
	var featuresString = "left=50,top=40,height=590,width=380,menubar=no,toolbar=no,scrollbars=no,resizable=no";
	openWindow(_url, "mail", 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=520,scrollbars=yes,resizable=yes,viewbar=yes";
	var _url = aspLink + "?site=" + viewAlias + "&page_alias=imageview&image=" + fullname;
	openWindow(_url, "imageview", featuresString);
}

function showZoomImg2(srcElement) {
	var featuresString = "left=100,top=100,height=500,width=550,scrollbars=yes,resizable=yes,viewbar=yes";
	_url = srcElement.getAttribute("href");
	openWindow(_url, "imageview", featuresString);
}


function showZoomImgTrend(srcElement) {
	var featuresString = "left=100,top=100,height=550,width=750,scrollbars=yes,resizable=yes,viewbar=yes";
	_url = srcElement.getAttribute("href");
	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';
			}
		}
	}
}

/* start tabs */
function swapTab(obj) {
	var tabItem = obj.parentNode;
	var tabList = tabItem.parentNode;
	var tabs = tabList.getElementsByTagName("li");
	var panelList = tabList.parentNode.nextSibling;
	var panels = panelList.childNodes;// .getElementsByTagName("div"); geeft alle descendants!!!!
	for (var i = 0; i < tabs.length; i++) {
		if (tabs[i] == tabItem) {
			tabItem.className = "activetab";
			if (panels[i]) {
				panels[i].className = "activetabpanel";
			}
		}
		else {
			tabs[i].className = "";
			if (panels[i]) {
				panels[i].className = "tabpanel";
			}
		}
	}
}
/* end tabs */

function focusAnchor() {
	this.blur();
}

function clickDfn() {
	showContextPopUp(this);
	return false;
}

function clickHierarchy() {
	showHierarchyPopUp(this);
	return false;
}

function clickZoomImage() {
	showZoomImg2(this);
	return false;
}

function clickZoomImageTrend() {
	showZoomImgTrend(this);
	return false;
}

function updateAnchors() {
	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++)
	{
		anchors[i].onfocus = focusAnchor;
		switch (anchors[i].className) {
			case "dfn":
			case "lemma":
				anchors[i].onclick = clickDfn;
				break;
			case "lemma":
				anchors[i].onclick = clickDfn;
				break;
			case "hierarchy":
				anchors[i].onclick = clickHierarchy;
				break;
			case "zoomimage":
				anchors[i].onclick = clickZoomImage;
				break;
			case "zoomimagetrend":
				anchors[i].onclick = clickZoomImageTrend;
				break;
		}
	}
}


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;
	}
}

// Homepage carrousel
function returnObjById(id){
  if (document.getElementById){
    var returnVar = document.getElementById(id);
  }else if (document.all) {
    var returnVar = document.all[id];
  }else if (document.layers) {
    var returnVar = document.layers[id];
  }
  return returnVar;
}

function setupHomepageCarrousel(){
  if (returnObjById('headercarrousel') != null){
    var flashvars = {
      dataprovider:siteRoot + "get?alias=" + viewAlias + "%26id=i000395"
    };
    var params = {
      menu: "false",
      scale: "noScale",
      allowFullscreen: "true",
      allowScriptAccess: "always",
      bgcolor: "#FFFFFF",
      wmode: "transparent"
    };
    
    var attributes = {
      id:"headercarrousel"
    };
 
     swfobject.embedSWF("/sites/" + viewAlias + "/flash/headercarrousel.swf", "headercarrousel", "980", "253", "9.0.0",false, flashvars, params, attributes);
  }
}

//addEvent(window, 'load', updateHilightedTerm);
//addEvent(window, 'load', hilightQWords);
addEvent(window, 'load', updateAnchors);
addEvent(window, 'load', setupHomepageCarrousel);

