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;
var openMenus = Array();
var visitedTriggers = Array();

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

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,btrigger ) {
        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-top-width:' + sectionRef['borderWidth'] + 'px;';
        out += 'border-bottom-width:' + sectionRef['borderWidth'] + 'px;';
	if( btrigger ) {
		out += 'border-left-width:' + sectionRef['borderWidth'] + 'px;';
		out += 'border-right-width:' + sectionRef['borderWidth'] + 'px;';
	}else {
	 	out += 'border-left-width:0px;';               
	 	out += 'border-right-width:0px;';	
	}
	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:' +(5000 + zIndex) + ';';
        out += 'visibility:' + visibility + ';';
        if(subImage || btrigger == 0) {
                out += '" onMouseOver=rollOver(this) onMouseOut=rollOut(this)><img  id="menu_image' + id + '" src ="/static/images/pfeil_navi.gif" border = 0  vspace = ' + (sectionRef['padding'] / 2) + ' align = "left">&nbsp;&nbsp;' + 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 push( v ) {
	this[ this.length ] = v;
} 

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;
	Array.prototype.push = push;
}

//end prototypes

function reverseMenuEntries() {
	var i = 1;
	while( ++i ) {
		var menuRef = eval('menu' + i);
		if( menuRef == null ) break
		var width = menuRef.shift()
		menuRef.reverse();
		menuRef.unshift( width );
	}
}


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 = (ie ) ? menuRef[0] - 12  : 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( typeof menuRef[0] != "number" ) {
                                	left = menuRef[0][ ( i - 1 )];
                                } else 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 {
                
                                var correction = sectionRef['padding'] * 2 + sectionRef['borderWidth'] * 2
                                var pixelLeft = ie ? (parentRef.style.pixelLeft -correction ): ( safari ? parentRef.offsetLeft : parseInt( parentRef.style.left ) );
                                
                        //}
                        var pixelWidth = -1 + ( 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( ie ) { left += 12; }
                        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( ie  ) {top -= 2 };
                if(menuRef[i][1] == 1) {
                		generateDIV( top,left,width, sectionRef, menuRef[i], id, menuNumber, visibility,sectionRef['subImage'], lvl);
                		
                } else {
                		generateDIV( top,left,width, sectionRef, menuRef[i], id, menuNumber, visibility,'', lvl );
      
                }
                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( (ref.id.indexOf('menu_1_') != -1) && activeSubMenu != -1) {
		resetOpenMenus();
        }

        if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
                ref.style.backgroundColor =  sectionRef['bgColorOver']
        }
	var img = document.getElementById( 'menu_image' + ref.id );
	if( img ) {
		img.src = '/static/images/pfeil_navi_over.gif';
        }
	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;
                visitedTriggers.push( lastTriggerRef );
        } 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;
        var img = document.getElementById( 'menu_image' + ref.id );  

	if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
                if(ref != lastTriggerRef) {
                        ref.style.backgroundColor =  sectionRef['bgColorOut']
			if( img ) {
	                        img.src = '/static/images/pfeil_navi.gif';
	                 }

                }
        }
        if(ref != lastTriggerRef) {
                ref.style.color = sectionRef['fgColorOut'];
                if( img ) {
	                img.src = '/static/images/pfeil_navi.gif';
	         }

		//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'];
               			var img = document.getElementById( 'menu_image' + baseID );
				 if( img ) {
				    img.src = '/static/images/pfeil_navi.gif';
				 }
                        }       
                }
        } 
        visitedTriggers.length = 0;
        openMenus.length = 0;
}

function resetOpenMenus() {
        for( var i = 0; i < openMenus.length; i++ ) {
                 closeMenuByNumber( openMenus[ i ] );
        }
        for( var i = 0; i < visitedTriggers.length; i++ ) {
                var sectionRef = (visitedTriggers[ i ].id.indexOf('menu_1_') != -1)  ?  sectionProps1 : sectionProps2;
                if((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
                        visitedTriggers[ i ].style.backgroundColor =  sectionRef['bgColorOut'];
                }
                visitedTriggers[ i ].style.color = sectionRef['fgColorOut'];
        }
        visitedTriggers.length = 0;
        openMenus.length = 0;
}

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'];
                	var img = document.getElementById( 'menu_image' + lastTriggerRef.id  );
			 if( img ) {
			     img.src = '/static/images/pfeil_navi.gif';
			 }
															      	
		}
                lastTriggerRef.style.color = sectionRef['fgColorOut'];
                lastTriggerRef = null; 
        } 
        
} 

