// custom.js






// Aus "menu_father" wird fuer alle divs "menu_children" initialisiert
function menu_init_div_hierarchy(  ) {
	var div	= obj('html_menu').getElementsByTagName('div');

	// Alle divs finden, die zum Menu gehoeren, und aus eindeutigem Vater-Zeigern mehrdeutige Kind-Zeiger bauen.
	for( var i=0 ; i<div.length ; i++ ) {
		if( div[i].className.match(/\bmenu_div\b/ ) ) {
			var father	= div[i].getAttribute("menu_father");
			if( father!="" ) {
				var tmp;
				tmp	= obj(father).getAttribute("menu_children") + "," + div[i].id;
				tmp	= tmp.replace(/^,/ , "" );
				obj(father).setAttribute("menu_children" , tmp );
			}
		}
	}
}


// Initialisiere Attribute fuer a tags
function menu_init_a( menu_div_all ) {
	var child;
	var tmp;
	
	// Erster Durchlauf: Pointer erstmal setzen
	for( var i=0 ; i<menu_div_all.length ; i++ ) {
		a_list	= menu_div_all[i].getElementsByTagName('a');
		for( var j=0 ; j<a_list.length ; j++ ) {
			var a	= a_list[j];
			if( a.className.match(/\bmenu_a\b/ ) ) {
			
				// Tiefe vom div uebernehmen
				a.setAttribute( 'menu_depth' , menu_div_all[i].getAttribute( 'menu_depth') );
				
				// father berechnen
				child	= a.getAttribute('menu_firstchild_a');
				if( child ) {
					var children	= menu_neighbours_a( obj(child) );
					for( k=0 ; k<children.length ; k++ ) {
						children[k].setAttribute( 'menu_father' , a.id );
					}
				}
			}
		}
	}
	// Zweiter Durchlauf mit bekannten Pointern
	for( var i=0 ; i<menu_div_all.length ; i++ ) {
		a_list	= menu_div_all[i].getElementsByTagName('a');
		for( var j=0 ; j<a_list.length ; j++ ) {
			var a	= a_list[j];
			if( a.className.match(/\bmenu_a\b/ ) ) {
			
				// js-Link statt a href
				a.setAttribute( "menu_a_href" ,  a.href );
				a.href='javascript:menu_a_clicked("'+ a.id +'")' ;
				
				// Sektionsfarbe rekursiv erben
				tmp	= a;
				var color	= '';
				while( ! color && tmp ) {
					color	= tmp.getAttribute( "menu_color" );
					tmp		= tmp.getAttribute( 'menu_father');
					if( tmp ) {
						tmp		= obj( tmp );
					}
				}
				
				if( ! color )	color	= "schwarz";

				a.setAttribute( 'menu_color' , color );
				a.className	+= ' hover_' + color;


			}
		}
	}

}


// Nachbarn eines a
function menu_neighbours_a( a ) {
	var div	= obj( a.getAttribute('menu_divgroup') );
	return div.getElementsByTagName('a');
}

// workaround fuer IE6, alle anderen schreiben a=b statt a=same(b)...
function same(o) {
	return obj(o.id);
}

function menu_a_clicked( me ) {
	if( obj(me).getAttribute( "menu_virtual")=='0' ) {
		document.location.href= obj(me).getAttribute( "menu_a_href");
	} else {
		menu_de_highlight(menu_selected_a);
		menu_selected_a		= obj(me);
		menu_selected_div	= menu_selected_a.getAttribute("menu_divgroup");
		menu_highlight(menu_selected_a);
		menu_redraw();
	}
}

// Nach Initialisierung: Direkte Kinder eines divs
function menu_children_of( o ) {
	var ret	= new Array();
	
	var tmp;
	tmp	= o.getAttribute("menu_children");
	if( tmp ) {
		tmp	= tmp.split(",");
		for( var i=0 ; i<tmp.length ; i++ ) {
			ret[i]	= obj( tmp[i] );
		}
		return ret;
	}
	return "";
}

// Nach Initialisierung: Rekursive Kinder eines divs
function menu_children_of__recursive( o ) {
	var ret	= new Array();
	var child	= menu_children_of( o );
	if( child ) {
		for( var i=0 ; i<child.length ; i++ ) {
			ret[ret.length]	= child[i];
			var tmp	= menu_children_of__recursive( child[i] );
			ret				= ret.concat( tmp );
		}
	}
	return ret;
}

// (un-)sichtbarkeit erst mal nur vorbereiten, damit nichts flackert
function menu_prepare_visibility_div(menu_div_all , menu_selected_div ) {
	// alle divs verstecken
	for( var i=0 ; i<menu_div_all.length ; i++ ) {
		menu_div_all[i].setAttribute( "menu_div_display" , 0 );
	}

	// selektierten div bis runter zur basis sichtbar machen
	if( menu_selected_div ) {
		var tmp	= menu_selected_div;
		while( tmp ) {
			obj(tmp).setAttribute( "menu_div_display" , 1 );
			tmp	= obj(tmp).getAttribute('menu_father');
		}
		
		// Untermenue-div des aktuellen Menuepunktes sichtbar machen
		var firstchild	= menu_selected_a.getAttribute('menu_firstchild_a');
		if( firstchild ) {
			firstchild	= obj(firstchild).getAttribute("menu_divgroup");
			obj(firstchild).setAttribute( "menu_div_display" , 1 );
		}
	}

	// Hauptmenue ist immer sichtbar
	obj('menu_div_1').setAttribute( "menu_div_display" , 1 );

}

// vorbereitete (un-)sichtbarkeit wirklich anwenden
function menu_apply_visibility_div(menu_div_all ) {
	var c;
	var s;
	for( var i=0 ; i<menu_div_all.length ; i++ ) {
		c	= menu_div_all[i].className;
		if( c.search(/\bmenu_VISIBILITY/) < 0 )		c+=" menu_VISIBILITY_egal ";
		c	= c.replace(/\bmenu_VISIBILITY_\w*/ , " menu_VISIBILITY_"+menu_div_all[i].getAttribute( "menu_div_display" )+"_depth_"+menu_div_all[i].getAttribute( "menu_depth" ) );
		menu_div_all[i].className	= c;
		
		// trennstrich oben mit (un-)sichtbar machen
		s	= menu_div_all[i].id.replace(/^menu_div/ , "menu_lead" );
		s	= obj(s);
		if(s) {
			s.style.display	= menu_div_all[i].getAttribute( "menu_div_display" )==1		?		"block" : "none" ;
		}
	}
}


function menu_find_selected_a() {
	var a	= html_menu.getElementsByTagName("a");
	var found	= 0;
	for( var i=0 ; i<a.length ; i++ ) {
		if( a[i].getAttribute("menu_selected") ) {
			found	= a[i];
		}
	}
	return found;
}

function menu_de_highlight( a ) {
	var tmp	= a;
	while ( tmp ) {
		c	= tmp.className;
		c	= c.replace(/\bmenu_HIGHLIGHT.*?\b/g , '' );
		tmp.className	= c;
		if( tmp.getAttribute( "menu_depth" ) == 0 ) {
			tmp.style.backgroundImage	= "";
		}

		tmp		= tmp.getAttribute( 'menu_father');
		if( tmp ) {
			tmp		= obj( tmp );
		}
	}


}

function menu_highlight( a ) {
	var tmp	= a;
	while ( tmp ) {
		c	= tmp.className;
		c	= c.replace(/\bmenu_HIGHLIGHT.*?\b/g , '' );
		c	+= " menu_HIGHLIGHT"		+		" menu_HIGHLIGHT_depth_"+tmp.getAttribute( "menu_depth" ) + 		" menu_HIGHLIGHT_color_"+tmp.getAttribute( "menu_color" ) ;
		tmp.className	= c;
		if( tmp.getAttribute( "menu_depth" ) == 0 ) {
			tmp.style.backgroundImage	= "url(customize/image/_menu/" + tmp.getAttribute( "menu_color" ) + ".gif)";
		}

		tmp		= tmp.getAttribute( 'menu_father');
		if( tmp ) {
			tmp		= obj( tmp );
		}
	}
}

function menu_redraw() {
	menu_prepare_visibility_div(menu_div_all , menu_selected_div , menu_selected_a );
	menu_apply_visibility_div(menu_div_all );
}


