// Tim Reeves JavaScript for Seminarraum, Stand 2009-06-20
// with xml-compliant code to insert an img node for the counter

// FAUSTREGEL: READ an attribute and SET a style (with a text string) !!!

// CONFIG
var intMinSiteHeight = 450;
// END CONFIG

var blnHeightToHtml = false;	// Memory for WebKit Bug

function getElemId(ident) {
 var Elem;
 if (document.getElementById) { // DOM; IE5, NS6, Mozilla, Opera
     if (typeof document.getElementById(ident) == "object")
     Elem = document.getElementById(ident);
     else Elem = void(0);
     }
 else if (document.all) { // Proprietary DOM; IE4
     if (typeof document.all[ident] == "object")
     Elem = document.all[ident];
     else Elem = void(0);
   }
 else if (document[ident]) { //Netscape alternative
     Elem = document[ident];
   }
 else Elem = void(0);
 return(Elem);
}

function ctrImage() {
	// The hidden counter which gathers statistics
	// Insert the URL call as the source of an image which is added via DOM
	// Safari needs this picture or it misses the horizontal scroller
	// Note that this is outside the HV-Centering, contained in <alles>
	// alert('Counter-Call');
	var ref = '';
	if (document.images && document.referrer && document.referrer.length>0)
	 { ref += escape(document.referrer); }
	var fbt = '..', cookies = ' U';
	if (screen.colorDepth && screen.colorDepth != null) {
		fbt = screen.colorDepth;
		if (fbt.length == 1) { fbt = '0' + screen.colorDepth; }
		}
	if (typeof navigator.cookieEnabled == 'boolean')
		cookies = navigator.cookieEnabled ? ' J' : ' N';
	ref += '&sh='+screen.height+'&sw='+screen.width+'&user='+fbt+cookies;
	var ctrImg = document.createElement('img');
	ctrImg.width = 1;
	ctrImg.height = 1;
	ctrImg.src = '/cgi-bin/counter.pl?id=1&rl=1200&ref=' + ref;
	// ctrImg.src = '/cgi-bin/counter.pl?id=1&rl=0&ref=' + ref;
	var ctrDiv = getElemId('ctrDiv');
	ctrDiv.appendChild(ctrImg);
	return;
}

function neuAufbau() {
	var objHtml = getElemId('myhtml');
	var objBody = getElemId('mybody');
	var objSite = getElemId('website');
	var objMain = getElemId('main');
	var objHead = getElemId('kopf');

	// 1: IE and Opera need this tweak (otherwise always space for degrading scroller)
	objMain.style.overflow = 'hidden';

	// 2: For div.website height 100% most browsers take objBody.offsetHeight
	// But we must modify this to objBody.clientHeight - or the min-height
	// This also cures the "old browser" problem of min-height not obeyed.
	var currViewHeight = objBody.clientHeight;
	var currSiteHeight = objSite.offsetHeight;
	var useHeight = (currViewHeight > intMinSiteHeight) ? currViewHeight : intMinSiteHeight;
	if (currSiteHeight != useHeight) {
		objSite.style.height = useHeight + 'px';
		currSiteHeight = useHeight;
	}

	// 3: Feature-Workaround - WebKit and IE8 calculate %-heights
	//    to nearest enclosing Fixed height (NOT % height) (here: Viewport)
	var intMainHeight = objMain.offsetHeight;	// Should be 94% of 'website'
	var intAvailForMain = Math.floor( currSiteHeight * 0.94 );
	// alert(intAvailForMain + ' / ' + intMainHeight);
	if (intMainHeight < intAvailForMain) {
		// Main height set to available in pixel
		intMainHeight = intAvailForMain;
		var objHeader = getElemId('header');
		if (objHeader) {
			var intAvailForHeader = Math.floor( currSiteHeight * 0.03 );
			objHeader.style.height = intAvailForHeader + 'px';
		}
		objMain.style.height = intMainHeight + 'px';
		blnHeightToHtml = true;
	}
	else if (blnHeightToHtml && intMainHeight > intAvailForMain) {
		// Viewport-Height has decreased but Main height was set in pixel
		intMainHeight = intAvailForMain;
		var objHeader = getElemId('header');
		if (objHeader) {
			objHeader.style.height = '3%';
			objMain.style.height = '94%';
		} else {
			objMain.style.height = '97%';
		}
	}

	// 4: Set content scroller height
	// var objExtra = getElemId('extra');
	// offsetHeight is the height including any margins, padding and borders
	var intMainHeight = objMain.offsetHeight;	// Should be 94% of body
	var intHeadHeight = objHead.offsetHeight;
	// var intExtraHeight = objExtra.offsetHeight;
	// var intScrollerHeight = intMainHeight - intHeadHeight - intExtraHeight;
	var intScrollerHeight = intMainHeight - intHeadHeight;
	if (intScrollerHeight < 2) intScrollerHeight = 2;
	// alert(intMainHeight + ' / ' + intHeadHeight + ' / ' + intScrollerHeight);
	// 4A:
	var objScroller = getElemId('scroller');
	objScroller.style.height = intScrollerHeight + 'px';
	// 4B: Allow for the content area borders here
	var objRealScroller = getElemId('realscroller');
	objRealScroller.style.height = (intScrollerHeight - 2) + 'px';

	// 5: WebKit fails to recalculate 'auto' width when degrading scroller goes
	objHead.style.width = objMain.offsetWidth + 'px';
	// objExtra.style.width = objMain.offsetWidth + 'px';

	// 6: IE6 displays the main bottom corner positioned to
	// objScroller.scrollHeight rather than objScroller.clientHeight
	var agent = navigator.userAgent.toLowerCase()
	// alert(agent);
	if (agent.indexOf("msie 6.0") > -1) {
		var objMainEckeUnten = getElemId('main_ecke_unten');
		var intTopPosn = intScrollerHeight - objMainEckeUnten.offsetHeight;
		objMainEckeUnten.style.top = intTopPosn + 'px';
	}

	return;
}

// This is called by the onload-event of every page
function startUp(pathprefix,testforjavascript) {
	// First we deal with the client height topic
	var objMain = getElemId('main');
	if (objMain != null && typeof objMain.clientHeight == 'number') {
		// BiR-Special: Die Degrading-Workarounds for IE6/7 rückgängig machen
		var objScroller = getElemId('scroller');
		// Unresolved Problem:
		// Dropdowns are misplaced (right) in IE6/7 when objScroller is 'relative'
		objScroller.style.position = 'relative';
		var objMainEckeUnten = getElemId('main_ecke_unten');
		objMainEckeUnten.style.display = 'block';
		// Weiter wie üblich
		neuAufbau();
		window.onresize = neuAufbau;
		}
	// Then we perform the ajax-based test for javascript
	if (testforjavascript) { ajaxStartUp(pathprefix); }
	// Now we look if the page contains an Email-Adress
	if (getElemId('email1')) { ajaxOnload(pathprefix); }
	// Finally the statistics counter - but only on initial website entry
	var re = /^Seminarraum_Flurstr10/;
	if (! re.test(window.name)) {
		// It IS the initial website entry - set the window name
		window.name = 'Seminarraum_Flurstr10';
		// And call the counter
		ctrImage();
	}
	// Caveat: Since hardly any site sets the window name, it tends to stay this way
	//         meaning that subsequent visits - even hours later - are not noted
	// The alternative is to have the counter called by every page and the data is
	// not recorded by the counter software if the IP address is (time-)locked out.
	// Display the font size selection elements
	setUserOptions();
	return;
}

// This would be called by the onunload-event of every page
// Except that AdMuncher overwrites that event by default
function unLoadPage() {
	loading = true;		// For colorflow
	saveSettings();		// For font size
	return;
}

// BEGIN Code for custom tooltips

function findPos(obj) {
	// Internal function - find position of obj (a menu <a>) relative to viewport
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			if (obj.tagName == 'TABLE') {
				// Only IE5 / IE6 have a table, they miss counting an LI Element
				// alert('TABLE class:' + obj.className +
				//  ' offsetLeft: ' + obj.parentElement.parentElement.offsetLeft +
				//  ' offsetTop: ' + obj.parentElement.parentElement.offsetTop);
				curleft += obj.parentElement.parentElement.offsetLeft;
				curtop += obj.parentElement.parentElement.offsetTop;
				}
			else {
				// alert(obj.tagName + ' id:' + obj.id + ' class:' + obj.className +
				//  ' offsetLeft: ' + obj.offsetLeft + ' offsetTop: ' + obj.offsetTop);
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
				}
		}
	}
	return [curleft,curtop];
}

var currentflyoutsublevel = 0;
var currentTimerHandle = null;

function showtip(currobj, sublevel, addleft, addtop, text) {
	// External function. At onmouseout text is the empty string.
	// Called at onmouseover / onmouseout to display / hide a tooltip
	// alert(currobj.tagName + ' ' + currobj.id + ' ' + currobj.className + ' ' + sublevel);
	if (sublevel < currentflyoutsublevel) return;
	currentflyoutsublevel = (text == '') ? sublevel - 1 : sublevel;
	showtipwork(currobj, addleft, addtop, text);
	return true;
}

function displaytip() {
	// Internal function - called via timer
	var tip = getElemId('fotooltip');
	tip.style.display = 'block';
	currentTimerHandle = null;
	return true;
}

function showtipwork(currobj, addleft, addtop, text) {
	// Internal function - do the actual work of displaying / hiding a tooltip
	var tip = getElemId('fotooltip');
	if (text == '') {
		// Clear any timer and hide the tooltip
		if (currentTimerHandle != null) {
			window.clearTimeout(currentTimerHandle);
			currentTimerHandle = null;
			}
		tip.style.display = 'none';
		return true;
		}
	var arrpos = findPos(currobj);
	// alert(arrpos[0] + ' ' + arrpos[1]);
	// Set up the tooltip position and content
	tip.style.left = (arrpos[0]+addleft) + 'px';
	tip.style.top = (arrpos[1]+addtop) + 'px';
	tip.firstChild.data = text;
	// Have it displayed after a delay of 600ms
	currentTimerHandle = window.setTimeout('displaytip()',600);
	return true;
}

function tipflow(sublevel, addleft, addtop, text, ti, cssclass) {
	// External function - like showtip() but also handle color flow
	if (sublevel < currentflyoutsublevel) return;
	currentflyoutsublevel = (text == '') ? sublevel - 1 : sublevel;
	var obj = getElemId('flow' + ti);
	showtipwork(obj, addleft, addtop, text);
	if (!loading) {
		// alert('flow ' + ti + ' ' + cssclass);
		if (cssclass != '') flowup(ti,cssclass); else flowdn(ti);
		}
	return true;
}

// END Code for custom tooltips

// Sweet little routines to scroll the page to the top or bottom
function pgtop(elem) {
	var obj = getElemId(elem);
	if (obj && typeof(obj.scrollTop) == 'number') obj.scrollTop = 0;
	else window.location.href = window.location.href;
	return;
}
function pgbot(elem) {
	var obj = getElemId(elem);
	if (obj && typeof(obj.scrollTop) == 'number') obj.scrollTop = obj.scrollHeight;
	return;
}
