// navigation (like www.musik-davos.ch)
// copyright frog style web engineering

var settings = {prefix_nav:'nav_',prefix_subnav:'subnav_'};
var nav = {};
var subnav = {};

function initNav(custSett) {
	// custom settings - copy
	for(var sn in custSett) settings[sn] = custSett[sn];

	// get all divs
	var divs = document.getElementsByTagName('div');

	// find the nav buttons and subnav containers
	for(var i=0; i<divs.length; i++) {
		var d = divs[i];
		if(d.id != null) {
			if(d.id.indexOf(settings.prefix_nav) == 0) {
				// it's a nav button - add to the collection
				var rid = d.id.substr(settings.prefix_nav.length);
				nav[rid] = d;
			}
			else if(d.id.indexOf(settings.prefix_subnav) == 0) {
				// it's a nav button - add to the collection
				var rid = d.id.substr(settings.prefix_subnav.length);
				subnav[rid] = d;
				// keep open?
				if(settings.visible == null) settings.visible = rid;
			}
		}
	}

	// now that we have them, add stuff
	for(var nn in nav) {
		// get the pointer
		var n = nav[nn];
		// use the hand cursor
		n.style.cursor = 'pointer';
		// navs need actions to show their subnavs, but only if those are available
		if(subnav[nn] != null) {
			// add the onclick action
			n.onclick = function(){toggleMenu(this.id);}
			// mouseover / out
			n.onmouseover = function(){startSShow(this.id)}
			n.onmouseout = function(){stopSShow(this.id)}
		}
		else {
			// find the a child node
			var an;
			var cn = n.childNodes;
			var j;
			for(j=0; j<cn.length;j++) {
				if(cn[j].nodeType == 1 && cn[j].tagName == 'A') {
					an = cn[j];
					break;
				}
			}
			if(an != null && an.href != null)  {
				n.onclick = function(){location.href = this.childNodes[j].href;}
			}
		}
	}
	
	for(var sn in subnav) {
		var s = subnav[sn];
		// get the padding

		var pt = 0, pb = 0;
		if(s.currentStyle) {
			pt = parsePxInt(s.currentStyle.paddingTop);
			pb = parsePxInt(s.currentStyle.paddingBottom);
		}
		else if(document.defaultView) {
			pt = parsePxInt(document.defaultView.getComputedStyle(s, '').getPropertyValue("padding-top"));
			pb = parsePxInt(document.defaultView.getComputedStyle(s, '').getPropertyValue("padding-bottom"));
		}
		// remove padding
		s.style.paddingTop = '0px';
		s.style.paddingBottom = '0px';
		// re-insert padding differently
		var iht = s.innerHTML;
		if(pt > 0) iht = '<img src="images/spacer.gif" width="1" height="' + pt + '" border="0" />' + iht;
		if(pb > 0) iht = iht + '<img src="images/spacer.gif" width="1" height="' + pb + '" border="0" />';
		s.innerHTML = iht;
		
		// store its initial height
		s.initialHeight = s.offsetHeight;
		s.style.height = s.initialHeight+'px';
		// hide overflow
		s.style.overflow = 'hidden';

		// if not the one we want to keep visible, set height to 0px
		if(sn != settings.visible) {
			s.style.height = '0px';
		}
		else {
			// store some things concerning the visible one
			document.visibleSubmenu = s;
		}
	}
}

function toggleMenu(fname) {
	var name = fname.substr(settings.prefix_nav.length);
	var s = subnav[name];

	// make sure no timeout is running
	if(document.sshowTimeout) {
		window.clearTimeout(document.sshowTimeout);
		document.sshowTimeout = null;
	}

	if(!document.activeCloseSubmenu && !document.activeOpenSubmenu) {
		if(s.offsetHeight > 0) {
			document.activeCloseSubmenu = s;
			document.submenuCloseIntv = window.setInterval(decreasemenuheight, 25);
			// if a menu is actively closed, no other menu should be visible
			document.visibleSubmenu = null;
		}
		else {
			document.activeOpenSubmenu = s;
			document.submenuOpenIntv = window.setInterval(increasemenuheight, 25);
			// if a menu is opened, make sure any other open menu is closed (can only be one)
			if(document.visibleSubmenu) {
				document.activeCloseSubmenu = document.visibleSubmenu;
				document.submenuCloseIntv = window.setInterval(decreasemenuheight, 25);
			}
			document.visibleSubmenu = s;
		}
	}
}
function decreasemenuheight() {
	if(document.activeCloseSubmenu) {
		var s = document.activeCloseSubmenu;
		if(s.offsetHeight > 0) {
			var d = Math.min(s.offsetHeight, 50);
			s.style.height = (s.offsetHeight - d) + 'px';
		}
		else {
			clearInterval(document.submenuCloseIntv);
			document.activeCloseSubmenu = null;
		}
	}
	else {
		clearInterval(document.submenuCloseIntv);
	}
}
function increasemenuheight() {
	if(document.activeOpenSubmenu) {
		var s = document.activeOpenSubmenu;
		if(s.offsetHeight < s.initialHeight) {
			var d = Math.min(s.initialHeight-s.offsetHeight, 50);
			s.style.height = (s.offsetHeight + d) + 'px';
		}
		else {
			clearInterval(document.submenuOpenIntv);
			document.activeOpenSubmenu = null;
		}
	}
	else {
		clearInterval(document.submenuOpenIntv);
	}
}
function startSShow(fname) {
	var name = fname.substr(settings.prefix_nav.length);
	var s = subnav[name];
	if(document.visibleSubmenu == s) return;

	// else: start an intv to show the submenu
	document.sshowTimeout = window.setTimeout("toggleMenu('" + fname + "')", 500);
}
function stopSShow(name) {
	// var s = document.submenus[name];
	if(document.sshowTimeout) {
		window.clearTimeout(document.sshowTimeout);
		document.sshowTimeout = null;
	}
}

function parsePxInt(p) {
	var n = parseInt(p);
	if(isNaN(n)) return 0;
	return n;
}

// finds the absolute position of an element
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}