var triggers = new Array();
var timeOutID;
var activeSubMenu = -1;
var menuNumber = 1;
var ns = (document.layers) ? true : false;
var ie = (document.all) ? true: false;
var dom = (document.getElementById) ? true : false;
var safari = ( navigator.userAgent.indexOf('Safari') != -1 ) ? true : false;
var lastTriggerRef;

var intargiaPositions= new Array( 0, 30, 105, 152, 188 );
var intargiaWidth = new Array( 35, 81, 56, 49, 46 );

if( menu_en ) {
	var intargiaPositions= new Array( 0, 30, 85, 130, 183 );
	var intargiaWidth = new Array( 35, 70, 48, 40, 56 );
}
var kmhMainDivRef = null;
function  getPageId( search ){
	var temp = search.split( '&' );
	if( !temp[ 0 ] || temp[ 0 ].indexOf( 'javascript' ) != -1  ) { return -1; }
	temp = temp[ 0 ].split( '=' );
	return temp[ 1 ];
}
function getDivReference(key) {
	if(ie) { 
		return document.all[key];
	} else if(ns) {
		return document.layers[key];
	} else if(dom) {
		return document.getElementById(key);
	}
}

function getStyleReference(key) {
	var ref = getDivReference(key)
	if(ie || dom) { 
		return ref.style;
	} else if(ns) {
		return ref
	} 
}

function generateDIV(top,left,width, sectionRef, content, id, zIndex, visibility,subImage) {
	
	if(content[1] == 1) {
		var temp = content[0].split("'");
		var lnk = (temp[1]) ? temp[1] : 'javascript:void(0)';
	} else {
		var lnk = content[1];
	} 
	//alert(lnk);
	//var lnk = (content[1] == 1) ? 'javascript:void(0)' : content[1];
	out = '<a href="' + lnk + '" style="text-decoration:none";>';
	out += '<div id="' + id + '" '; 
	out += 'align="' + sectionRef['alignment'] +'" style="';
	out += 'position: absolute;';
	out += 'top:' + top + 'px;';
	out += 'left:' + left + 'px;';
	if(ie) {
		out += 'width:' + width + 'px;';
	} else {
          out += 'width:' + (width - (2 * sectionRef['borderWidth'] + 2 * sectionRef['padding'])) + 'px;';	
	}
	if((mainProps['bgFirstSection'] == 1 && sectionRef == sectionProps1) || (mainProps['bgSecondSection'] == 1 && sectionRef == sectionProps2)) {
		out += 'background-color:' + sectionRef['bgColorOut'] + ';';
	}
	out += 'padding:' + sectionRef['padding'] + 'px;';
	//out += 'height:' + ( sectionRef[ 'fontSize' ] + sectionRef[ 'borderWidth'  ] + sectionRef[ 'padding' ] ) + 'px;';
	out += 'border-style: solid;';
	out += 'border-width:' + sectionRef['borderWidth'] + 'px;';
	out += 'border-color:' + sectionRef['borderColor'] + ';';
	out += 'color:' + sectionRef['fgColorOut'] + ';';
	out += 'font-family:' + sectionRef['font'] + ';';
	out += 'font-size:' + sectionRef['fontSize'] + 'px;';
	out += 'font-weight:' + sectionRef['fontWeight'] + ';';
	out += 'font-style:' + sectionRef['fontStyle'] + ';';
	out += 'z-index:' + zIndex + ';';
	out += 'visibility:' + visibility + ';';
	if(subImage) {
		out += '" onMouseOver=\'rollOver(this)\' onMouseOut=\'rollOut(this)\'><img src ="' + subImage + '" border = 0  vspace = ' + (sectionRef['padding'] - 1) + ' align = "right">' + content[0]+ '</div></a>';
	} else {
		out += '" onMouseOver=\'rollOver(this)\' onMouseOut=\'rollOut(this)\'>' + content[0] + '</div></a>';
	}
	document.write(out);
	if( content[ 0 ].indexOf( kmhMain ) != -1 ) {
		kmhMainDivRef = getDivReference( id );
		kmhMainDivRef.style.color = sectionRef['fgColorOver'];
		//alert( kmhMainDivRef );
	}
}

function generateMenu(lvl,parentRef) {
	if(menuNumber == null) {
		 menuNumber = 1;
	} 
	if(lvl == null) {
		var lvl = 0;
	}
	var menuRef = eval('menu' + menuNumber);
	var sectionRef = (menuNumber == 1) ? sectionProps1 : sectionProps2;
	var width =  ( lvl == 0 ) ? 0 : menuRef[0];
	var count = menuRef.length;
	var height = 0;
	var top = 0;
	var left = 0;
	var baseID = 'menu_' + menuNumber + '_';
	var visibility = (menuNumber == 1) ? 'visible' : 'hidden';
	for (var i = 1; i < count; i++) {
		id = baseID  + i
		height = (sectionRef['fontSize'] + sectionRef['borderWidth'] * 2  + sectionRef['padding'] * 2);
		if(lvl == 0) {
			//intargia variable width
			kmhMain[ id ] = 1;
			if(mainProps['orientation'] == 'horizontal') {
				top = mainProps['y'];
				left = mainProps['x'] + intargiaPositions[ ( i - 1 ) ] + ( 23 * ( i - 1 ) ) ;
				width = intargiaWidth[ ( i - 1 ) ];
			} else {
				if(mainProps['switchDirection']) {
					top = mainProps['y'] - height * (i-1);
				} else {
					top = mainProps['y'] + height * (i-1);
				}
				if(i > 1) {
					if(mainProps['switchDirection']) {
						top -= topCorrection * (i-1);
					} else {
						top += topCorrection * (i-1);
					}
				}
				left =  mainProps['x'];
				
			}
		} else {
		
			//auskommentiert hbv
			/*if(lvl == 1) {
				var pixelLeft = ie ? parentRef.style.pixelLeft : parseInt(parentRef.style.left);
			} else {
				*/var correction = sectionRef['padding'] * 2 + sectionRef['borderWidth'] * 2
				var pixelLeft = ie ? (parentRef.style.pixelLeft + 2): ( safari ? parentRef.offsetLeft : parseInt( parentRef.style.left ) );
				
			//}
			var pixelWidth = ie ?  parentRef.style.pixelWidth  :  ( safari ? parentRef.offsetWidth : parseInt(parentRef.style.width) );
			pixelWidth += ( ( mac || dom ) && !safari ) ? parseInt(parentRef.style.padding) * 2 + parseInt(parentRef.style.borderWidth) : 0;
			var pixelTop = ie ? parentRef.style.pixelTop : ( safari ? parentRef.offsetTop : parseInt(parentRef.style.top) );
		
			height += topCorrection; 
			if(mainProps['orientation'] == 'horizontal') {
				left = (lvl == 1) ?  pixelLeft : pixelLeft +  pixelWidth + leftCorrection;
				
			} else {
				left = pixelLeft +  pixelWidth + leftCorrection;
			}
			if(mainProps['switchDirection']) {
				top = pixelTop - height * (i-1);
			} else {
				top = pixelTop + height * (i-1);
			}
			if(lvl == 1 && mainProps['orientation'] == 'horizontal') {
				var padding = ( safari ) ? parseInt( parentRef.style.paddingTop ) :  parseInt( parentRef.style.padding );
				var borderWidth = ( safari ) ? parseInt( parentRef.style.borderTopWidth ) : parseInt( parentRef.style.borderWidth );
				var fontSize = parseInt( parentRef.style.fontSize );
				if(mainProps['switchDirection']) {
					top -= padding * 2 + borderWidth * 2 + fontSize + 4;
				} else {
					top += padding * 2 + borderWidth * 2 + fontSize + 4;
				}	
			}	
		}
		
			
		if(menuRef[i][1] == 1) {
			generateDIV(top,left,width, sectionRef, menuRef[i], id, menuNumber, visibility,sectionRef['subImage']);
		} else {
			generateDIV(top,left,width, sectionRef, menuRef[i], id, menuNumber, visibility,'');
		}
		if(menuRef[i][1] == 1) {
			triggers[triggers.length] = id;
			menuNumber++;
			generateMenu(++lvl,getDivReference(id));
			lvl --;
		}
	}
}

function rollOver(ref, pID) {
	var sectionRef = (ref.id.indexOf('menu_1_') != -1)  ?  sectionProps1 : sectionProps2;
	if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
		ref.style.backgroundColor =  sectionRef['bgColorOver']
	}
	if( ref != kmhMainDivRef ) {
		ref.style.color = sectionRef['fgColorOver'];
	}
	ref.style.cursor = 'hand';
	var menuToTrigger = getTriggerRef(ref.id) + 1;
	if(menuToTrigger) {
		if(menuToTrigger != activeSubMenu && activeSubMenu != -1 && ref.id.indexOf('menu_' + activeSubMenu) == -1) {
			closeMenuByNumber(activeSubMenu)
		}
		var menuLength = eval('menu' + menuToTrigger + '.length');
		for(var i = 1; i < menuLength; i++) {
			getStyleReference('menu_' + menuToTrigger + '_' + i).visibility = 'visible';
		}
		activeSubMenu = menuToTrigger;
		lastTriggerRef = ref;
		
	} else if( ref.id.indexOf('menu_' + activeSubMenu) == -1 && activeSubMenu != -1) {
		closeMenuByNumber(activeSubMenu)
	}
	if(ref.id.indexOf('menu_1_') != -1 && !menuToTrigger) {
		hideAllSubMenus()
	}
	
	clearTimeout(timeOutID);
}

function rollOut(ref, pID) {
	var sectionRef = (ref.id.indexOf('menu_1_') != -1)  ?  sectionProps1 : sectionProps2;
	if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
		ref.style.backgroundColor =  sectionRef['bgColorOut']
	}
	if(ref != lastTriggerRef) {
		if( ref != kmhMainDivRef ) {
			ref.style.color = sectionRef['fgColorOut'];
		}
			//ref.style.cursor = 'default';
	}
	timeOutID = setTimeout('hideAllSubMenus()',timeGap);
}

function getTriggerRef(ref) {
	var tCount = triggers.length;
	for(var i = 0; i < tCount; i++) {
		if(triggers[i] == ref) {
		 	return i+1;
		 	break;
		}
	}
	return -1;
}

function hideAllSubMenus() {
	for( var m = 1; m <= menuNumber; m++ ) {
		var menuRef = eval('menu' + m);
		for( var i = 1; i <  menuRef.length ; i++) {
			if(lastTriggerRef) {
				baseID = 'menu_' + m + '_' + i;
				getStyleReference(baseID).visibility = (menuRef == menu1) ? 'visible' :'hidden';
				var sectionRef = (baseID .indexOf('menu_1_') != -1)  ?  sectionProps1 : sectionProps2;
				if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
					getStyleReference(baseID).backgroundColor =  sectionRef['bgColorOut'];
				}
				if( getDivReference( baseID ) != kmhMainDivRef ) {
					getStyleReference(baseID).color = sectionRef['fgColorOut'];
				}
		
			}	
		}
	} 
}

function closeMenuByNumber(menuToClose) {
	var menuLength = eval('menu' + menuToClose + '.length');
	for(var i = 1; i < menuLength; i++) {
		getStyleReference('menu_' + menuToClose + '_' + i).visibility = 'hidden';
	}
	if(lastTriggerRef) {
		var sectionRef = (lastTriggerRef.id.indexOf('menu_1_') != -1)  ?  sectionProps1 : sectionProps2;
		if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
			lastTriggerRef.style.backgroundColor =  sectionRef['bgColorOut'];
		}
		if( lastTriggerRef != kmhMainDivRef  ) {
			lastTriggerRef.style.color = sectionRef['fgColorOut'];
		}
		lastTriggerRef = null; 
	} 
	
}
		 