//
// Language switch using an optional 'lang' attribute.
//
// Further reading:
// * http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/
//
// Reading on alternate style sheets:
// * (http://www.google.de/search?q=html+switch+style+sheet)
// * http://alistapart.com/articles/alternate/
// * http://www.stichpunkt.de/css/switch.html
//
// Copyright (c) 20 Sep 2006 by Peter Bittner

//
// Tells the browser to open any 'a href's, which are specifying a
// 'rel="external"' attribute, in a new browser window.
//
// NOTE: You need to specify a lang-attribute with a respective
//       language id value (e.g. "de", "en", etc.) in any of the
//       following tags: h1, h2, h3, dl, ol, ul, p
//
// e.g. <dl lang="de">...</dl>
//

function setLanguage(lang_id)
{
	if (!document.getElementsByTagName)
		return;

	var extra = getShowExtra();
	var tagsArr = new Array("h1", "h2", "h3", "p", "dl", "ol", "ul", "dt", "dd", "li", "span");

	for (var i = 0; i < tagsArr.length; i++)
	{
		var tags = document.getElementsByTagName(tagsArr[i]);

		for (var j = 0; j < tags.length; j++)
		{
			var tag = tags[j];
			var langAttr = tag.getAttribute("lang");
			var classAttr = tag.getAttribute("class");
			if (tag.className)	// IE, you suck!!
				classAttr = tag.className;

			tag.style.display = "";

			if ((langAttr && langAttr != lang_id) ||
			   (classAttr && classAttr.indexOf("extra") >= 0 && !extra))
				tag.style.display = "none";
		}
	}
}

function getLanguage()
{
	return document.URL.substr(document.URL.lastIndexOf("/cv-") + 4, 2);
}

function getShowExtra()
{
	// check for cookie (if cookies disabled internal value will be fallback)
	var cstart = document.cookie.indexOf("show_extra=");
	var cstop  = document.cookie.indexOf(";", cstart + 1);
	if (cstart != -1 && cstart + 11 < cstop)
	{
		var cookie = document.cookie.substring(cstart + 11, cstop);
		if (cookie == "true" || cookie == "false")
			getShowExtra.show = (cookie == "true");
	}

	// init pseudo-static variable
	if (typeof getShowExtra.show == "undefined")
		getShowExtra.show = false;

	return getShowExtra.show;
}

// Defined whether to how or hide all non-essential cv content (i.e. items
// having class "extra"), and sets a cookie to remember this choice for
// next visit.          
function setShowExtra(show)
{
	// set pseudo-static variable value of getter, then set browser cookie
	getShowExtra.show = show;
	document.cookie = "show_extra=" + show + "; expires=Mon, 15 Jun 2020 15:06:20 UTC; path=/";
}

function showExtra(show)
{
	setShowExtra(show);
	// document.getElementById("noextra_button").disabled = !show;
	// document.getElementById("extra_button").disabled = show;
	var noextButton = document.getElementById("noextra_button");
	var extraButton = document.getElementById("extra_button");
	if (noextButton)
		noextButton.disabled = !show;
	if (extraButton)
		extraButton.disabled = show;
	setLanguage(getLanguage());
}

// hide other languages and non-essential content as soon as finished loading the document
addLoadEvent(function() { showExtra(getShowExtra()); });
// load hidden document explanation image for print version as soon as finished displaying
addLoadEvent(function() { var img = document.getElementById("explained"); if (img) img.src = "/cv-docs/cv-explained.png"; });


