//-------- Config-Variablen --------------
var menuPanelContainer = "#menuPanels";
var menuClass = "panelItem";
var activeMenu ;
var timeOut;
var menuLocked = false;
var nextMenu;
var effects = [];
var first = true;
var firstTimeout = null;

/*
 * Wird aufgerufen, wenn das DOM fertig geladen und aufgebaut ist
 */
window.addEvent('domready', function(){
	$$(menuPanelContainer + ' .' + menuClass).each(function(el) {
		//Effekte anlegen
		effects[el.getProperty('id')] = new Fx.ExtendedSlide(el, {mode: 'vertical', direction: 'reverse'});
		//Panels einklappen
		effects[el.getProperty('id')].toggle();
		effects[el.getProperty('id')].addEvent('onComplete', unlockMenu);
	});
	
	activeMenu = null;
});

/*
 * Wechselt die Menü-Panels
 * Ist kein Menü aktiv, wird das gewünschte ausgeklappt.
 * Andernfalls wird das Menü ohne Animation gewechselt
 */
function slide(menuName, change){
	if(first)
	{
		//Beim ersten Mal, muss ein wenig gewartet werden
		first = false;
		firstTimeout = window.setTimeout('slide("' + menuName + '")', 500);
		return;
	}

	// Zeichen für ID-Selektor entfernen (JQuery Kompatibilität)
	if(menuName.charAt(0) == '#')
		menuName = menuName.substr(1);
		
	try{
		stopTimeout();
			
		var menu = $(menuName);
		
		if(menu == activeMenu)
			return;
		
		menu.setStyle('visibility', 'visible');
		
		if(!$defined(menu))
			return;
		
		// Wenn Panel bereits offen
		if(activeMenu != null){
			if(menuLocked){
				nextMenu = menuName; 
				return;
			}
			
			menuLocked = true;
			effects[activeMenu.getProperty('id')].hide(); // altes verstecken
			effects[menu.getProperty('id')].show(); // neues anzeigen
			
			activeMenu = menu;
			menuLocked = false;
			
		}
		// Wenn kein Panel offen
		else{
			//Panel setzen und anzeigen
			if($defined(change) && !change)
				return;
				
			if(menuLocked){
				nextMenu = menuName;
				return;
			}
			activeMenu = menu;
			menuLocked = true;
			
			effects[menu.getProperty('id')].toggle();
		}
		
	}catch(e){

	}
}

/*
 * Timeout zum Verstecken des Menüs wird gegstartet
 */
function startTimeout(){
	timeOut = window.setTimeout("hideMenuPanel()", 100);	
}

/*
 * Timeout zum Verstecken des Menüs wird gestoppt
 */
function stopTimeout(){
	if(timeOut != null)
		window.clearTimeout(timeOut);
}

/*
 * Diese Funktion versteckt das momentan aktive Menü-Panel
 */
function hideMenuPanel(){
	// Wenn gerade eine Animation aktiv ist (== menü aktiv u. gesperrt)
	if(menuLocked && activeMenu != null){
		effects[activeMenu.getProperty('id')].stop();
		effects[activeMenu.getProperty('id')].toggle();
		//menuLocked = false;
		activeMenu = null;
	}
	else //Wenn ein Menü aktiv ist, aber vollständig offen
	if(activeMenu != null){
		menuLocked = true;
		effects[activeMenu.getProperty('id')].toggle();
		activeMenu = null;
	}
}

/*
 * Diese Funktion wird nach dem Toggle-Vorgang aufgerufen.
 * Sie hebt die Sperre des Menüs für andere Animationen wieder auf
 */
function unlockMenu(){	
	menuLocked = false;
	if(nextMenu == null)
		return;
	
	if(this.activeMenu != null)
		slide(nextMenu, true);
	else
		slide(nextMenu, false);
		
	nextMenu = null;
}

var acitveContent = null;
var activeMain = '';
function content(idSelektor, isMain) {
	
	if (isMain == true) activeMain = idSelektor;
	
	if (acitveContent != null) {
		document.getElementById(acitveContent).style.display = "none";
		//jQuery("#" + acitveContent).hide("fast");
		acitveContent = null;
	}
	document.getElementById(idSelektor).style.display = "block";
	//jQuery("#" + idSelektor).show("normal");
	acitveContent = idSelektor;
	/*
	if (idSelektor != null) {
		jQuery.("#" + acitveContent).hide("fast");
		 acitveContent = null;
	}
	jQuery.("#" + idSelektor).show("fast");
	acitveContent = idSelektor;
	*/
}

function restore_content() {
	if (acitveContent != null) {
		document.getElementById(acitveContent).style.display = "none";
		//jQuery("#" + acitveContent).hide("fast");
	}
	document.getElementById(activeMain).style.display = 'block';
	acitveContent = activeMain;
}

var menuIMG = new Array();
function switchIMG(id, hoverIMG) {
	if (hoverIMG == 'none') return;
	if (typeof menuIMG[id] == "undefined") {
		menuIMG[id] = new Array();
		menuIMG[id]['over'] = hoverIMG;
		menuIMG[id]['off'] = document.getElementById(id).src;
	}
	if (hoverIMG == '') {
		document.getElementById(id).src = menuIMG[id]['off'];
	} else {
		 if (typeof menuIMG[id]['over'] != "undefined") document.getElementById(id).src = menuIMG[id]['over'];
	}
}
