	/*
		mfischer, 2006.11.06
		After the page is loaded, hook over another JS which
		on mouseover over a link also changes the li upwards
		so the background-color goes with it.
	*/
	/*
		Global variable for custom neuroservice.at code
	*/
	var nsat = {
		/* Apply 'behaviours' to specific selectors, in this case the main menu
		 * navigation */
		rules : {
			'.main-menu a' : function(el) {
				nsat.menuhover.apply(el);
			},
			'.sub-menu-container div[id]' : function(el) {
				nsat.menuhover.applySubmenuObserver(el);
			},
			'#font-size-smaller' : function(el) {
				el.onclick = nsat.fontsizeChanger.eventSmaller;
				el.style.visibility = 'visible';
			},
			'.header-menu .vertical-dotted-line' : function(el) {
				el.style.visibility = 'visible';
			},
			'#font-size-bigger' : function(el) {
				el.onclick = nsat.fontsizeChanger.eventBigger;
				el.style.visibility = 'visible';
			},
			'.header-menu img[src|=fileadmin/templates/img/misc/header/smaller]' : function(el) {
				el.onclick = nsat.fontsizeChanger.eventSmaller;
			},
			'.header-menu img[src|=fileadmin/templates/img/misc/header/bigger]' : function(el) {
				el.onclick = nsat.fontsizeChanger.eventBigger;
			}
		},
		/* the post main menu JS code to make the hover correctly on the menus
		 * with the mouse over them */
		menuhover : {
			domSubMenuContainer : null,
			aMainmenuLi : [],
			aSubmenuDiv : [],
			iMouseoutDelay : 500, // in [ms]
			iTimerDelay : null,
			iDelayLi : null,
			iPageActiveMenuIndex : -1,
			bPageActiveMenuHasSubmenu : false,
			apply : function(el) {
				// search for the parent LI element
				var dLi = el;
				while (dLi.parentNode) {
					dLi = dLi.parentNode;
					if (dLi.tagName == 'LI') {
						break;
					}
				}
				// this part has been moved to setSubMenuHeight because it was
				// needed before page is loaded
				/*
				dLi._current = false;
				if (dLi.className == 'on') {
					dLi._current = true;
					nsat.menuhover.iPageActiveMenuIndex = nsat.menuhover.aMainmenuLi.length;
				}
				*/
				el._li = dLi;
				// find all elements in the li-tag and also tell them what the parent LI is
				var aElements = el.getElementsByTagName('*');
				for (var i = 0, l = aElements.length; i < l; i++) {
					aElements[i]._li = el;
				}
				// apply the over/out event
				Event.observe(el, 'mouseover', nsat.menuhover.eventMouseOver);
				Event.observe(el, 'mouseout', nsat.menuhover.eventMouseOut);
				dLi._index = nsat.menuhover.aMainmenuLi.length;
				nsat.menuhover.aMainmenuLi[ nsat.menuhover.aMainmenuLi.length ] = dLi;
			},
			applySubmenuObserver : function(el) {
				el._index = nsat.menuhover.aSubmenuDiv.length;
				Event.observe(el, 'mouseover', nsat.menuhover.eventSubmenuOver);
				Event.observe(el, 'mouseout', nsat.menuhover.eventSubmenuOut);
				nsat.menuhover.aSubmenuDiv[ nsat.menuhover.aSubmenuDiv.length ] = el;
			},
			eventMouseOver : function(ev) {
				var el = ev.target || ev.srcElement;
				// Is the main menu element currently selected the currently active one?
				if (el._li._current == false) {
					if (el._li._hasSubmenu) {
						el._li.className = 'on';
						if (nsat.menuhover.iPageActiveMenuIndex >= 0) {
							nsat.menuhover.aMainmenuLi[ nsat.menuhover.iPageActiveMenuIndex ].className = 'onborder';
						}
					} else {
						// If no, don't show any submenu which may currently be
						// displayed. This also means that the current active
						// (not the current selected) element needs a
						// border additionally at the bottom, if it has a
						// submenu which needs to be hidden.
						el._li.className = 'onborder';
						if (nsat.menuhover.iPageActiveMenuIndex >= 0) {
							nsat.menuhover.aMainmenuLi[ nsat.menuhover.iPageActiveMenuIndex ].className = 'onborder';
							nsat.menuhover.aSubmenuDiv[ nsat.menuhover.iPageActiveMenuIndex ].style.display = 'none';
						}
					}
				}
				if (nsat.menuhover.iTimerDelay) {
					window.clearTimeout(nsat.menuhover.iTimerDelay);
					nsat.menuhover.iTimerDelay = null;
					var li = nsat.menuhover.iDelayLi;
					if (li) {
						if (li != el._li && li._current == false) {
							li.className = '';
						}
                        if (nsat.menuhover.aSubmenuDiv[ li._index ] != null) {
    						nsat.menuhover.aSubmenuDiv[ li._index ].style.display = 'none';
                        }
						if (el._li._index == nsat.menuhover.iPageActiveMenuIndex) {
							var pageActiveLi = nsat.menuhover.aMainmenuLi[ nsat.menuhover.iPageActiveMenuIndex ];
							if (pageActiveLi._hasSubmenu) {
								pageActiveLi.className = 'on';
							} else {
								pageActiveLi.className = 'onborder';
							}
						} else if (nsat.menuhover.iPageActiveMenuIndex >= 0 && li != el._li) {
							var pageActiveLi = nsat.menuhover.aMainmenuLi[ nsat.menuhover.iPageActiveMenuIndex ];
							pageActiveLi.className = 'onborder';
						}
					}
				}
				if (el._li._index < nsat.menuhover.iPageActiveMenuIndex) {
                    if (nsat.menuhover.aSubmenuDiv[ nsat.menuhover.iPageActiveMenuIndex ] != null) {
    					nsat.menuhover.aSubmenuDiv[ nsat.menuhover.iPageActiveMenuIndex ].style.display = 'none';
                    }
				}
                if (nsat.menuhover.aSubmenuDiv[ el._li._index ] == null) {
                    return;
                }
				nsat.menuhover.aSubmenuDiv[ el._li._index ].style.display = 'block';
			},
			eventMouseOut : function(ev) {
				var el = ev.target || ev.srcElement;
				nsat.menuhover.iTimerDelay = window.setTimeout( nsat.menuhover.eventMouseOutDelay, nsat.menuhover.iMouseoutDelay );
				nsat.menuhover.iDelayLi = el._li;
			},
			eventMouseOutDelay : function() {
				nsat.menuhover.iTimerDelay = null;
				var li = nsat.menuhover.iDelayLi;
				nsat.menuhover.iDelayLi = null;
				if (li && li._current == false) {
					if (nsat.menuhover.aSubmenuDiv[ li._index ] != null) {
    					nsat.menuhover.aSubmenuDiv[ li._index ].style.display = 'none';
                    }
					li.className = '';
				}
				if (nsat.menuhover.iPageActiveMenuIndex >= 0) {
					nsat.menuhover.aSubmenuDiv[ nsat.menuhover.iPageActiveMenuIndex ].style.display = 'block';
					var pageActiveLi = nsat.menuhover.aMainmenuLi[ nsat.menuhover.iPageActiveMenuIndex ];
					if (pageActiveLi._hasSubmenu) {
						pageActiveLi.className = 'on';
					} else {
						pageActiveLi.className = 'onborder';
					}
				}
			},
			eventSubmenuOver : function() {
				window.clearTimeout(nsat.menuhover.iTimerDelay);
			},
			eventSubmenuOut : function() {
				nsat.menuhover.iTimerDelay = window.setTimeout( nsat.menuhover.eventMouseOutDelay, nsat.menuhover.iMouseoutDelay );
			},
			setSubMenuHeight : function() {
				// first, find and remember also for later, which of the menus
				// is active. This is needed for the next step to look in the
				// actual submenu whether there is content or not. The whole
				// logic assumes that on the start page is never a permanent
				// submenu
				var iActive = -1;
				var counter = -1;
				var aMainMenu = $A($('main-menu').getElementsByTagName('li'));
				aMainMenu.each(
					function(el) {
						counter++;
						el._current = false;
						el._hasSubmenu = false;
						if (el.className == 'on') {
							el._current = true;
							iActive = counter;
						}
					}
				);
				nsat.menuhover.iPageActiveMenuIndex = iActive;
				// now find the submenu of the current active main menu and see
				// if it actually has submenu entries. If it has, we adjust the
				// CSS height of #sub-menu-container to a have a specific
				// height and move the complete page down so it doesn't overlap
				// anything
				var aSubmenus = $A($('sub-menu-container').getElementsByTagName('div'));
				for (var i = 0, l = aSubmenus.length; i < l; i++) {
					if (aSubmenus[i].getElementsByTagName('h1').length > 0) {
						// has submenu
						aMainMenu[i]._hasSubmenu = true;
						if (i == iActive) {
							nsat.menuhover.bPageActiveMenuHasSubmenu = true;
							$('sub-menu-container').style.height = '3em';
						}
					} else if (i == iActive) {
						aMainMenu[i].className = 'onborder';
					}
					if (i == nsat.menuhover.iPageActiveMenuIndex) {
						aSubmenus[i].style.display = 'block';
					} else {
						aSubmenus[i].style.display = 'none';
					}
				}
			}
		},
		/**
		 * Package everything related to changing the font size together
		 */
		fontsizeChanger : {
			iCurrentValuePos : 1,
			sValueUnit : '%',
			aFontSizeValues : [60, 70, 80, 100],
			eventSmaller : function() {
				nsat.fontsizeChanger.iCurrentValuePos--;
				if (nsat.fontsizeChanger.iCurrentValuePos < 0) {
					nsat.fontsizeChanger.iCurrentValuePos = 0;
					return false;
				}
				document.getElementsByTagName('body')[0].style.fontSize = nsat.fontsizeChanger.aFontSizeValues[ nsat.fontsizeChanger.iCurrentValuePos ] + nsat.fontsizeChanger.sValueUnit;
				nsat.cookie.set('fontsize', nsat.fontsizeChanger.iCurrentValuePos, 30, '/');
				return false;
			},
			eventBigger : function() {
				nsat.fontsizeChanger.iCurrentValuePos++;
				if (nsat.fontsizeChanger.iCurrentValuePos >= nsat.fontsizeChanger.aFontSizeValues.length) {
					nsat.fontsizeChanger.iCurrentValuePos = nsat.fontsizeChanger.aFontSizeValues.length - 1;
					return false;
				}
				document.getElementsByTagName('body')[0].style.fontSize = nsat.fontsizeChanger.aFontSizeValues[ nsat.fontsizeChanger.iCurrentValuePos ] + nsat.fontsizeChanger.sValueUnit;
				nsat.cookie.set('fontsize', nsat.fontsizeChanger.iCurrentValuePos, 30, '/');
				return false;
			},
			applyCookieFontsize : function() {
				// get cookie value
				var pos = nsat.cookie.get('fontsize');
				if (pos == null) {
					return;
				}
				if (pos >= 0 && pos < nsat.fontsizeChanger.aFontSizeValues.length) {
					nsat.fontsizeChanger.iCurrentValuePos = pos;
					// if not present, use default
					document.getElementsByTagName('body')[0].style.fontSize = nsat.fontsizeChanger.aFontSizeValues[ nsat.fontsizeChanger.iCurrentValuePos ] + nsat.fontsizeChanger.sValueUnit;
				}
			}
		},
        /**
         * Package cookie related functions
         */
        cookie : {
            /**
             * Retrieves the cookie by that name or returns null
             * @param	string	Name of cookie
             */
            get : function(sName) {
                if (document.cookie.length > 0) {
                    var begin = document.cookie.indexOf(sName + "=");
                    if (begin != -1) {
                        begin += sName.length + 1;
                        var end = document.cookie.indexOf(";", begin);
                        if (end == -1) {
                            end = document.cookie.length;
                        }
                        return unescape(document.cookie.substring(begin, end));
                    }
                }
                return null;
            },
            /**
             * Sets a cookie by name, value and days to expire
             * @param	string	Name of cookie
             * @param	string	Value of cookie to set
             * @param	integer	TTL of cookie in days
			 * @param	string	Optional: Path to set the cookie to; if missing default to current page
             * @todo	Why days?
             */
            set : function(sName, sValue, iExpireDays, sPath) {
                var expireDate = new Date ();
                expireDate.setTime(expireDate.getTime() + (iExpireDays * 24 * 3600 * 1000));
				var sPathPart = '';
				if (typeof sPath == 'string') {
					sPathPart = '; path=' + sPath;
				}
                document.cookie = sName + "=" + escape(sValue) + ((iExpireDays == null) ? "" : "; expires=" + expireDate.toGMTString()) +
					sPathPart;
            },
            /**
             * Deletes the cookie by the name
             * @param	string	Name of cookie
             */
            del : function(sName) {
                if (nsat.cookie.get(sName)) {
                    document.cookie = sName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
                }
            }
        }
	};

	/*
	var debugtextarea = null;

	function deb(msg) {
		if (debugtextarea) {
			debugtextarea.value = debugtextarea.value + msg + '\n';
		}
	}

	function test() {
		var ta = document.createElement('textarea');
		ta.id = 'debugtextarea';
		ta.style.width = '500px';
		ta.style.height = '15em';
		ta.style.position = 'relative';
		ta.style.top = '-25em';
		document.getElementsByTagName('body')[0].appendChild(ta);
		debugtextarea = ta;
	}
	*/

	Behaviour.register(nsat.rules);
	// Event.observe(window, 'load', nsat.fontsizeChanger.applyCookieFontsize);
	/*
	if (window.addEventListener) {
		window.addEventListener('load', test, false);
	}
	*/

