// nhStandard.js: Standard functions
// (c) Niels Horn
// 1.xx - 13/09/2001: old DHTML version
// 2.00 - 08/03/2007: equal height for divs
// 2.01 - 25/08/2009: menu functions
// 2.02 - 29/08/2009: nhQs2Var function
// 2.03 - 29/05/2011: setWide function for auto-width contents

/*
 * Equal height for divs
 */

// based on work by Paul@YellowPencil.com and Scott@YellowPencil.com
function setTall() {
	//alert('tall');
	if (document.getElementById) {
		var divs = new Array(
			document.getElementById('navigation'), 
			document.getElementById('content'));
		
		// Let's determine the maximum height out of all columns specified
		var maxHeight = 0;
		for (var i = 0; i < divs.length; i++) {
			divs[i].style.height="auto";
			if (divs[i].offsetHeight > maxHeight) maxHeight = divs[i].offsetHeight;
		}
		
		// Let's set all columns to that maximum height
		for (var i = 0; i < divs.length; i++) {
			divs[i].style.height = maxHeight + 'px';

			// Now, if the browser's in standards-compliant mode, the height property
			// sets the height excluding padding, so we figure the padding out by subtracting the
			// old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
			if (divs[i].offsetHeight > maxHeight) {
				divs[i].style.height = (maxHeight - (divs[i].offsetHeight - maxHeight)) + 'px';
			}
		}
	}
}

/*
 * Set width of contents from header - navigation
 */
function setWide() {
	//alert('wide');
	// Get width of header, which is 100%
	var maxWidth = document.getElementById('header').offsetWidth;
	var navWidth = document.getElementById('navigation').offsetWidth;
	// Width of contents is the difference
	var contWidth=maxWidth - navWidth;
	elCont=document.getElementById('content');
	elCont.style.width=contWidth + 'px';
	// Set width of half boxes if present
	var coll=elCont.getElementsByTagName("p");
	contHWidth=contWidth / 2 - 55;
	for (i=0; i<coll.length; i++){
		if (coll[i].className.substr(0,3)=="box"){
			if (coll[i].className.indexOf("left")>=0 || coll[i].className.indexOf("right")>=0){
				coll[i].style.width=contHWidth + 'px';
			}
		}
	}
	t=setTall();
}


// Set up Event Listener - the script that allows us to use the addEvent call below
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

addEvent(window, 'load', setTall, false);
addEvent(window, 'load', setWide, false);
addEvent(window, 'resize', setTall, false);
addEvent(window, 'resize', setWide, false);

/*
 Menu functions
 */

function nhSubOpen(level){
	var t = "";
	var e = document.getElementById('navigation');
	//alert(e.offsetHeight);
	var coll = e.childNodes;
	l2="nav"+level+"-";
	for (i=0;i<coll.length;i++) {
		t=coll[i].id+"*";
		if (t.indexOf(l2)>=0) {
			if (t.length == l2.length+3){
				coll[i].style.display='inherit';
			}
		}
	}
	e=document.getElementById("navBTimg"+level);
	ksub=e.src;
	k=ksub.indexOf("_d.");
	ksub=ksub.substr(0,k)+"_u."+ksub.substr(k+3);
	e.src=ksub;
	e=document.getElementById("navBTa"+level);
	ksub=e.href;
	k=ksub.indexOf("Open");
	ksub=ksub.substr(0,k)+"Close"+ksub.substr(k+4);
	e.href=ksub;

	t=setTall();
}

function nhSubClose(level){
	var t = "";
	var e = document.getElementById('navigation');
	var coll = e.childNodes;
	l2=level+"-";
	for (i=0;i<coll.length;i++) {
		t=coll[i].id+"*";
		if (t.indexOf("nav"+l2)>=0) {
			coll[i].style.display='none';
			//find corresponding <a></a>
			e=document.getElementById("navBTa"+coll[i].id.substr(3));
			if (e != null) {
				ksub=e.href;
				k=ksub.indexOf("Close");
				if (k>=0) {
					ksub=ksub.substr(0,k)+"Open"+ksub.substr(k+5);
					e.href=ksub;
				}
			}
			//find corresponding <img />
			e=document.getElementById("navBTimg"+coll[i].id.substr(3));
			if (e != null) {
				ksub=e.src;
				k=ksub.indexOf("_u.");
				if (k>=0) {
					ksub=ksub.substr(0,k)+"_d."+ksub.substr(k+3);
					e.src=ksub;
				}
			}
		}
	}
	e=document.getElementById("navBTimg"+level);
	ksub=e.src;
	k=ksub.indexOf("_u.");
	ksub=ksub.substr(0,k)+"_d."+ksub.substr(k+3);
	e.src=ksub;
	e=document.getElementById("navBTa"+level);
	ksub=e.href;
	k=ksub.indexOf("Close");
	ksub=ksub.substr(0,k)+"Open"+ksub.substr(k+5);
	e.href=ksub;

	t=setTall();
}

/*
 other functions
 */

//Get value from link (does not work?)
function gup(name){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return "";
	else
	    return results[1];
}

//Get value from QueryString
function nhQs2Var(arg){
	var i,j,k,ls;
	ls=unescape(location.search);
	i=ls.indexOf(arg);
	if (i==-1) return "";
	j=ls.indexOf("=",i);
	if (j==-1) return "";
	k=ls.indexOf("&",j);
	if (k==-1) k=ls.length+1;
	return ls.substr(j+1,k-j-1)
}


