window.onload = initializePage;
window.onresize = repositionLayout;


function initializePage () {
	destroyFrameset();
	initializeLayout();
}


function destroyFrameset () {
	// ...;
}


function initializeLayout () {
	if (document.getElementById('featureimage').className != 'feature1') {
		document.getElementById('featureimage').className = 'feature' + Math.floor(Math.random() * 5.999 + 1);
	}
	
	/*controlExpandableList('collapseAll', '');*/
	
	changeFontSize('load');
	
	repositionLayout();
}


function getWindowWidth () {
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	else if (document.body && document.body.offsetWidth) {
		return document.body.offsetWidth;
	}
	else {
		return 2000;
	}
}


function getWindowHeight () {
	if (window.innerHeight) {
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	else if (document.body && document.body.offsetHeight) {
		return document.body.offsetHeight;
	}
	else {
		return 2000;
	}
}


function repositionLayout () {
	var windowWidth = getWindowWidth();
	var windowHeight = getWindowHeight();
	
	document.getElementById('overlaymask').style.width = (windowWidth + 16) + 'px';
	document.getElementById('overlaymask').style.height = (windowHeight + 16) + 'px';
	document.getElementById('photoframe').style.left = ((windowWidth - 650) / 2) + 'px';
	document.getElementById('photoframe').style.top = ((windowHeight - 510) / 2) + 'px';
	
	if (windowWidth > 820) {
		document.getElementById('canvas').style.left = ((windowWidth - 780) / 2) + 'px';
		document.getElementById('container').style.left = ((windowWidth - 780) / 2) + 'px';
		document.getElementById('shadowleft').style.left = (windowWidth / 2 - 394) + 'px';
		document.getElementById('shadowright').style.left = (windowWidth / 2 + 390) + 'px';
		
		/* Browser specific fixes for css-fixed attribute */
		document.getElementById('header').style.left = (windowWidth / 2 - 390) + 'px';
		document.getElementById('sidenavi').style.left = (windowWidth / 2 - 390) + 'px';
		document.getElementById('circles').style.left = (windowWidth / 2 - 390) + 'px';
		document.getElementById('unilink').style.left = (windowWidth / 2 - 390) + 'px';
		document.getElementById('footer').style.left = (windowWidth / 2 - 390) + 'px';
		/* Fixes stop */	
	}
	else {
		document.getElementById('canvas').style.left = '0px';
		document.getElementById('container').style.left = '0px';
		document.getElementById('shadowleft').style.left = '-4px';
		document.getElementById('shadowright').style.left = '780px';
		
		/* Browser specific fixes for css-fixed attribute */
		document.getElementById('header').style.left = '0px';
		document.getElementById('sidenavi').style.left = '0px';
		document.getElementById('circles').style.left = '0px';
		document.getElementById('unilink').style.left = '0px';
		document.getElementById('footer').style.left = '0px';
		/* Fixes stop */	
	}
	
	document.getElementById('background').style.top = (windowHeight - 350) + 'px';
	document.getElementById('shadowleft').style.top = (windowHeight - 320) + 'px';
	document.getElementById('shadowright').style.top = (windowHeight - 320) + 'px';
	document.getElementById('footer').style.top = (windowHeight - 32) + 'px';
	document.getElementById('circles').style.top = (windowHeight - 305) + 'px';
	document.getElementById('unilink').style.top = (windowHeight - 85) + 'px';
	
	document.getElementById('sidenavi').style.height = (windowHeight - 280) + 'px';
	
	// if (document.getElementById('container').offsetHeight > 0 && document.getElementById('container').offsetHeight < windowHeight) {
	document.getElementById('canvas').style.height = windowHeight + 'px';
	document.getElementById('container').style.height = (windowHeight - document.getElementById('container').offsetTop) + 'px';
	// }
}


function initializeNavigation (category) {
	thesidenavi = document.getElementById('sidenavi');
	thecurrentelement = document.getElementById('navi-' + category);
	
	// zunächst alle Subnavi-Ebenen ausblenden
	for (var x = 0; x < thesidenavi.getElementsByTagName('ul').length; x++) {
		thesidenavi.getElementsByTagName('ul')[x].className = 'hidden';
	}
	
	// checken, ob aktuell angeforderter Navi-Eintrag existiert
	if (thecurrentelement) {
		// aktuellen Navi-Eintrag hervorheben
		thecurrentelement.className = 'active';
		
		// wenn in Subebene platziert, ausklappen
		if (thecurrentelement.parentNode.getAttribute('class') == 'hidden') {
			thecurrentelement.parentNode.className = '';
		}
		
		// wenn Subebene vorhanden, ausklappen
		for (var x = 0; x < thecurrentelement.getElementsByTagName('ul').length; x++) {
			thecurrentelement.getElementsByTagName('ul')[x].className = '';
		}
	}
}


function gotoPageTop () {
	window.scrollTo(0, 0);
}


function goLevelUp () {
	var currentLocation = /^(.*\/)[^\/]+\/[^\/]*$/;
	currentLocation.exec(window.location.href);
	
	var newLocation = RegExp.$1 + 'index.html';
	
	window.location.href = newLocation;
}


function changeFontSize (action, arguments) {
	switch (action) {
		case 'big':
			document.body.style.fontSize = '1em';
			document.cookie = 'fontSize=big';
			
			if (navigator.cookieEnabled == false) {
				alert('In Ihrem Browser ist das Speichern von Cookies zzt. deaktiviert. Bitte aktivieren Sie die Cookie-Unterstützung, um die Schriftvergrößerung dauerhaft anzuwenden.');
			}
			
			break;
			
		case 'small':
			document.body.style.fontSize = '0.8em';
			document.cookie = 'fontSize=small';
			break;
			
		case 'load':
			if (document.cookie) {
				var cookieString = document.cookie;
				
				if (cookieString == 'fontSize=big') {
					document.body.style.fontSize = '1em';
				}
			}
			
			break;
	}
}


function addBookmark () {
	alert('Ihr Browser unterstützt diese Funktion leider nicht. Bitte fügen Sie das Lesezeichen manuell hinzu!');
}


function controlSearch (action) {
	searchelement = document.getElementById('searchfield');
	inactivestring = 'Bitte Suchbegriff(e) eingeben…';
	
	switch (action) {
		case 'activate':
			searchelement.focus();
			break;
			
		case 'checkdown':
			if (searchelement.className != 'active') {
				searchelement.value = '';
				searchelement.className = 'active';
			}
			break;
			
		case 'checkup':
			if (searchelement.value == '') {
				controlSearch('reset');
			}
			break;
			
		case 'submit':
			if (searchelement.value == '' || searchelement.className == '' && searchelement.value == inactivestring) {
				alert('Der Suchvorgang konnte leider nicht durchgeführt werden, weil Sie keinen Suchbegriff eingegeben haben.');
				controlSearch('reset');
			}
			else {
				document.getElementById('searchform').submit();
			}
			break;
			
		case 'reset':
			searchelement.className = '';
			searchelement.value = inactivestring;
			break;
	}
}


function maillink (action, emailaddress) {
	switch (action) {
		case 'createLink':
			window.location.href = 'mailto:' + emailaddress;
			break;
			
		case 'createText':
			
			break;
	}
}


function imageviewer (action, picture) {
	switch (action) {
		case 'show':
			document.getElementById('overlaymask').className = '';
			document.getElementById('overlaypicture').src = picture;
			// document.getElementById('pictureholder').style.left = ((640 - document.getElementById('pictureholder').offsetWidth) / 2 + 5) + 'px';
			// document.getElementById('pictureholder').style.top = ((480 - document.getElementById('pictureholder').offsetHeight) / 2 + 5) + 'px';
			// alert();
			break;
			
		case 'hide':
			document.getElementById('overlaymask').className = 'hidden';
			document.getElementById('pictureholder').style.backgroundImage = 'none';
			break;
	}
}


function controlExpandableList (action, element) {
	switch (action) {
		case 'switchState':
			if (element.nextSibling.nextSibling.style.display == 'none' || element.nextSibling.nextSibling.style.display == '') {
				element.nextSibling.nextSibling.style.display = 'block';
				element.className = 'switchexpanded';
			}
			else {
				element.nextSibling.nextSibling.style.display = 'none';
				element.className = 'switchcollapsed';
			}
			
			break;
			
		case 'expand':
			
			break;
			
		case 'expandAll':
			for (var i = 0; i < document.links.length; i++) {
				if (document.links[i].className == 'switchexpanded' || document.links[i].className == 'switchcollapsed') {
					document.links[i].nextSibling.nextSibling.style.display = 'block';
					document.links[i].className = 'switchexpanded';
				}
			}
			
			break;
			
		case 'collapseAll':
			for (var i = 0; i < document.links.length; i++) {
				if (document.links[i].className == 'switchexpanded' || document.links[i].className == 'switchcollapsed') {
					document.links[i].nextSibling.nextSibling.style.display = 'none';
					document.links[i].className = 'switchcollapsed';
				}
			}
			
			break;
	}
	
	return false;
}


function redirect () {
	var rexpr = /[\?|&]redir=([^&]*)/;
	rexpr.exec(window.location.search);
	var redirurl = 'http://www.zfkw.uni-bonn.de' + RegExp.$1;
	document.getElementById('redirblock').innerHTML = '<a href="' + redirurl + '">' + redirurl + '</a>';
}

