// Tim Reeves JavaScript for Denk, Stand 2010-02-25
// 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 (these values used both in 94% and fixed height)
var intMinSiteHeight = 400;
var intMinSiteWidth = 958;
// END CONFIG

// Recognise IE 6 + 7
var isIE6 = false, isIE7 = false;

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 objKopf = getElemId('kopf');
	var objFuss = getElemId('fuss');

	var objHeader = getElemId('header');
	var objFooter = getElemId('footer');

	// 1: IE6 stupidly reduces the size of the <body> element...
	if (isIE6) {
		objBody.style.height = objHtml.clientHeight + 'px';
		objBody.style.width  = objHtml.clientWidth + 'px';
	}

	// 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;
	}
	// 2b: Same game for div.website width
	var currViewWidth = objBody.clientWidth;
	var currSiteWidth = objSite.offsetWidth;
	var useWidth = (currViewWidth > intMinSiteWidth) ? currViewWidth : intMinSiteWidth;
	if (currSiteWidth != useWidth) {
		objSite.style.width = useWidth + 'px';
		currSiteWidth = useWidth;
	}

	if (objHeader.offsetHeight == 0) {
		// FIXED HEIGHT - perform vertical centering.
		// Degrading mode has a top margin on div.main
		// Just overwrite that with (browser - main) / 2 (but not less than zero)
		var intMainHeight = objMain.offsetHeight;
		if (intMainHeight+1 < currSiteHeight) {
			var intMainTopMargin = Math.floor( (currSiteHeight - intMainHeight) / 2 );
		}
		else {
			var intMainTopMargin = 0;
		}
		objMain.style.marginTop = intMainTopMargin + 'px';

	}	// fixed

	else {
		// 94% HEIGHT

		// 3: Feature-Workaround - WebKit and IE8 calculate %-heights
		//    to nearest enclosing Fixed height (NOT % height) (here: Viewport)
		// Avoid rounding errors which may lead to <main> being too short or long
		// Correct rounding up on Header/Footer to rounding down (e.g. for IE7)
		// Since browsers differ a lot simply ALWAYS do it ourselves !!!
		// offsetHeight is the height including any margins, padding and borders
		var intMainHeight = objMain.offsetHeight;	// Should be 94% of 'website'

		var intAvailForHdrFtr3pc = Math.floor( currSiteHeight * 0.03 );
		var intAvailForMainBySubtractAvailHdrFtr =
			(objHeader) ? currSiteHeight - (2 * intAvailForHdrFtr3pc)
						: currSiteHeight - intAvailForHdrFtr3pc;

		var intCurrFooter = objFooter.offsetHeight, intCurrHeader = 0;
		if (objHeader) intCurrHeader = objHeader.offsetHeight;

		/*alert('availHdrFtr3pc=' + intAvailForHdrFtr3pc +
			', currHeader=' + intCurrHeader +
			', currFooter=' + intCurrFooter);
		alert('siteHeight=' + currSiteHeight +
			', mainHeight=' + intMainHeight +
			', AMBSAHF=' + intAvailForMainBySubtractAvailHdrFtr
			);*/

		if (intCurrHeader > 0 && intCurrHeader != intAvailForHdrFtr3pc) {
			objHeader.style.height = intAvailForHdrFtr3pc + 'px';
		}
		if (intCurrFooter != intAvailForHdrFtr3pc) {
			objFooter.style.height = intAvailForHdrFtr3pc + 'px';
		}
		if (intMainHeight != intAvailForMainBySubtractAvailHdrFtr) {
			intMainHeight = intAvailForMainBySubtractAvailHdrFtr;
			objMain.style.height = intMainHeight + 'px';
		}

		// 4: Set content scroller height
		// var objExtra = getElemId('extra');
		var intKopfHeight = objKopf.offsetHeight;
		var intFussHeight = objFuss ? objFuss.offsetHeight : 0;
		var intScrollerHeight = intMainHeight - intKopfHeight - intFussHeight;
		if (intScrollerHeight < 0) intScrollerHeight = 0;
		// alert(intMainHeight + ' / ' + intKopfHeight + ' / ' + intFussHeight + ' / ' + intScrollerHeight);
		var objScroller = getElemId('scroller');
		objScroller.style.height = intScrollerHeight + 'px';

	}	// 94%

	// 5: IE fixes - sigh...
	if (isIE6) {
		// alert('IE6');
		// Add any IE6 fix here
	}
	if (isIE7) {
		// alert('IE7');
		// Add any IE7 fix here
	}

	return;
}

// This is called by the onload-event of every page
function startUp(pathprefix,testforjavascript) {

	// Recognise IE 6 + 7 (store in global variables)
	if (navigator.appName.indexOf('Microsoft') >= 0) {
		var ua = navigator.userAgent.toLowerCase();
		// alert(an); alert(ua);
		// Note: IE8 ua does NOT contain the string 'msie 6'
		// Test ua for IE7 first - it contains 'msie 6.0'
		if (ua.indexOf('msie 7') != -1) isIE7 = true; else
		if (ua.indexOf('msie 6') != -1) isIE6 = true;
	}

	// First we deal with the client height topic
	var objMain = getElemId('main');
	if (objMain != null && typeof objMain.clientHeight == 'number') {
		if (isIE6) {
			// IE6 has some crazy css styles for degrading mode; reset them
			var objHtml = getElemId('myhtml');
			var objBody = getElemId('mybody');
			var objSite = getElemId('website');
			objHtml.style.overflow = 'hidden';
			objBody.style.overflow = 'auto';
			objBody.style.height = '100%';
			objBody.style.width = '100%';
			objSite.style.width = '100%';
		}
		neuAufbau();
		// IE6+7 benötigen einen 2. Aufruf für die richtige Höhe von div.scroller
		if (isIE6 || isIE7) neuAufbau();
		window.onresize = neuAufbau;
		}

	// Perform the ajax-based test for JavaScript
	if (testforjavascript) { ajaxStartUp(pathprefix); }

	// Look if the page contains an Email-Adress
	if (getElemId('email1')) { ajaxOnload(pathprefix); }

	// The statistics counter - but only on initial website entry
	var re = /^Denk_Website/;
	if (! re.test(window.name)) {
		// It IS the initial website entry - set the window name
		window.name = 'Denk_Website';
		// 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();

	// Zeige "Nach oben" wenn die Seite diesen Absatz enthält
	var objNachOben = getElemId('nachoben');
	if (objNachOben != null) objNachOben.style.display = 'block';

	return;
}

// This would be called by the onunload-event of every page
// Except that AdMuncher overwrites that event by default
function unLoadPage() {
	g_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);
	// Denk does not use colorflow
	return true;
	if (!g_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;
}

