/*
  $("ul#headernavi > li").hover(
    function() {
	  $(this).next().find("a").addClass("hovernext")
    },
    function () {
      $(this).next().find("a").removeClass("hovernext")
    }
  );
*/
function nachAktiven() {
  $("ul#headernavi li.active").next().find("a").stop().addClass("actnext");
}

$(document).ready(function() {
	
  /*
  var i = 0;
  //a's maximieren
  $('ul#headernavi .sub ul').each(function() {
    hoeheDavor = $(this).height();
	for(weite = $(this).width(); weite <= 400; weite +=5) {
		$(this).width(weite);
		if($(this).height() < hoeheDavor) {
			$(this).width(weite);
			weite = 600;
		}
	}
	if ($(this).height()==hoeheDavor) {
		$(this).height(hoeheDavor);
	}
    if(i<=3) {
    alert(hoeheDavor);
	i++;
	}
  });
  */
  


  function megaHoverOver(){
      $(this).next().children("a.actnext").stop().removeClass("actnext");
	  $(this).next().children("a").stop().addClass("hovernext");
	  $(this).children("a").stop().addClass("hover");
  	  

	  $(this).find(".sub").stop().fadeTo('fast', 1).show();
		  
	  //Calculate width of all ul's
	  (function($) { 
		  jQuery.fn.calcSubWidth = function() {
			  rowWidth = 0;
			  //Calculate row
			  $(this).find("ul").each(function() {					
				  rowWidth += $(this).width(); 
			  });	
		  };
	  })(jQuery); 
	  



	  
	  if ( $(this).find(".row").length > 0 ) { //If row exists...
		  var biggestRow = 0;	
		  //Calculate each row
		  $(this).find(".row").each(function() {							   
			  $(this).calcSubWidth();
			  //Find biggest row
			  if(rowWidth > biggestRow) {
				  biggestRow = rowWidth;
			  }
		  });
		  //Set width
		  $(this).find(".sub").css({'width' :biggestRow});
		  $(this).find(".row:last").css({'margin':'0'});
		  
	  } else { //If row does not exist...
		  
		  $(this).calcSubWidth();
		  //Set Width
		  $(this).find(".sub").css({'width' : rowWidth});
		  
	  }
  }
  
  function megaHoverOut(){ 
    $(this).next().find("a").stop().removeClass("hovernext");
	$(this).find("a").stop().removeClass("hover");
	var checkme = $(this).hasClass("active");
	if(checkme){
		$(this).next().find("a").stop().addClass("actnext");
	}
	$(this).find(".sub").stop().fadeTo('fast', 0, function() {
		$(this).hide(); 
		
	});
	

  }

  var config = {    
	   sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
	   interval: 0, // number = milliseconds for onMouseOver polling interval    
	   over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
	   timeout: 0, // number = milliseconds delay before onMouseOut    
	   out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
  };
  $.fn.addAbdecker = function() {
	  $(this).each(function(index) {
        var liWidth = $(this).parents('li').width() + 5;
		if ($(this).parents('li.last').length) {
			liWidth -=7;
		}
	    return $(this).width(liWidth).height(1);
      });
  }

//headernavi
  $(".headernavi-abdecker").addAbdecker();
  $("ul#headernavi > li .sub").css({'opacity':'0'});
  $("ul#headernavi > li").hoverIntent(config);
//mainnavi
  $("ul#topnav li .sub").css({'opacity':'0'});
  $("ul#topnav li").hoverIntent(config);
});
