function ieVer() {
	msieIndex = navigator.appVersion.indexOf( "MSIE" ) + 5;
  	return(parseFloat(navigator.appVersion.substr(msieIndex, 3))); 
}

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 ieVersion = ieVer();
var dom = (document.getElementById) ? true : false;
var safari = ( navigator.userAgent.indexOf('Safari') != -1 ) ? true : false;
var lastTriggerRef;



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 += '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);
}

//Prototypes for ie 5.5 and below
function shift () {
  var val = this[0];
  for (var i = 1; i < this.length; ++i) {
    this[i-1] = this[i];
  }
  this.length--;
  return val;
} 

function unshift() {
  var i = unshift.arguments.length;
  for (var j = this.length - 1; j >= 0; --j) {
    this[j + i] = this[j];
  }
  for (j = 0; j < i; ++j) {
    this[j] = unshift.arguments[j];
  }
} 

if( ie && ieVersion <= 5.5) {
	Array.prototype.shift = shift;
	Array.prototype.unshift = unshift;
}

//end prototypes
function reverseMenuEntries() {
	var i = 2;
	while( eval('window.menu' + i) ) {
		var menuRef = eval('window.menu' + i);
		var width = menuRef.shift();
		menuRef.reverse();
		menuRef.unshift( width );
		i++;
	}
}

function generateMenu(lvl,parentRef) {
	if(menuNumber == null) {
		 menuNumber = 1;
	} 
	if(lvl == null) {
		if(mainProps['switchDirection']) { 
			reverseMenuEntries();
		}
		var lvl = 0;
	}
	var menuRef = eval('menu' + menuNumber);
	var sectionRef = (menuNumber == 1) ? sectionProps1 : sectionProps2;
	var width = 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) {
			if(mainProps['orientation'] == 'horizontal') {
				top = mainProps['y'];
				if( mainProps['x'] == -1 ) {
					left =  getMenuOffset() + (menuRef[0] * (i - 1))
				} else {
					left =  mainProps['x'] + (menuRef[0] * (i - 1));
				}
				if(i > 1) {
					left += leftCorrection * (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 ): ( 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']
	}
	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'])) {
		if(ref != lastTriggerRef) {
			ref.style.backgroundColor =  sectionRef['bgColorOut']
		}
	}
	if(ref != lastTriggerRef) {
		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'];
				}
				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'];
		}
		lastTriggerRef.style.color = sectionRef['fgColorOut'];
		lastTriggerRef = null; 
	} 
	
}
		 
