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

navHtmlPre = document.location.pathname+"?";
navHtmlPost = ".html";
navTopPre = "gif/mal_nav_";
navTopPost = ".gif";
navTopActive = "a";
navOtherFrame = "";
navFrameTarget = "";
navTopName = "navTop";
navSubName = "navSub";

cssMode = false;

navTopNum = 9;
navSubNum = Array(0, 0, 0, 0, 0);

navLinks = new Object();


navLinks["1"] = "?";
//navLinks["2"] = "?_a=page&_key=tipps";
navLinks["2"] = "?_a=show&_nid=1150";
navLinks["3"] = "?_a=page&_key=mal";
navLinks["4"] = "?_a=page&_key=jugend";
navLinks["5"] = "?_a=page&_key=lehrer";
navLinks["6"] = "?_a=page&_key=auko";
//navLinks["7"] = "?_a=page&_key=hilfe";
navLinks["7"] = "?_a=show&_nid=1160";
navLinks["8"] = "?_a=search";
navLinks["9"] = "?_a=page&_key=feedback";

navOutDelay = 1;
navOverDelay = 1;

navTopLast = 0;
navSubLast = 0;
navTopAct = 0;
navSubAct = 0;
navTimer = 0;
navOverTimer = 0;
imgCache = new Object();
setTimeout('navPreload()', 1000);
setTimeout('navSync()', 150);
fastTimeout('navSync()', 'navSub4', 100);
navNoSub = true;

// main navigation functions

function fastTimeout(code, waitFor, time, count) {
	if (count > 10) {
		alert("Could not initiliase site. everything ok?");
	}
	var obj = findObj(waitFor);
	if (obj)
		eval(code);
	else
		setTimeout("fastTimeout('"+code+"', '"+waitFor+"', "+time+", "+(count+1)+");", time);
}

function navSync(t, s) {
	if (t && s)
		navSubActivate(top, sub);
	// content eq navigation frame:
	str = document.location.href;
	
	// 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);
	var top = str.substring(i+1, j);
	var sub = str.substring(j+1, k);
	if (top > 0 && sub > 0) 
		// activate level
		navSubActivate(top, sub);
		
	else {
		return;
		// not now, please..
		var linkCode;
		for (linkCode in navLinks) {
			link = navLinks[linkCode];
			page = link.substring(0, link.lastIndexOf('.'))
			if (str.indexOf('page')) {
				top = linkCode.substr(0, linkCode.indexOf('_'));
				sub = linkCode.substr(linkCode.indexOf('_')+1);
				alert("found from link: "+top+"_"+sub);
				navSubActivate(top, sub);
			}
		}
	}
}

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

function navOutClearTimer() {	
	//clearTimeout(navTimer);
}

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

function navOverClearTimer() {	
	clearTimeout(navOverTimer);
}

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

function navTopShow(t) {
	navSubShow(t);
	if (navTopLast > 0 && navTopLast != t && navTopLast != navTopAct) navPicActive(false, navTopLast);
	if (t > 0 && t != navTopLast) navPicActive(true, t);
	navTopLast = t;
}

function navSubShow(t) {
	var imgCode, imgSrc, img;
	if (cssMode) {
		if (navTopLast > 0 && t != navTopLast) showHideLayers('divNavTop'+navTopLast, '', 'hide');
		if (t != navTopLast && t > 0) showHideLayers('divNavTop' + t,'','show');
	} else {
		for (s=1; s <= navSubNum[t]; ++s) {
			imgCode = navSubName + s;
			imgSrc = navTopPre + t + "_" + s + (navSubAct == s && navTopAct == t ? navTopActive : "" ) + navTopPost;
			img = findObj(imgCode);
			if (img != null) img.src = imgSrc;
		}
	}
	navSubLast = s;
}

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


function navTopActivate(t) {
	if (t != navTopAct) {
		navPicActive(false, navTopAct);
		navTopAct = t;
		navTopShow(t);
	}
}

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

function navPicActive(isActive, t, s) {
	if (t == 0) return;
	//var imgCode = (s > 0 ? navSubName *** + t + "_" *** + s : navTopName + t);
	var imgCode = (s > 0 ? navSubName + 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) {
	var link, code;
	// if there is no subnavigation, we evalutate the top-link diretly
	if (navNoSub) {
		//navOverClearTimer();
		
		// don't activate top, when there are no subnavs and the click loads a new page
		// because of a not-so-nice IE-behaviour
		//navTopActivate(t);
		if (navLinks[t+""]) {
			link = navLinks[t+""];
			if (link.substring(0,1) == '!') {
				code = link.substring(1);
				eval(code);
				return false;
			}
		}
		else 
			link = navHtmlPre + t + navHtmlPost;
		
		doc = navFrameTarget ? parent.frames[navFrameTarget] : document;
		doc.location = link;
		
		if (navOtherFrame) 
			parent.frames[navOtherFrame].navReset();
		return false;
	} else {
		navTopActivate(t);
		return navSubClick(1);
	}
}

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

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

function navSubClick(s) {
	t = navTopLast;
	navOverClearTimer();
	navSubActivate(t, s);

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

	if (navOtherFrame) 
	    parent.frames[navOtherFrame].navReset();
	
	return false;
}


function navToolOver(t) {
	return false;
}

function navToolOut(t) {
	return false;
}

function navToolClick(t) {
	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();
}

/* Cookie Library */


function acceptsCookies() {
	if (document.cookie == '') {
	    document.cookie = 'acceptsCookies=yes'; 
	    if(document.cookie.indexOf('acceptsCookies=yes') != -1) {
	        return true; 
	    }
	} else {
	    return true;
	}
	return false;
}


function setCookie (name, value, hours, path, domain, secure) {
    if (acceptsCookies()) { 
	var not_NN2 = (navigator && navigator.appName && (navigator.appName == 'Netscape') && navigator.appVersion && (parseInt(navigator.appVersion) == 2))?false:true;
	if(hours && not_NN2) {
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) {
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { 
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} 


function readCookie(name) {
    if(document.cookie == '') {
	return false; 
    } else {
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
	    firstChar += name.length + 1;
	    lastChar = theBigCookie.indexOf(';', firstChar); 
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { 
	    return false;
	}
    }	
} 

function removeCookie(name, path, domain) {
  var val = readCookie(name);
  if (val) {
      document.cookie = name + '=' + val + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
  }
} 

function showPic(file, w, h) {
  w = window.open(file, 'pic', 'width='+w+',height='+h);    
  w.focus();
  w.document.writeln('<body bgcolor=white marginwidth=0 marginheight=0 leftmargin=0 topmargin=0><a href=javascript:window.close()><img src="'+file+'" width="'+width+'" height="'+height+'" border=0></a>');
  return false;
}


