// FindWord JavaScript, Stand 2006-08-03
// with xml-compliant code to insert an img node for the counter

// FAUSTREGEL: READ an attribute and SET a style (with a text string) !!!

var nutzHoehe = 0;
var globReferrer = '';
var globOrigin = '';

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 getClientHeight() {
	/* Get the EFFECTIVE client height of an object, i.e. excluding any scrollbar */
	/* Note that offsetHeight (a) includes any scrollbar, and (b) in older browsers */
	/* is only available for the window itself (and maybe document.body) so in fact */
	/* our only chance is to see if the browser provides clientHeight or just give up */
	var curHeight = 0;
	var viewport = window.opera ? getElemId('mybody') : getElemId('myhtml');
	if (viewport != null && typeof viewport.clientHeight == 'number') {
		curHeight = viewport.clientHeight;
		}
	// alert(curHeight);
	return curHeight;
}

function neuAufbau() {
	/* Don't make the content area shorter than the navigation height. */
	/* The navigation height varies according to which submenu is open */
	var effektivHoehe = getClientHeight();
	if (nutzHoehe != effektivHoehe) {
		// Allow space for "English" link at bottom of mynavhg
		var myNavHeight = getElemId('mynav').clientHeight + 32;
		var myNav2 = getElemId('mynav2');
		if (myNav2) {
			// If 'mynav2' is present and longer than 'mynav', use its height
			var myNav2Height = myNav2.clientHeight;
			if (myNav2Height > myNavHeight) myNavHeight = myNav2Height;
		}
		// alert(myNavHeight);
		// Navigation starts at 3% of effektivHoehe, so add on 4% of that
		myNavHeight += (effektivHoehe*4)/100;
		nutzHoehe = effektivHoehe > myNavHeight ? effektivHoehe : myNavHeight;
		if (window.opera) getElemId('myhtml').style.height = nutzHoehe + 'px';
		getElemId('sizer').style.height = nutzHoehe + 'px';
	}
	return;
}

// Not now used - initially the navigation covered the full viewport height
function neuAufbau_nav100percenthoehe() {
	/* Note that setting 'height' (if the object has that attribute) does not change */
	/* the objects clientHeight (that which is bequethed), but clientHeight is read only */
	var effektivHoehe = getClientHeight();
	if (nutzHoehe != effektivHoehe) {
		var myNavHeight = getElemId('mynav').clientHeight;
		var myNav2 = getElemId('mynav2');
		if (myNav2) {
			// If 'mynav2' is present and longer than 'mynav', use its height
			var myNav2Height = myNav2.clientHeight;
			if (myNav2Height > myNavHeight) myNavHeight = myNav2Height;
		}
		// alert(myNavHeight);
		nutzHoehe = effektivHoehe > myNavHeight ? effektivHoehe : myNavHeight;
		getElemId('myhtml').style.height = nutzHoehe + 'px';
		getElemId('mybody').style.height = nutzHoehe + 'px';
		getElemId('sizer').style.height = nutzHoehe + 'px';
		getElemId('mynavhg').style.height = nutzHoehe + 'px';
		if (myNav2) {
			getElemId('mynav2hg').style.height = nutzHoehe + 'px';
		}
	}
	return;
}

function ctrImage(ref) {
	// The hidden counter which gathers statistics
	// Safari needs this picture or it misses the horizontal scroller
	// Note that this is outside the HV-Centering, contained in <alles>
	var refPar = '';
	if (ref.length>0) refPar += ref;
	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';
	refPar += '&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=1800&ref=' + refPar;
	// ctrImg.src = '/cgi-bin/counter.pl?id=1&rl=0&ref=' + refPar;
	var ctrDiv = getElemId('ctrDiv');
	ctrDiv.appendChild(ctrImg);
	return;
}

/* Dummy declaration, overloaded if ajax.js included afterwards */
function ajaxOnload() {
	return;
}

function getFirefox() {
	var getFF = "Am Besten holen Sie den modernen Gratis-Browser \"Firefox\" \nper DownLoad. Wollen Sie jetzt Firefox herunterladen?"
	var ans = confirm(getFF);
	if (ans == true) location.href = 'http://www.mozilla-europe.org/de/products/firefox/';
	return;
}

function fromHex(input) {
	var output = '', ts = '', tn = 0;
	for (var i=0; i<input.length; i=i+2) {
		ts = input.substr(i,2);
		tn = parseInt(ts,16);
		ts = String.fromCharCode(tn.toString());
  		output += ts;
  	}
  	return output;
}

function toHex(input) {
	var output = '', ts = '', tn = 0;
	for (var i=0; i<input.length; i++) {
		tn = parseInt(input.charCodeAt(i));
		ts = tn.toString(16);
		if (ts.length == 1) ts = '0' + ts;
  		output += ts;
  	}
  	return output;
}

// This is called extra early if the page loads an Iframe, otherwise in startUp()
// It sets the window name, after Referrer extraction for deep links,
// returns Referrer if initial call, otherwise an empty string.

function setWinName() {

	// We arrive in the calling html file in three possible ways:
	// 1. From within our own website - nothing needs doing - window.name == "FindWord_..."
	// 2. From a deep link - orig_file and any referrer (in that order) coded in window.name
	// 3. By direct call of this file from outside the website - use document.referrer

	// Do nothing if not the initial website entry for this browser window
	// Window name contents:
	// Set in this file:     "FindWord_<Hex-Referrer>"
	// Set in "funktionen":  "fw_jmp_mem_<Hex-Zieldatei>{fw_ref_mem_<Hex-Referrer>}"
	// Set Fragen / Presse:  "fw_sec_mem_<Hex-Zieldatei>{fw_ref_mem_<Hex-Referrer>}"

	var re = /^FindWord_/;
	if (! re.test(window.name)) {

		// It IS the initial website entry - decode the window name
	 	var ref = '';
		if (document.images && document.referrer && document.referrer.length > 0)
			ref = document.referrer;

		var origin = '';
		var loadCont2 = false;

		if (window.name.indexOf("fw_jmp_mem_") == 0) {
			// Case 2 - Deep link to a function
			var teile = window.name.split("fw_ref_mem_");
			// origin gets the name of the targeted deep link file
			origin = 'funktionen/' + fromHex(teile[0].slice(11));
			// Note any referrer
			if (teile.length == 2) {
				// Yeah, we have a referrer
				ref = origin + '=>' + fromHex(teile[1]);
			}
			else {
				// Nope, no referrer - just the deep link
				ref = origin;
			}
			loadCont2 = true;
		}
		else
		if (window.name.indexOf("fw_sec_mem_") == 0) {
			// Case 2 - Deep link to "fragen" or "presse"
			var teile = window.name.split("fw_ref_mem_");
			// origin gets the name of the targeted deep link file
			origin = fromHex(teile[0].slice(11));
			// Note any referrer
			if (teile.length == 2) {
				// Yeah, we have a referrer
				ref = origin + '=>' + fromHex(teile[1]);
			}
			else {
				// Nope, no referrer - just the deep link
				ref = origin;
			}
			// For njwcor.html we must also overload the default presse.html
			if (origin == 'njwcor.html') loadCont2 = true;
		}
		else {
			// Nothing encoded in the window name
		}
		// alert('ref: ' + ref);
		// Set the window name for files loaded into an iframe
		// - appending any information about origin / referrer
		window.name = 'FindWord_';
		if (ref != '') {
			window.name += toHex(ref);
			ref = escape(ref);
			}
		// Note any pending load of a deep-linked "function" file into the iframe
		// This can only be done after the page is established
		if (loadCont2) globOrigin = origin;

		// Finally note the referrer gloablly for the statistics counter call
		globReferrer = ref;

		return true;
		}	// Initial Website Entry

	return false;
}

// This is called by the onload-event of every main page
function startUp() {

	// First we deal with the client height topic
	var tHoehe = getClientHeight();
	if (tHoehe > 0) {
		neuAufbau();
		window.onresize = neuAufbau;
	}

	// Secondly we set the window name and call the visit counter if not done already
	// Pages which load an Iframe whose source file may be deep-linked do this extra early
	// Thirdly the statistics counter, if this was the initial entry to the website
	if (globReferrer != '') ctrImage(globReferrer);	// Page with Iframe called setWinName()
	else
		if (setWinName()) ctrImage(globReferrer);

	// Load any deep-linked "function" file into the iframe
	if (globOrigin != '') {
		window.fw_cont2.location.replace(globOrigin);
		}

	// Finally we look if the page contains an Email-Adress
	// Note that the page must then itself include the Ajax library
	if (document.getElementById) {
		if (typeof document.getElementById('email1') == 'object') { ajaxOnload(); }
	}
	else {
		alert("Sie verwenden ein sehr veralteter Browser, mit\ndem diese Website nicht korrekt dargestellt wird.");
		getFirefox();
	}

	return;
}

// Sweet little routine to scroll the page back to the top
function pgtop () {
	var obj = getElemId('scroller');
	if (obj && typeof(obj.scrollTop) == 'number') obj.scrollTop = 0;
	else window.location.href = window.location.href;
	return;
}

// This one scrolls an examples page to the klick buttons
function posKlick () {
	var obj = getElemId('scroller');
	if (obj && typeof(obj.scrollTop) == 'number') {
		var kli = getElemId('klicks');
		// offsetTop ist eine Microsoft-Eigenschaft und bezieht sich auf das Elternelement
		// Deswegen muss "klicks" ein direktes Kind von "scroller" sein, sonst stimmts nicht
		obj.scrollTop = kli.offsetTop;
		}
	return;
}

