// navigation scripts by atelier w, stefan niederhauser
//

h = location.host;
PATH = (h.indexOf('node.ch') >= 0 || h.indexOf('atelier-w.ch') >= 0 || h.indexOf('200.4.180') >= 0) ? '/securite/frnew/' : '/'; 
navHtmlPre = PATH + "html/sec_";
navHtmlPost = ".html";
navTopPre = PATH + "gif/sec_nav_";
navTopPost = ".gif";
navTopActive = "a";
navTopXXX = "a";
navOtherFrame = "";
navFrameTarget = "";
navTopName = "navTop";
navSubName = "navSub";

navTopNum = 9;
navSubNum = Array(0, 5, 4, 2, 3, 2, 3);

navLinks = new Object();
navLinks["2_11"] = "!popup('sec_2_11_win_1.html', 690, 330);";

/*
navLinks["1_1"] = PATH + "html/sec_1.html";
navLinks["2_1"] = PATH + "html/sec_2.html";
navLinks["3_1"] = PATH + "html/sec_3.html";
navLinks["4_1"] = PATH + "html/sec_4.html";
navLinks["5_1"] = PATH + "html/sec_5.html";
navLinks["6_1"] = PATH + "html/sec_6.html";
*/
navOutDelay = 0;
navOverDelay = 0;

navTopLast = 0;
navSubLast = 0;
navTopAct = 0;
navSubAct = 0;
navTimer = 0;
navOverTimer = 0;
imgCache = new Object();
navNoSub = false;
navSynced = false;

function initNav() {
	navSync(document.location.href);
	//resizeFix(true);
	//navPreload();
	setTimeout('navPreload()', 1000);
}


function resizeFix(init) { 
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=resizeFix; }}
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}

// main navigation functions

function navSync(str, t, s) {
	if (navSynced)
		return true;
	// only set ACT - Gif (x)

	if (t > 0) {
		navSynced = true;
		if (s > 0)
		  return navSubActivate(t, s);
		else 
		  return navTopActivate(t);
	}
	// decode the filename ( /path/topic_x_y.html)
	if (str.lastIndexOf('/') >= 0) str = str.substring(str.lastIndexOf('/')+1);
	var i = str.indexOf('_');
	var j = str.indexOf('_', i+1);
	var k = str.indexOf('.', j+1);
	var topic = str.substring(0, i);
	if (j >=0) {
	  var top = str.substring(i+1, j);
	  var sub = str.substring(j+1, k);
	} else
	  var top = str.substring(i+1, str.indexOf('.', i+1));
	if (topic != "" && top > 0) {
		navSynced = true;
		if (sub)
		  return navSubActivate(top, sub);
		else
  		  return navTopActivate(top);
	}
}

function navOutSetTimer() {
	navOutClearTimer();
	//topOut = navTopAct;
	topOut = 0;
	navTimer = setTimeout('navTopShow('+ topOut +');', navOutDelay);
}

function navOutClearTimer() {	
	clearTimeout(navTimer);
}

function navOverSetTimer(t) {
	navOverClearTimer();
	navTimer = setTimeout('navTopShow('+t+');', navOverDelay);
}

function navOverClearTimer() {	
	clearTimeout(navOverTimer);
}

function navReset() {
	navSubActivate(0, 0);
}

function navTopShow(t) {
	if (navTopLast > 0 && t != navTopLast) showHideLayers('divNavTop'+navTopLast, '', 'hide');
	if (t != navTopLast && t > 0) showHideLayers('divNavTop' + t,'','show');
	if (navTopLast > 0 && navTopLast != t) navPicActive(false, navTopLast);
	if (t > 0 && t != navTopLast) navPicActive(true, t);
	if (navTopAct == navTopLast) navTopActivate(navTopAct);
	navTopLast = t;
}

// link to some page, but activate some level in the navigation
function navOther(t, s, link) {
	navOutClearTimer();
	navSubActivate(t, s);
	if (navFrameTarget) 
	  parent.frames[navFrameTarget].location = link;
	else 
	  document.location = link;
	if (navOtherFrame) 
	    parent.frames[navOtherFrame].navReset();
	return false;
    
}

function navLink(t, s, link) {
	var lvl = t + (s ? "_" + s : "");
	if (navLinks[lvl]) {
		var link = navLinks[lvl];
		if (link.substring(0,1) == '!') {
			var code = link.substring(1);
			eval(code);
		} else {
			if (navFrameTarget) 
				parent.frames[navFrameTarget].location = link;
			else
				document.location = link;
		}
	}
	else {
		var link = navHtmlPre + t + (s ? "_" + s : "") + navHtmlPost;
		if (navFrameTarget) 
			parent.frames[navFrameTarget].location = link;
		else
			document.location = link;
	}
	if (navOtherFrame) 
		parent.frames[navOtherFrame].navReset();
}

function navTopActivate(t) {
	if (t != navTopAct) {
		navPicActive(false, navTopAct);
		navTopAct = t;
		//navTopShow(t);
	}
	var imgCode = navTopName + t;
	var imgSrc = navTopPre + t + navTopXXX + navTopPost;
	var img = findObj(imgCode);
	if (img != null) img.src = imgSrc;
}

function navSubActivate(t, s) {
	if (t != navTopAct || s != navSubAct) {
		navPicActive(false, navTopAct, navSubAct);
		navPicActive(true, t, s);
		navTopActivate(t);
		navSubAct = s;
	}
}

function navPicActive(isActive, t, s) {
	if (t == 0) return;
	var imgCode = (s > 0 ? navSubName + t + "_" + s : navTopName + t);
	var imgSrc = navTopPre + t + (s > 0 ? "_" + s : "") + (isActive ? navTopActive : "") + navTopPost;
	var img = findObj(imgCode);
	if (img != null) img.src = imgSrc;
}

// navigation mouse events

function navTopOver(t) {
	navOutClearTimer();
	navOverSetTimer(t);
	return false;
}

function navTopOut(t) {
	navOverClearTimer();
	navOutSetTimer();
	return false;
}

function navTopClick(t) {
	// if there is no subnavigation, we evalutate the top-link diretly
	if (navNoSub) {
		//navOverClearTimer();
		// only activate on click in a frame-system. does not look nice, when same page is reloaded.
		if (navFrameTarget)
			navTopActivate(t);
		navLink(t);
		return false;
	} else {
		return navSubClick(t, 1);
	}
}

function navSubOver(t, s) {
	navOverClearTimer();
	navOutClearTimer();
	if (t == navTopAct && s == navSubAct) return false;
	navPicActive(true, t, s);
	navSubLast = s;
	return false;
}

function navSubOut(t, s) {
	navOutSetTimer();
	if (t == navTopAct && s == navSubAct) return false;
	navPicActive(false, t, s);
	return false;
}

function navSubClick(t, s) {
	navOverClearTimer();
	// only activate on click in a frame-system. does not look nice, when same page is reloaded.
	if (navFrameTarget)
		navSubActivate(t, s);
	navLink(t, s);
	return false;
}

// utility functions

function findObj(n, d) {
	var p,i,x;
  	if (!d) d=document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
  	if (!(x = d[n]) && d.all) x=d.all[n];
	for (i=0; !x && i < d.forms.length; i++) x=d.forms[i][n];
	for (i=0; !x && d.layers && i < d.layers.length; i++) x = findObj(n,d.layers[i].document);
	if (x==null && document.getElementById) x = document.getElementById(n);
	return x;
}


function showHideLayers() {
	var i, p, v, obj, args = showHideLayers.arguments;
	for (i = 0; i < (args.length - 2); i += 3) {
	    if ((obj = findObj(args[i])) != null) { 
    		v = args[i+2];	
		if (obj.style) {
		    obj = obj.style; 
		    v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden': v;
		}
		obj.visibility=v;
	    }
	}
}

function navPreload() {
	for (i = 1; i <= navTopNum; i++) {
	    imgPreload(navTopName, navTopPre, i, navTopPost, navTopActive);
	    for (j = 1; j <= navSubNum[i]; j++) {
		imgPreload(navSubName, navTopPre, i+"_"+j, navTopPost, navTopActive);
	    }
	}
}

function imgPreload(name, imgPre, imgNum, imgPost, imgAct) {
	var imgName = name + imgNum + imgAct;
	var img = new Image();
	img.src = imgPre + imgNum + imgAct + imgPost;
	imgCache[imgName] = img;
	if (imgAct) imgPreload(name, imgPre, imgNum, imgPost, "");
}

function winOpen(winName, path, feat) {
	if (feat == "full")
		feat = "toolbar=1,location=1,status=1,menubar=1,resizable=1,scrollbars=1";
	var win = window.open(winName, path, feat);
	win.focus();
}


function popup(path, width, height, feat, notcenter) {
	if (!width)
	    width = 300;
	if (!height)
	    height = 300;
	if (feat == "full")
		feat = "toolbar=1,location=1,status=1,menubar=1,resizable=1,scrollbars=1";
	else 
		feat = "width="+width+",height="+height+(feat ? ","+feat : "");
	if (!notcenter) 
		feat = "left="+(screen.width/2-width/2)+",top="+(screen.height/2-height/2) + ","+feat;
  	var win = window.open(path, width+"x"+height, feat);
	win.focus();
}
