window.onload = init;

function init() {
	var sec_current, nav_current, classes, c_ = 0, j = 0;
	var opend = new Array();
	if ( sec_current = document.getElementsByTagName( 'body' )[ 0 ].id.replace( 'p-', '' ) ) {
		c_++;
	}
	if ( classes = document.getElementsByTagName( 'body' )[ 0 ].className ) {
		classnames = classes.split( ' ' );
		for ( var i = 0; i < classnames.length; i++ ) {
			if ( classnames[ i ].substring( 0, 1 ) == 's' ) {
				nav_current = classnames[ i ];
			} else if ( classnames[ i ].substring( 0, 2 ) == 'o_' ) {
				opend[ j ] = classnames[ i ].replace( 'o_', 's-' );
				j++;
			}
		}
	}
	if ( c_ > 0 ) {
		naviInit( sec_current, nav_current, opend );
	}
}

function naviInit( sec_current, nav_current, opend ) {

	if ( nav_current ) {
		var cur_nav_item = document.getElementById( nav_current );
		cur_nav_item.className = 'nicurrent';
	}

	var uls = document.getElementsByTagName( 'ul' );
	var linkuls = new Array();
	var k = 0;
	for ( var i = 0; i < uls.length; i++ ) {
		if ( uls[ i ].className.indexOf( 'links' ) != -1 ) {
			linkuls[ k ] = uls[ i ];
			k++;
		}
	}

	var lis = document.getElementsByTagName( 'li' );
	for ( var i = 0; i < lis.length; i++ ) {
		if ( lis[ i ].className.indexOf( 'sectionlink' ) != -1 ) {
			var submenue = findSubmenue( lis[ i ] );
			if ( submenue ) {
				if ( lis[ i ].id != sec_current && in_array( lis[ i ].id, opend ) == false ) {
					lis[ i ].childNodes[ submenue ].style.display = 'none';
				}
				var m_item_a = findMainItemAnchor( lis[ i ] );
				lis[ i ].childNodes[ m_item_a ].onclick = function() {
					var sb = findSubmenue( this.parentNode );
					this.parentNode.childNodes[ sb ].style.display = 'block';
			//		closeOthers( this.parentNode.id, linkuls );  // hier entkommentieren, wenn auf- und zuklappen soll
				}
			}
		}
	}

}

function findSubmenue( li ) {
	for ( var i = 0; i < li.childNodes.length; i++ ) {
		var el = li.childNodes[ i ];
		if ( el.nodeType == 1 ) {
			if ( el.className.indexOf( 'links' ) != -1 ) {
				return i;
			}
		}
	}
	return false;
}

function findMainItemAnchor( li ) {
	for ( var i = 0; i < li.childNodes.length; i++ ) {
		var el = li.childNodes[ i ];
		if ( el.nodeType == 1 ) {
			if ( el.className.indexOf( 'cat' ) != -1 ) {
				return i;
			}
		}
	}
}

function closeOthers( current, others ) {
	for ( var i = 0; i < others.length; i++ ) {
		if ( others[ i ].id != 's-' + current ) {
			others[ i ].style.display = 'none';
		}
	}
}

function in_array( elid, opend ) {
	for ( var i = 0; i < opend.length; i++ ) {
		if ( opend[ i ] == 's-' + elid ) {
			return true;
		}
	}
	return false;
}
