/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

menus = new Array("File","Search","Help","Debug")
var activeMenu = null; //defines which menu is currently open

//runs at onload of window        
function initialize() {
    userEventsInit(); //set up all user events
}

function menuOver() {
    document.onclick = null;
    if (window.event != null) window.event.cancelBubble=true; // IE requires this
}

function menuOut() {
    document.onclick = eventHideMenu;
    if (window.event != null) window.event.cancelBubble=true; // IE requires this
}

//turns on the display for nav menus
function eventShowMenu(e) {
    if (activeMenu != null) eventHideMenu();
    activeMenu = "menu"+this.id.substring(3,this.id.length)
    obj = document.getElementById(activeMenu);
    if (obj != null) obj.style.visibility = "visible";
    if (document.all) document.onclick = eventHideMenu; // IE requires this
    if (window.event != null) window.event.cancelBubble=true; // IE requires this
}

//hides nav menus and pop ups
function eventHideMenu() {
    document.onclick = null;
    if (activeMenu != null) {
        obj = document.getElementById(activeMenu);
        obj.style.visibility = "hidden";
    }
}

//nav events
function userEventsInit() {
    for (i=0; i < menus.length; i++) {
        obj = document.getElementById("nav"+menus[i]);
	    if (obj != null) {
		    obj.onclick = eventShowMenu;
	    }

        obj = document.getElementById("menu"+menus[i]);
	    if (obj != null) {
		    obj.onmouseover = menuOver;
		    obj.onmouseout = menuOut;
	    }
    }
}

/*
 * Aenderungen f�r Menu
 * 2005 proventis
 */

var lastmenuMouseOver;
var lastmenuMouseOvertimer;
var lastmenuMouseOuttimer;
var lastmenuPopupTimer;
var lastPopup;

/* Kennung fuer Psyma auf internen Link */
var psyma_navi_click = false;

function menuMouseOver(e) {
	var id = this.getAttribute('id');

	var b1 = id && "asnh".indexOf(id.charAt(0))>=0;
	var b2 = lastmenuMouseOver && "asnh".indexOf(lastmenuMouseOver.charAt(0))>=0;
	var s1 = b1?id.slice(1):id;
	var s2 = b2?lastmenuMouseOver.slice(1):lastmenuMouseOver;
	debug("s1: "+s1+"  s2:"+s2);
	if (s1==s2) {
		if (lastmenuMouseOuttimer) {
			window.clearTimeout(lastmenuMouseOuttimer);
			lastmenuMouseOuttimer=false;
		}
		return false;
	}
	menuMouseOutNow();

	lastmenuMouseOver = id
	lastmenuMouseOvertimer = window.setTimeout("menuMouseShowNow()",10);
	return true;
}

function menuMouseOut(e) {
	if (lastmenuMouseOvertimer||lastmenuMouseOuttimer) return;
	lastmenuMouseOuttimer = window.setTimeout("menuMouseOutNow()",10);
}

function menuMouseShowNow() {
	if (lastmenuMouseOvertimer) window.clearTimeout(lastmenuMouseOvertimer);
	lastmenuMouseOvertimer = false;
	if (lastmenuMouseOver) {
		document.onclick=null;
		menuMouseShow(lastmenuMouseOver,true);
	}
}

function menuMouseOutNow() {
	if (lastmenuMouseOuttimer) {
		window.clearTimeout(lastmenuMouseOuttimer);
		lastmenuMouseOuttimer = false;
	}
	if (lastmenuMouseOver) menuMouseShow(lastmenuMouseOver,false);
	lastmenuMouseOver = false;
	window.status = '';
}

function menuMouseShow(id,b) {
	var d;
	d = document.getElementById('h'+id);
	if (d) d.style.display=b?'':'none';

	d = document.getElementById('n'+id);
	if (d) d.style.display=b?'none':'';

	if (b) {
		var s = 's'+id;
		d = document.getElementById(s);
		if (d && s!=lastPopup) menuHidePopup(null);
		if (d) {
			d.style.visibility='visible';
			d.onmouseout = menuPopupMouseOut;
			d.onmouseover = menuPopupMouseOver;
			lastPopup = s;
	
			try {
				if (document) document.onclick = menuHidePopup; // IE requires this
			} catch(ex) {}
			try {
				if (window.event != null) window.event.cancelBubble=!b; // IE requires this
			} catch(ex) {}
		}
	}
}

function menuHidePopup(e) {
	if (!lastPopup) return false;
	menuMouseOut(e);
	var d = document.getElementById(lastPopup);
	if (d) d.style.visibility='hidden';
	try {
		if (document) document.onclick = null; // IE requires this
	} catch(ex) {}
	try {
		if (window.event != null) window.event.cancelBubble=false; // IE requires this
	} catch(ex) {}
	
	lastPopup = false;
}

function menuPopupMouseOver(e) {
	if (lastmenuPopupTimer) window.clearTimeout(lastmenuPopupTimer);
	lastmenuPopupTimer = false;
}

function menuPopupMouseOut(e) {
	if (lastmenuPopupTimer) window.clearTimeout(lastmenuPopupTimer);
	lastmenuPopupTimer = window.setTimeout("menuHidePopup()",300);
}

function menuClick(e) {
	var d = document.getElementById('a'+lastmenuMouseOver);
	if (d) {
		var b = true;
		var link = this.getAttribute('href');
		try {
			if (d.target.length>0) {
				var ccbank = /cc-bank/;
				if (ccbank.test(link)) {
					window.open(link,d.target,"width=544,height=500,menubar=no,toolbar=no,location=no,dependent=yes");
				} else {
					window.open(link,d.target);
				}
				b = false;
			}
		} catch (e) {
			b=true;
			alert(e);
		}
		if (b) {
			psyma_navi_click = true;
			window.location.href=link;
		}
	}
	return true;
}

function menuLinkMouseOver(e) {
	window.status = this.getAttribute('title');
	return true;
}

function menuLinkMouseOut(e) {
	window.status = '';
	return true;
}

function menuInit() {
	var a = document.getElementsByTagName("div");
	for(i=0; i<a.length;i++) {
		var o = a[i];
		if (o.getAttribute('name') != 'mouseover') continue;
		o.onmouseover = menuMouseOver;
		o.onmouseout = menuMouseOut;
		o.onclick = menuClick;
		
		var id = o.getAttribute('id');
		if (id) {
			var e = document.getElementById('h'+id);
			if (e) {
				e.style.visibility = 'visible';
				e.style.position = 'static';
				e.style.display = 'none';
			}

			var link = document.getElementById('a'+id);
			if (link) {
				o.setAttribute('href',link.getAttribute('href'));
				link.setAttribute('href','');
				link.onmouseover = menuLinkMouseOver;
				link.onmouseout = menuLinkMouseOut;
				link.onclick = function(e) { return false; }
				try {
					o.style.cursor='pointer';
				} catch (ex) {}
			}
			
		}
	}
	//debugInit();
}

var debugElement;
var debugSwitch = false;
var debugCnt = 1;

function debugInit() {
	debugElement=document.createElement("div");
	document.body.appendChild(debugElement);
	debugElement.style.border='1px solid green';
	debugElement.style.zIndex=20000;
	debugElement.style.padding='4px';
	debugElement.style.visibility='show';
	debugElement.style.position='fixed';
	debugElement.style.width='98%';
	debugElement.style.display='block';
	debugElement.style.backgroundColor='#DDDDFF';
	debugElement.style.height='10em';
	debugElement.style.bottom='0px';
	debugElement.style.overflow='scroll';
}

function debug(s) {
	if (!debugElement) return;
	var d = document.createElement("div");
	if (debugSwitch) d.style.backgroundColor='#EEEEFF';
	d.appendChild(document.createTextNode( (debugCnt++)+' '+s));
	var f = debugElement.firstChild;
	if (f) {
		debugElement.insertBefore(d,f);
	} else {
		debugElement.appendChild(d);
	}
	debugSwitch=!debugSwitch;
}


var lastMenuA = false;
var lastMenuAHideTimer = false;

function hideMenuAPopup() {
	if (lastMenuAHideTimer) {
		window.clearTimeout(lastMenuAHideTimer);
	}
	lastMenuAHideTimer = window.setTimeout("hideMenuAPopupNow()", 200);
}

function showMenuAPopup(currentid) {
	var e = document.getElementById(currentid);
	if (e) {
		if (lastMenuAHideTimer) {
			window.clearTimeout(lastMenuAHideTimer);
			lastMenuAHideTimer=false;
		}
		if (e!=lastMenuA) {
			hideMenuAPopupNow();
			e.style.display='block';
			lastMenuA=e;
		}
	}
}

function hideMenuAPopupNow() {
	if (lastMenuAHideTimer) {
		window.clearTimeout(lastMenuAHideTimer);
		lastMenuAHideTimer=false;
	}
	if (lastMenuA) {
		lastMenuA.style.display='none';
		lastMenuA = false;
	}
}


if(window.navigator.systemLanguage && !window.navigator.language) {

	/* IE 5 und 6 kann kein Hoover, drum per script nachbauen */
	function hoverIE() {
		var versionregex = /.*MSIE (\d)\.*/;
		var result = versionregex.exec(window.navigator.userAgent);
		if (!result) return;
		var version = parseInt(result[1]);
		if (version>6) return;

		var menunav = document.getElementById("mainnav");
		if (menunav) {
			var LI = menunav.firstChild;
			while (LI) {
				if (LI.nodeName == "LI") {
					var UL = sucheUL(LI);
					if (UL) {
						LI.onmouseover=einblenden;
						LI.onmouseout=ausblenden;
					}
				}
				LI = LI.nextSibling;
			}
		}
	}
	
	function sucheUL(node) {
		var e = node.firstChild;
		while(e) {
			if (e.nodeName == "UL") return e;
			e = e.nextSibling;
		}
	    return false;
	}

	  function einblenden() {
	    var UL = sucheUL(this);
	    UL.style.display = "block";
	  }
	  function ausblenden() {
	    sucheUL(this).style.display = "none";
	  }
	  
	  window.onload=hoverIE;
}

