/**********************************************************************************
*	Script für vertikales Dropdown-Menue
*
*	insert in head:
*		<script src="navi.js" type="text/javascript"></script>
*		<link rel="stylesheet" href="navi.css" type="text/css">
*
*	and in body instead of menu:
*		<script src="navi_defs_en.js" type="text/javascript"></script>
*
*	NOTE: wenn man im #divCont die position von absolute auf relative setzt, kann man das Menü in eine Tabelle setzen.
*
*   (c)2008 Version 3.0 Internet Service Laidler, Friedrichshafen
*   changes: rolladen-effekt
*********************************************************************************/

// Submenue-Parameter lesen
query = window.location.search;

param = new Array;
value = new Array;
menuItem = 1;	// default (home)
if (query != ''){
  	query = query.substr(1, query.length - 1);	// ? entfernen
  	query = query.replace(/%26/,'&');			// decode all &
  	params = query.split('&');					// parameter splitten
	for (i=0; i<params.length; i++){
		pair = params[i].split('=');
		param[i] = pair[0];
		value[i] = pair[1];
	}	
	for (i=0; i<param.length; i++){				// alle Parameter checken
		if (param[i]=="sub") menuItem = value[i];	// gefunden
	}
}
	
oldTop = 0;		// nichts geoeffnet

//===============================================================

// stayFolded = false;			//Öffnen/Schließen Top-Menü true = alle Top öffen lassen,  false = Top wieder schließen?
withImg = 1;			//Grafik ja/nein (später, bei 0 müssen im body alle img für das Menü entfernt werden)?

//================================================================
//object constructor...
function makeMenu(obj){
	
	// element (mit div-id holen) 
	this.el= document.getElementById(obj);

	// style	
   	this.css = this.el.style;

	this.ref = document;
	
	// position		
	this.x = this.el.offsetLeft;	// Abstand zum übergeordneten Element
	this.y = this.el.offsetTop;
	// hoehe
	this.h = this.el.offsetHeight;	// tatsächliche Höhe des Elements
	
	// methoden
    this.vis= b_vis;
	this.hideIt= b_hideIt;
    this.showIt= b_showIt;
    this.moveIt= b_moveIt;
	return this;
}
//object methods...
function b_showIt(){
	this.css.display='block';
}
function b_hideIt(){
	this.css.display='none';
}
function b_vis(){
	if(this.css.display=='none' || this.css.display=='NONE') return false;
	return true;
}
function b_moveIt(x,y){
	this.x=x; 
	this.y=y; 
	// neue Werte als Eigenschaft ablegen
	this.css.left=this.x; 
	this.css.top=this.y;
}

// fuer den Rolladeneffekt
// setTimeout fuehrt die Funktion im "global context" aus!
// mehrfache Verzögerung funktioniert nur bei rekursivem Aufruf von setTimeout!

var iForTOs;
var numForTOs; 
function to_showIt() { 
	oSubSub[numForTOs][iForTOs].showIt();
	for(j=numForTOs+1; j<oTop.length; j++){
		oTop[j].moveIt(0, oTop[j-1].y);
	}
	if (iForTOs++ < oSubSub[numForTOs].length-1) setTimeout("to_showIt()", rollTime); 
} 

var iForTOh; 
var oldTopForTOh;
function to_hideIt() { 
	oSubSub[oldTopForTOh-1][iForTOh].hideIt();
	for(j=oldTopForTOh; j<oTop.length; j++){
		oTop[j].moveIt(0, oTop[j-1].y);
	}
	if (iForTOh-- > 0) setTimeout("to_hideIt()", rollTime); 
} 

/************************************************************************************
This is the function that changes the sub menus to folded or unfolded state.
	oldTop: geöffnetes Topmenue
************************************************************************************/
function menu(num){

	if (roll) {
		// mit Ablauf Rolladen ========================================
		
		if (oldTop > 0) {	// nur wenn geöffnet
			//for alle subsubs (i=last bis i=1)
			iForTOh = oSubSub[oldTop-1].length - 1;
			oldTopForTOh = oldTop; 
			setTimeout("to_hideIt()", rollTime);
			//closeImage(oldtop)
			if (withImg) {
				if (sub_text[oldTop-1].length > 0) {
					oTop[oldTop-1].ref["imgA"+(oldTop-1)].src = closedImg.src; 
				} else {
					oTop[oldTop-1].ref["imgA"+(oldTop-1)].src = emptyImg.src; 
				}
			}
		}	
		//fertig, wenn nur zumachen
		if (num == oldTop-1) {
			oldTop = 0;
			return;	// nur zuklappen
		}
		
		//openImage(num)
		if (withImg) {
			if (sub_text[num].length > 0) {
				oTop[num].ref["imgA"+num].src = openedImg.src;
			} else {
				oTop[num].ref["imgA"+num].src = emptyImg.src; 
			}
		}

		// nur aufklappen, wenn subs da
		if (sub_text[num].length > 0) {
			//for alle subsubs (i=1 bis last)
			iForTOs = 0;
			numForTOs = num; 
			setTimeout("to_showIt()", rollTime);
		}
				
	} else {
		// Ablauf normal ==============================================
		if (oldTop > 0) {
			// hide sub(oldtop)
			oSub[oldTop-1].hideIt();
			// move top(oldtop+1) bis top(lasttop) nach oben
			for(var i=oldTop; i<oTop.length; i++){
				oTop[i].moveIt(0, oTop[i-1].y);
			}
			// closeImage(oldtop)
			if (withImg) {
				if (sub_text[oldTop-1].length > 0) {
					oTop[oldTop-1].ref["imgA"+(oldTop-1)].src = closedImg.src; 
				} else {
					oTop[oldTop-1].ref["imgA"+(oldTop-1)].src = emptyImg.src; 
				}
			}
		}
		if (num == oldTop-1) {
			oldTop = 0;
			return;	// nur zuklappen
		}
		
		// openImage(num)
		if (withImg) {
			if (sub_text[num].length > 0) {
				oTop[num].ref["imgA"+num].src = openedImg.src;
			} else {
				oTop[num].ref["imgA"+num].src = emptyImg.src; 
			}
		}
	
		// show sub von top(num)
		if (sub_text[num].length > 0) {
			oSub[num].showIt();
			// move top(num+1) bis top(lasttop) nach unten
			for(i=num+1; i<oTop.length; i++){
				oTop[i].moveIt(0, oTop[i-1].y); // + offsetYTop);
			}
		}	
	}
	oldTop = num+1;
}

/*********************************************************************
The init function... 
*********************************************************************/
function initNavi(){

	oTop = new Array();
	oSub = new Array();
	
	oSubSub = new Array;		// zweidimensional
	for (i=0; i<top_text.length; i++)
		oSubSub[i] = new Array;

	//Making the objects and hiding the subs...
	for (i=0; i<top_text.length; i++){
		oTop[i] = new makeMenu('divTop'+i);
		oSub[i] = new makeMenu('divSub'+i);
		for (j=0; j<sub_text[i].length; j++){
			oSubSub[i][j] = new makeMenu('divSub'+i+'Sub'+(j+1));
			if (roll)
				oSubSub[i][j].hideIt();	// alle Submenues verstecken
		}
		if (sub_text[i].length > 0) oSubSub[i][0].css.borderTopWidth = "0";		// das erste sub ohne Strich oben
		if (roll){
			if (sub_text[i].length == 0)
				oSub[i].hideIt();	// alle Subs ohne subsubs verstecken
		}
		else
				oSub[i].hideIt();	// alle Submenues verstecken
	}
	
	//Positioning the top objects...
	oTop[0].moveIt(0,0);
	for (var i=1; i<oTop.length; i++){
		oTop[i].moveIt(0, oTop[i-1].y); // + offsetYTop);
	}

	// event. submenu oeffnen	
	if (sub_text[menuItem-1].length > 0)
		menu(menuItem-1);
		
}

// prueft, ob target-argument eingefügt werden muss
function checkTarget(topx, subx){
	targ = '';
	for (var m=0; m<targets.length; m++){
		if (targets[m][0] == topx){
			if (targets[m][1] == subx){
				targ = targets[m][2];
				break;
			}	
		}	
	}
	return targ;
}

// menu als html ausgeben
function writeMenu(){
	document.writeln('<div id="divCont">');
	
	// fuer alle Top-Menues
	for (i=0; i<top_text.length; i++){
		document.writeln('<div id="divTop' + i + '" class="clTop">');
		if (top_href[i] == '#'){
			document.writeln('<a href="#" onclick="menu(' + i + '); return false" onfocus="this.blur()">');
		} else {
			t = checkTarget(i,'');
			if (t==''){
				document.writeln('<a href="' + top_href[i] + '" onclick="menu(' + i + '); return true" onfocus="this.blur()">');
			} else {
	document.writeln('<a href="' + top_href[i] + '" onclick="menu(' + i + '); return true" onfocus="this.blur()" target="' + t + '">');
			}
		}	
		if (sub_text[i][0]){
			// submenue existiert
	document.writeln('&nbsp;<img src="' + img_Path + 'arrow_r.gif" name="imgA' + i + '" width="12" height="12" alt="" border="0">');
			document.writeln(' ' + top_text[i] + '<\/a>');
			document.writeln('<div id="divSub' + i + '" class="clSub">');
			// fuer alle Sub-Menues
			for (j=0; j<sub_text[i].length; j++){
				document.writeln('<div id="divSub' + i + 'Sub' + (j+1) + '" class="clSubSub">');
				if (sub_href[i][j] != '#'){
					t = checkTarget(i, j);
					if (t==''){
						document.writeln('<a href="' + sub_href[i][j] + '?sub=' + (i+1) + '">' + sub_text[i][j] + '<\/a><\/div>');
					} else {
	document.writeln('<a href="' + sub_href[i][j] + '?sub=' + (i+1) + '" target="' + t + '">' + sub_text[i][j] + '<\/a><\/div>');
					}
				}else{
					document.writeln('<a href="' + sub_href[i][j] + '">' + sub_text[i][j] + '<\/a><\/div>');
				}	
			}
			document.writeln('<\/div><\/div>');
		} else {
			// kein submenue
			document.writeln('&nbsp;<img src="' + img_Path + 'leer.gif" name="imgA' + i + '" width="12" height="12" alt="" border="0">');
			document.writeln(' ' + top_text[i] + '<\/a>');
			document.writeln('<div id="divSub' + i + '" class="clSub"><\/div><\/div>');
		}	
	}			

	document.writeln('<\/div>');
}
