var menus = [];

function Menu(groupMenuItemsArray,groupMenuPositionArray,groupMenuStylesArray)
{
	this.groupMenuItemsArray=groupMenuItemsArray;
	this.groupMenuPositionArray=groupMenuPositionArray;
	this.groupMenuStylesArray=groupMenuStylesArray;
	this.id=menus.length;
	this.items=[];
	this.children=[];
	this.add_item=Menu_Add_item;
	this.hide=Menu_Hide;
	this.onclick=Menu_onclick;
	this.onmouseout=Menu_onmouseout;
	this.onmouseover=Menu_onmouseover;
	this.onmousedown=Menu_onmousedown;

	var i;
	for (i=0;i<this.groupMenuItemsArray.length;i++)
	{
		new Menu_item(i,this,this);
	}
	for (i=0;i<this.children.length;i++)
	{
		this.children[i].visibility(true);
	}
	menus[this.id]=this;
}

function Menu_Add_item(item)
{
	var id = this.items.length;
	this.items[id] = item;
	return (id);
}

function Menu_Hide()
{
	for (var i = 0; i < this.items.length; i++)
	{
		this.items[i].visibility(false);
		this.items[i].switch_style('onmouseout');
	}
}

function Menu_onclick(id)
{
	var item = this.items[id];
	location.href=item.fields[1];
//	return (item.fields[1] ? true : false);
}

function Menu_onmouseout(id)
{
	this.hide_timer = setTimeout('menus['+ this.id +'].hide();',this.groupMenuPositionArray['hide_delay'][this.active_item.depth]);
	if (this.active_item.id == id) this.active_item = null;
}

function Menu_onmouseover(id)
{
	this.active_item = this.items[id];
	clearTimeout(this.hide_timer);
	var curr_item, visib;
	for (var i = 0; i < this.items.length; i++)
	{
		curr_item = this.items[i];
		visib = (curr_item.arrpath.slice(0, curr_item.depth).join('_') ==	this.active_item.arrpath.slice(0, curr_item.depth).join('_'));
		if (visib) curr_item.switch_style (curr_item == this.active_item ? 'onmouseover' : 'onmouseout');
		curr_item.visibility(visib);
	}
}


function Menu_onmousedown(id)
{
	this.items[id].switch_style('onmousedown');
}

function Menu_item(path,parent,container)
{
	this.path = new String (path);
	this.parent = parent;
	this.container = container;
	this.arrpath = this.path.split('_');
	this.depth = this.arrpath.length - 1;
	var struct_path = '', i;
	for (i = 0; i <= this.depth; i++)
		struct_path += '[' + (Number(this.arrpath[i]) + (i ? 3 : 0)) + ']';
	eval('this.fields = this.container.groupMenuItemsArray' + struct_path);
	if (!this.fields) return;

	this.get_x = MenuItem_Get_X;
	this.get_y = MenuItem_Get_Y;
	this.init = MenuItem_Init;
	this.visibility = MenuItem_visibility;
	this.switch_style = MenuItem_switch_style;

	this.id = this.container.add_item(this);
	parent.children[parent.children.length] = this;

	this.init();
	this.children = [];
	var child_count = this.fields.length - 3;
	for (i = 0; i < child_count; i++)
	{
		new Menu_item (this.path + '_' + i, this, this.container);
	}
	this.switch_style('onmouseout');
}


function MenuItem_Init()
{

  var tmpStr="this.style.cursor='pointer';";

	document.write (
		  '<div id="mi_' + this.container.id + '_'
			+ this.id +'" class="TJSNavigation_' + this.container.id + 'l' + this.depth
			+'o" href="' + this.fields[1] + '"'
			//+ (this.fields[2] ? ' target="' + this.fields[2] + '"' : '')
			+ ' style="position: absolute; top: '
			+ this.get_y() + 'px; left: '	+ this.get_x() + 'px; width: '
			+ this.container.groupMenuPositionArray['width'][this.depth] + 'px; height: '
			+ this.container.groupMenuPositionArray['height'][this.depth] + 'px; visibility: hidden;'
			+' background: black; color: white; z-index: ' + this.depth + ';" '
			+ 'onclick="void menus[' + this.container.id + '].onclick('
			+ this.id + ');" onmouseout="menus[' + this.container.id + '].onmouseout('
			+ this.id + ');" onmouseover="'+tmpStr+' menus[' + this.container.id + '].onmouseover('
			+ this.id + ');" onmousedown="menus[' + this.container.id + '].onmousedown('
			+ this.id + ');">'
			+ this.fields[0] + "</div>\n"
		);


/*
	document.write (
		  '<a id="mi_' + this.container.id + '_'
			+ this.id +'" class="TJSNavigation_' + this.container.id + 'l' + this.depth
			+'o" href="' + this.fields[1] + '"'
			//+ (this.fields[2] ? ' target="' + this.fields[2] + '"' : '')
			+ ' style="position: absolute; top: '
			+ this.get_y() + 'px; left: '	+ this.get_x() + 'px; width: '
			+ this.container.groupMenuPositionArray['width'][this.depth] + 'px; height: '
			+ this.container.groupMenuPositionArray['height'][this.depth] + 'px; visibility: hidden;'
			+' background: black; color: white; z-index: ' + this.depth + ';" '
			+ 'onclick="return menus[' + this.container.id + '].onclick('
			+ this.id + ');" onmouseout="menus[' + this.container.id + '].onmouseout('
			+ this.id + ');" onmouseover="menus[' + this.container.id + '].onmouseover('
			+ this.id + ');" onmousedown="menus[' + this.container.id + '].onmousedown('
			+ this.id + ');"><div class="TJSNavigation_'  + this.container.id + 'l' + this.depth + 'i">'
			+ this.fields[0] + "</div></a>\n"
		);

*/
	this.element = document.getElementById('mi_' + this.container.id + '_' + this.id);
}


function MenuItem_visibility(make_visible)
{
	if (make_visible != null)
	{
		if (this.visible == make_visible) return;
		this.visible = make_visible;
		if (make_visible) this.element.style.visibility = 'visible';
		else if (this.depth) this.element.style.visibility = 'hidden';
	}
	return (this.visible);
}

function MenuItem_Get_X()
{
	var value = 0;
	for (var i = 0; i <= this.depth; i++)
	{
		value+=this.container.groupMenuPositionArray['block_left'][i]+this.arrpath[i]*this.container.groupMenuPositionArray['left'][i];
	}
	return (value);
}

function MenuItem_Get_Y()
{
	var value = 0;
	for (var i = 0; i <= this.depth; i++)
	{
		value+=this.container.groupMenuPositionArray['block_top'][i]+this.arrpath[i]*this.container.groupMenuPositionArray['top'][i];
	}
	return (value);
}


function MenuItem_switch_style(state)
{
	if (this.state == state) return;
	this.state = state;
	var style = this.container.groupMenuStylesArray[state];

	for (var i = 0; i < style.length; i += 2)
	{
		if (style[i] && style[i+1])
		{
			eval('this.element.style.' + style[i] + "='" + style[i+1][this.depth] + "';");
		}
	}
}



function Redraw_JSMenu()
{
  return;
  if (Menu)
  {
    var windowWidth=document.body.clientWidth;
    var newLeftMenuItemPos=windowWidth/2-menuWidth/2;
    var firstMenuItem=document.getElementById("mi_0_0");
    var oldFirstMenuItemLeftPos=parseInt(firstMenuItem.style.left);

    if (newLeftMenuItemPos<minLeftfirstMenuItemPos) newLeftMenuItemPos=minLeftfirstMenuItemPos;

    var positionFaktor=newLeftMenuItemPos-oldFirstMenuItemLeftPos;



    for (var i=0;i<jsGroupNaigationItemCnt;i++)
    {
      var menuItem=document.getElementById("mi_0_"+i);
      var oldMenuItemLeftPos=parseInt(menuItem.style.left);
      if (menuItem)
      {
      	var newLeft=oldMenuItemLeftPos+positionFaktor;
        menuItem.style.left=newLeft;
      }
    }
  }
}

