var active = "";
var opened = "";
var hover = "";
var iniactive = "nav0";
var time = "";
var hidetime = "";
var hovertime = "";

$(document).ready(function(){
	$('#text').jScrollPane({ topCapHeight: 15, showArrows:true, scrollbarWidth: 7 } );
	$('#infoscroll').jScrollPane({ showArrows:true, scrollbarWidth: 7 } );

	var r = 1;
	$("#navigation li").children().each(function() {
        var child = $(this);
		child.attr('id', "nav"+r);
		if (child.hasClass("menu-parent"))  child.addClass("aktiv");
		r++;
	});
	
	var r = 1;
	$("#subnav").children().each(function() {
        var child = $(this);
		
		if(child.attr('id')=="nav0"){
			//isnavcont = child;		
		}
		else{
			child.attr('id', "nav"+r);
			//child.fadeIn("fast");
			//child.fadeOut("fast");
			
			if (child.find(".menu-current").length>0)  {
				child.addClass("aktiv");
				
				if($("#subnav #nav0").contents().length>0){
					$("#subnav #nav0").fadeIn("fast");
					iniactive = "nav0";
				}
				else{
					child.fadeIn("fast");
					iniactive = child.attr('id');
				}
				
			}
			else if(iniactive!="" && iniactive==child.attr('id')){
				child.addClass("aktiv");
				child.fadeIn("fast");	
			}
			else child.fadeOut("fast");
			r++;
		}
		
		if($("#subnav #nav0").contents().length>0){
			$("#subnav #nav0").fadeIn("fast");
			iniactive = "nav0";
		}
	});
	
	var r = 0;
	$("#kopf").children().each(function() {
        var child = $(this);
		child.addClass("nav"+r);
		child.attr('id', "nav"+r);
		if(r>0){
			child.fadeOut("fast");
		}
		r++;
	});
	
	
	$("#navigation a").hover(
		function() {
	        if(hidetime) clearTimeout(hidetime); 
				if(opened==""){	
	        		clearTimeout(hovertime); 
	        		clearTimeout(time); 
					var navid = $(this).attr('id');
					var nav = $("#subnav").find("#"+navid);	
					
					if(active!="" && active!=iniactive){	
						$("#kopf").find("#"+active).fadeOut("fast");			
					}
					$("#subnav div").stop(true, true).fadeOut("fast");		
					$("#navigation").find("a").removeClass("aktiv");	
					//nav.children().fadeIn("fast");
					//alert(nav.attr('id'));
					nav.fadeIn("fast");
					hover = navid;	
				}
			},
			function() {
				var navid = $(this).attr('id');
				var nav = $("#subnav").find("#"+navid);
       				hovertime = setTimeout(function() {
						if(opened==""){			
							nav.fadeOut("slow");
						}
					}, 300); 
							
       				time = setTimeout(function() {
						if(opened==""){	
							$("#subnav").find("#"+iniactive).fadeIn("slow");
							//alert($("#navigation").find("a#"+iniactive).attr('id'));
							$("#navigation").find("a#"+iniactive).addClass('aktiv');
							hover = "";	
						}
					}, 300); 
			}
	);
	
	$("#subnav div").hover(
		function() {
			var timeid = $(this).attr('id');
	        clearTimeout(hovertime); 
        	clearTimeout(hidetime); 
        	clearTimeout(time); 
			//alert(hover+" == "+timeid+" && "+active+" != "+timeid+" && "+timeid+" != "+iniactive);
			if(hover == timeid && active != timeid && iniactive != timeid){
				setNav(timeid, false);
				hover = "";	
			}
		},
		function() {
			var timeid = $(this).attr('id');
			//alert(active+" != "+timeid+" && "+timeid+" != "+iniactive);
			if(active != "" && iniactive != timeid && iniactive != active){
	     		hidetime = setTimeout(function() {
					setNav(timeid, true);
				}, 300); 
			}
		}
	);
	$("#kopf img").hover(
		function() {
			var timeid = $(this).attr('id');
        	clearTimeout(hidetime); 
        	clearTimeout(time); 
			
			if(hover == timeid && active != timeid && iniactive != timeid){
				setNav(timeid, false);
			}
		},
		function() {
			var timeid = this.className;
			if(active != "" && iniactive != timeid && iniactive != active){
	     		hidetime = setTimeout(function() {
					setNav(timeid, true);
				}, 300); 
			}
		}
	);
	
	
		$("#navigation a").click(function() {
			if($(this).attr('id')!="nav1"){
				if(opened == $(this).attr('id')){
					opened = "";
					setNav($(this).attr('id'), true);
					return false;
				}
				else {
					active = opened;
					opened = "";
					if(active!="") setNav(active, true);
					setNav($(this).attr('id'), true);
					opened = $(this).attr('id');
	        		clearTimeout(hidetime); 
					return false;
				}
			}
	});
});


function setNav(elemid, change){
    clearTimeout(hidetime); 
    clearTimeout(time); 
	
	if(opened==""){
	//wenn aktiv gesetzt -> deaktivieren	
		//alert(active+" == "+elemid)
		if(active != "" && active == elemid){		
			$("#kopf").find("#"+active).fadeOut("fast");	
			if(iniactive != elemid){			
				$("#navigation").find("#"+active).removeClass('aktiv');		
				$("#subnav").find("#"+active).fadeOut("fast");	
			}
			active = "";		
		}
		else change=false;
		
	//wenn noch nicht aktiv gesetzt -> aktivieren
	//$("#kopf").children().fadeOut("fast");
	
		if(change==false && iniactive != elemid){
			var kopf = $("#kopf").find("#"+elemid);
			var nav = $("#subnav").find("#"+elemid);
			//alert(elemid+" != "+iniactive);
			if(elemid!="" && elemid != iniactive) $("#kopf").children().fadeOut("fast");
			
			$("#navigation").find("#"+elemid).addClass('aktiv');		
			kopf.fadeIn("slow");				
			nav.fadeIn("slow");
			
			active = elemid;					
		}
		else if(iniactive != ""){
     		hovertime = setTimeout(function() {
				$("#navigation *").removeClass('aktiv');	
				if(elemid!="" && elemid != iniactive)  $("#kopf").children().fadeOut("fast");
				
				$("#navigation").find("#"+iniactive).addClass('aktiv');	
				$("#subnav").find("#"+iniactive).fadeIn("slow");
				if($("#kopf #nav0").contents().length>0) $("#kopf #nav0").fadeIn("slow");
				else $("#kopf").find("#"+iniactive).fadeIn("slow");
				active = iniactive;	
			}, 500); 
		}
	}
}
