var $j = jQuery.noConflict();

sfHover = function() {
	var sfEls = document.getElementById("nav_main").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


auswahlHover = function() {
	var auswahlEls = document.getElementById("auswahl_wrapper").getElementsByTagName("LI");
	for (var i=0; i<auswahlEls.length; i++) {
		auswahlEls[i].onmouseover=function() {
			this.className+=" auswahlhover";
		}
		auswahlEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" auswahlhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", auswahlHover);



$j(document).ready(function(){

// KREISANIMATION
    
    $j(".showhidelevel2").click(function () {
     if ($j(".showlevel2").is(":hidden")) {
        $j(".showlevel2").slideDown("slow");
      } else {
       $j(".showlevel2").slideUp("slow");
      }
     });        
});

$j(function(){
	$j("#jquery-test").html("jQuery is loaded");
});


// Runs when the DOM has been loaded
$j(document).ready(function() {

	// Check if map exists
	if($j('#map')) {
		// Loop through each AREA in the imagemap
		$j('#map area').each(function() {
	
			// Assigning an action to the mouseover event
			$j(this).mouseover(function(e) {
				var kreis_id = $j(this).attr('id').replace('area_', '');
				$j('#'+kreis_id).fadeIn('fast');
			});
			
			// Assigning an action to the mouseout event
			$j(this).mouseout(function(e) {
				var kreis_id = $j(this).attr('id').replace('area_', '');
				$j('#'+kreis_id).fadeOut('fast');
			});
			
			// Assigning an action to the click event
			$j(this).click(function(e) {

			});
		
		});
	}
	
// MEGADROPDOWN	
	
	function megaHoverOver(){
		$j(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($j) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$j(this).find("ul").each(function() {					
					rowWidth += $j(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $j(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$j(this).find(".row").each(function() {							   
				$j(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$j(this).find(".sub").css({'width' :biggestRow});
			$j(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$j(this).calcSubWidth();
			//Set Width
			$j(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	 $j(this).find(".sub").stop().fadeTo('fast', 0, function() {
		 $j(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$j("ul#topnav li .sub").css({'opacity':'0'});
	$j("ul#topnav li").hoverIntent(config);
	
	$j("#search ul li .sub").css({'opacity':'0'});
	$j("#search ul li").hoverIntent(config);
	
});

