function menu_dynamic(menu) {
	this.menu = menu;

	// save some tree data
	this.menu_elements = new Array();
	this.parse_tree = parse_tree;

	this.menu_expand = menu_expand;
	this.menu_reset = menu_reset;
	this.test = test;

	this.onmouseover = md_onmouseover;
	this.onmouseout = md_onmouseout;
	this.onmousedown = md_onmousedown;
	this.onclick = md_onclick;

	this.show_submenu = show_submenu;
}

function menu_reset (id) {

	for (var i = 0; i < this.menu.items.length; i++) {
		if ( !this.menu_elements[id][0] ) { // reset only if level 0 is klicked;
			this.menu.items[i].element.style.top = this.menu_elements[i][3].toString() + 'px'
			this.menu.items[i].visibility(false);
		}
		this.menu.items[i].switch_style('default');
	}
}

function parse_tree () {
	var curr_element, cssclass, depth;
	var childno = 0;
	for (var i = 0; i < this.menu.items.length; i++) {
		this.menu_elements[i] = new Array(4);
		curr_element = document.getElementById('mi_0_' + i);
		cssclass = curr_element.getAttributeNode("class");
		depth = parseInt(cssclass.value.substring(3,4));
		this.menu_elements[i][0] = depth;  								// depth = level
		this.menu_elements[i][1] = (depth ? 0 : ++childno);				// top level entry no
		this.menu_elements[i][2] = this.menu.children[childno-1].fields.length - 2;	// second level entries
		this.menu_elements[i][3] = parseInt(curr_element.style.top);	// absolute postition
	}
}

function test () {
	alert("test output");
	for (i = 0; i < this.menu.items.length; i++) {
		for (j = 0; j < 4; j++)
			document.writeln(this.menu_elements[i][j]) ;
	document.writeln("<br>");
	}
}

function menu_expand(id) {

	this.id = id;
	var count_children
	var height = this.menu.pos['top'][1];   // height of submenu elements
	count_children = this.menu_elements[this.id][2];
	if ( count_children == 0 ) return; 	// no submenus
	if (this.menu_elements[this.id][0]) return; 		// submenu, dont expand

	this.show_submenu(this.id + 1, count_children);

	for (i = this.id + 1; i < this.menu.items.length; i++) {
		if (this.menu_elements[i][0]) continue; 		// submenu, skip
		document.getElementById('mi_0_' + i).style.top =
		(this.menu_elements[i][3] + height * count_children).toString() + 'px';
	}
}

function show_submenu (id, children) {
	this.id = id;
	this.count_children = children;
	for (var i = 0; i < this.count_children; i++)
		this.menu.items[i + this.id].visibility(true);
}

function md_onmousedown (id) {
	curr_item = this.items[id];
	curr_item.switch_style('onmousedown');
}
function md_onmouseover (id) {
	curr_item = this.items[id];
	curr_item.switch_style('onmouseover');
}
function md_onmouseout (id) {
	curr_item = this.items[id];
	curr_item.switch_style('onmouseout');
}
function md_onclick (id) {
	var item = this.items[id];

	this.md.menu_reset(id);
	// move succeeding elements
	this.md.menu_expand(id);
	// color current item
	curr_item = this.items[id];
	curr_item.switch_style('onmousedown');
	
	return (item.fields[1] ? true : false);
}
