function createMenuStyle() {
	var menuStyle = '';
	menuStyle += '<style type="text/css">\n<!--\n'
	+ '.mainitemout { '
	+ 'background-position: 0px ' + (menuBorderWidth + mainItemSpacerHeight) + 'px; '
	+ 'background-repeat: no-repeat; '
	+ 'border: ' + menuBorderColor + ' solid ' + menuBorderWidth + 'px; '
	+ 'color: ' + mainItemTxtColor + '; '
	+ 'font-family: '+ itemFontFamily +'; '
	+ 'font-size: ' + subItemFontSize + 'px; '
	+ 'font-weight: ' + subItemFontWeight + '; '
	+ 'text-align: ' + mainItemTxtAlign + '; '
	+ 'height: ' + mainItemHeight + '; '
	+ 'width: ' + mainItemWidth + '; '
	+ 'cursor: hand; ';
	if (document.all) {
		menuStyle += 'width: ' + (mainItemWidth + 2 * menuBorderWidth) + 'px; '
		+ 'height: ' + (mainItemHeight + 2 * menuBorderWidth) + 'px; '
		+ 'clip: rect(0px ' + (mainItemWidth + 2 * menuBorderWidth) + 'px ' + (mainItemHeight + 2 * menuBorderWidth) + 'px 0px); ';
	} else {
		menuStyle += 'width: ' + mainItemWidth + 'px; '
		+ 'height: ' + (mainItemHeight) + 'px; '
		+ 'clip: rect(0px ' + (mainItemWidth + 2 * menuBorderWidth) + 'px ' + (mainItemHeight + 2 * menuBorderWidth) + 'px 0px); ';
	}
	menuStyle += '}\n';
	menuStyle += '.mainitemover { '
	+ 'background-position: 0px ' + (menuBorderWidth + mainItemSpacerHeight) + 'px; '
	+ 'background-repeat: no-repeat; '
	+ 'border: ' + menuBorderColor + ' solid ' + menuBorderWidth + 'px; '
	+ 'color: ' + mainItemTxtOverColor + '; '
	+ 'font-family: '+ itemFontFamily +'; '
	+ 'font-size: ' + subItemFontSize + 'px; '
	+ 'font-weight: ' + subItemFontWeight + '; '
	+ 'text-align: ' + mainItemTxtAlign + '; '
	+ 'height: ' + mainItemHeight + '; '
	+ 'width: ' + mainItemWidth + '; '
	+ 'cursor: hand; ';
	if (document.all) {
		menuStyle += 'width: ' + (mainItemWidth + 2 * menuBorderWidth) + 'px; '
		+ 'height: ' + (mainItemHeight + 2 * menuBorderWidth) + 'px; '
		+ 'clip: rect(0px ' + (mainItemWidth + 2 * menuBorderWidth) + 'px ' + (mainItemHeight + 2 * menuBorderWidth) + 'px 0px); ';
	} else {
		menuStyle += 'width: ' + mainItemWidth + 'px; '
		+ 'height: ' + (mainItemHeight) + 'px; ';
	}
	menuStyle += '}\n';

	menuStyle += '.subitemout { '
	+ 'background-position: ' + (subItemWidth - subImgWidth - subItemImgPadding) + 'px ' + Math.ceil((subItemHeight - 2 * menuBorderWidth - subImgHeight) / 2) + 'px; '
	+ 'background-repeat: no-repeat; '
	+ 'border: ' + menuBorderColor + ' solid ' + menuBorderWidth + 'px; '
	+ 'color: ' + subItemTxtOverColor + '; '
	+ 'font-family: '+ itemFontFamily +'; '
	+ 'font-size: ' + subItemFontSize + 'px; '
	+ 'font-weight: ' + subItemFontWeight + '; '
	+ 'padding-left: ' + subItemTxtPadding + 'px; '
	+ 'padding-top: ' + subItemTxtPaddingTop + 'px; '
	+ 'cursor: hand; '
	if (document.all) {
		menuStyle += 'width: ' + (subItemWidth) + 'px; '
		+ 'height: ' + (subItemHeight) + 'px; ';
	} else {
		menuStyle += 'width: ' + (subItemWidth - subItemTxtPadding) + 'px; '
		+ 'height: ' + (subItemHeight - subItemTxtPaddingTop) + 'px; ';
	}
	menuStyle += '}\n';
	menuStyle += '.subitemover { '
	+ 'background-position: ' + (subItemWidth - subImgWidth - subItemImgPadding) + 'px ' + Math.ceil((subItemHeight - 2 * menuBorderWidth - subImgHeight) / 2) + 'px; '
	+ 'background-repeat: no-repeat; '
	+ 'border: ' + menuBorderColor + ' solid ' + menuBorderWidth + 'px; '
	+ 'color: ' + subItemTxtOverColor + '; '
	+ 'font-family: '+ itemFontFamily +'; '
	+ 'font-size: ' + subItemFontSize + 'px; '
	+ 'font-weight: ' + subItemFontWeight + '; '
	+ 'padding-left: ' + subItemTxtPadding + 'px; '
	+ 'padding-top: ' + subItemTxtPaddingTop + 'px; '
	+ 'cursor: hand; '
	if (document.all) {
		menuStyle += 'width: ' + (subItemWidth) + 'px; '
		+ 'height: ' + (subItemHeight) + 'px; '
	} else {
		menuStyle += 'width: ' + (subItemWidth - subItemTxtPadding) + 'px; '
		+ 'height: ' + (subItemHeight - subItemTxtPaddingTop) + 'px; '
	}
	menuStyle += '}\n';
	menuStyle += '-->\n</style>\n';

	document.write(menuStyle);
}
createMenuStyle();

var menuTimer = null;
var currentItem = false;
var openMenu = false;

function initMenu() {
	createMenu('main');
}

function createMenu(name,parent) {
	var lvl;
	var arrName = name;
	var array = eval(arrName + 'Arr');
	var container = new layerObj(arrName + 'Cont');
	container.hasParent = (parent) ? true : false;
	container.activeItem = false;
	container.setZindex(700);
	if (container.hasParent) {
		container.parentItem = parent;
		container.parentItem.subMenu = container;
		container.hideParent = _hideParent;
		lvl = 'sub';
	} else {
		lvl = 'main';
		container.setStyle("padding-top", "15px");
	}
	
	var firstItem = false;
	var containerHeight = false;
	var arrSubName,prevItem,itemName;
	var item,overItem,dummyItem;
	var itemWidth,itemHeight;
	if (maxMenuItems < array.length - 1) {
		var splitMenu = true;
		var splitPoint = Math.floor(array.length / 2);
	} else {
		var splitMenu = false;
	}
	for (var i = 1; i < array.length; i++) {
		//window.status = arrName + i;
		arrSubName = arrName + i + 'sub';
		prevItem = (i > 1) ? item : false;
		itemName = arrName + i + 'Item';
		itemWidth = eval(lvl + 'ItemWidth');
		itemHeight = eval(lvl + 'ItemHeight');
		
		item = (container.hasParent) ? new layerObj(itemName,container) : new layerObj(itemName);
		item.hasSub = eval('window.' + arrSubName + 'Arr') ? true : false;
		item.container = container;
		item.clearItem = _clearItem;
		item.setup = _itemSetup;
		if (splitMenu && i == splitPoint + 1) {
			item.setup(array[i],prevItem,firstItem);
		} else {
			item.setup(array[i],prevItem);
		}
		if (i == 1) {
			firstItem = item;
		} else if (splitMenu && i == splitPoint - 1) {
			containerHeight = item.getTop() + item.getHeight() - menuBorderWidth;
		}
		
		if (item.hasSub) {
			createMenu(arrSubName,item);
		}
	}
	
	if (!container.hasParent) {
		container.moveTo(menuLeft - menuBorderWidth,menuTop - mainItemSpacerHeight - 2 * menuBorderWidth);
		container.show();
		//window.status = '';
	} else {
		container.hide();
		if(document.all) {
			container.setWidth(item.getLeft() + item.getWidth() + menuBorderWidth*2);
			container.setHeight(((!containerHeight) ? item.getTop() : containerHeight) + item.getHeight() + menuBorderWidth*2);
		} else {
			container.setWidth(item.getLeft() + item.getWidth());
			container.setHeight(((!containerHeight) ? item.getTop() : containerHeight) + item.getHeight());
		}
	}
	window.status = "";
}

function _itemSetup(array,prevItem,firstItem) {
	this.inherit();
	if (this.container.hasParent) {
		//this.overClass = "subitemover";
		this.outClass = "subitemout";
		this.setClassName(this.outClass);
		this.container.setStyle("border", "1px solid #333333");
		var topElement = getTopParent(this.id);
		this.setupSub = _setupSub;
		
		this.setupSub(array, topElement);
		if (prevItem) {
			if (firstItem) {
				this.moveTo(firstItem.getLeft() + subItemWidth + menuBorderWidth,firstItem.getTop());
			} else {
				//this.moveTo(prevItem.getLeft(),prevItem.getTop() + subItemHeight);
				this.moveTo(prevItem.getLeft(),prevItem.getTop() + prevItem.getHeight());
			}
		}
	} else {
		this.overClass = 'mainitemover';
		this.outClass = 'mainitemout';
		this.setupMain = _setupMain;
		// Overklasse setzen, wenn aktiv (siehe index.php3)
		if(array[2] != array[3])
		{
			this.setClassName(this.outClass);
		}
		else
		{
			this.outClass = this.overClass;
			this.setClassName(this.outClass);
		}

		this.setupMain(array);

		if (prevItem) {
			this.moveTo(prevItem.getLeft() + mainItemWidth + menuBorderWidth,prevItem.getTop());
		} else {
			this.moveTo(menuLeft - menuBorderWidth,menuTop - mainItemSpacerHeight - menuBorderWidth);
		}
	}
	
	this.layer.parentObj = this;
	this.layer.onmouseover = _itemOver;
	this.layer.onmouseout = _itemOut;
	this.link = (array[1]) ? array[1] : false;
	if (this.link) this.layer.onclick = _itemClick;
}

function _setupSub(array, topElement) {
	window.status = array[0]; // + " wird geschrieben...";
	if (this.hasSub) {
		this.outImg = subImgOut;
		this.overImg = subImgOver;
		this.style.backgroundImage = 'url(' + subImgOut.src + ')';
	}
	topElement = topElement.parentObj;
	//var topElement = getTopParent(this.id);
	this.style.backgroundColor  = topElement.getSubMouseOutColor();
	this.setMouseOverColor(topElement.getSubMouseOverColor());
	this.setMouseOutColor(topElement.getSubMouseOutColor());
	this.write(array[0]);
}

function _setupMain(array) {
	this.outImg = mainImgKat0;
	this.overImg = mainImgKat0;
	this.style.backgroundColor  = array[2]; // BGMAINCOLOR
	this.setMouseOverColor(array[3]); // BGCOLOR
	this.setMouseOutColor(array[2]); 
	this.setSubMouseOverColor(array[4]);
	this.setSubMouseOutColor(array[3]);
	//if(array[2] == array[3]) this.setClassName('mainitemover');
	this.write("<nobr>"+array[0]+"</nobr>");
}

function getTopParent(id)
{
	var topElement = document.getElementById("main"+id.substr(4,1)+"Item");
	return topElement;
}

function _itemOver() {
	currentItem = this.parentObj;

	if (currentItem.container.activeItem) {
		currentItem.container.activeItem.clearItem();
		if (currentItem.container.activeItem.hasSub) {
			currentItem.container.activeItem.subMenu.hide();
		}
	}
	clearTimeout(menuTimer);
	currentItem.container.activeItem = this.parentObj;
	
	if (openMenu && openMenu != currentItem.container) {
		hideAll();
		openMenu = false;
	}
	
	if (currentItem.container.hasParent) {
		//currentItem.setClassName(currentItem.overClass);
		//currentItem.setClassName('subitemover');
		currentItem.style.backgroundColor = currentItem.getMouseOverColor();
		if (currentItem.link) {
			currentItem.style.cursor = (document.all) ? 'hand' : 'pointer';
		}
		if (currentItem.hasSub && currentItem.style.backgroundImage != 'url(' + currentItem.overImg.src + ')') currentItem.style.backgroundImage = 'url(' + currentItem.overImg.src + ')';
	} else {
		currentItem.setClassName(currentItem.overClass);
		currentItem.style.backgroundColor = currentItem.getMouseOverColor();
	}
	
	if (currentItem.hasSub) {
		openMenu = currentItem.subMenu;

		if (currentItem.container.hasParent) {
			var openMenuTop = ((currentItem.getPageTop() + openMenu.getHeight()) > (currentItem.container.getPageTop() + currentItem.container.getHeight())) ? currentItem.getPageTop() : currentItem.getPageTop();
			openMenu.moveTo(currentItem.getPageLeft() + currentItem.getWidth() + menuBorderWidth,openMenuTop);
		} else {
			openMenu.moveTo(currentItem.getPageLeft(),currentItem.getPageTop() + currentItem.getHeight() + menuBorderWidth - 2);
		}
		openMenu.show();
	}
}

function _itemOut() {
	currentItem = false;
	if (!openMenu || openMenu == this.parentObj.container) this.parentObj.clearItem();
	currentItem = this.parentObj;
	if (currentItem.container.hasParent) {
		currentItem.style.backgroundColor = currentItem.getMouseOutColor();
	}
	if (openMenu) menuTimer = setTimeout('hideAll()',menuKeepAlive);
}

function _itemClick() {
	window.location.href = "index.php3?lang="+lang+"&load=" + this.parentObj.link;
	return false;
}

function hideAll() {
	openMenu.parentItem.clearItem();
	openMenu.hide();
	openMenu.hideParent();
}

function _hideParent() {
	this.parentItem.clearItem();
	if (this.hasParent && this.parentItem.container.hasParent && this.parentItem.container != currentItem.container) {
		this.parentItem.container.hide();
		this.parentItem.container.hideParent();
	}
}

function _clearItem() {
	if (this.container.hasParent) {
		//this.setClassName(this.outClass);
		//this.setClassName('subitemout');
		if (this.hasSub && this.style.backgroundImage != 'url(' + this.outImg.src + ')') this.style.backgroundImage = 'url(' + this.outImg.src + ')';
	} else {
		this.setClassName(this.outClass);
		this.style.backgroundColor = this.getMouseOutColor();
		if (this.style.backgroundImage != 'url(' + this.outImg.src + ')') this.style.backgroundImage = 'url(' + this.outImg.src + ')';
	}
}
