// General Fade Menu
$(document).ready(function() {
  //this is needed because of event document-ready is fired before css is loaded in webkit browsers....
  setTimeout("on_dom_load_critical()",90);
});

function on_dom_load_critical(){
  
  $('.dropdown').hide();
  $('#navigation li').hover(function (e) {
    var identifier = e.currentTarget.id.substr(e.currentTarget.id.indexOf("_")+1,e.currentTarget.id.length);    

    //$('.dropdown').fadeOut("fast");
    $('.dropdown').hide();
    //$('#untermenu_'+identifier).fadeIn("fast");
    $('#untermenu_'+identifier).show();

    //setting spacer image to the length of the div
    $('#untermenu_'+identifier+' .level2').each(function(){
      $('#untermenu_'+identifier+' .l2 .line img').width($(this).width()) ;
      $('#untermenu_'+identifier+' .l3 .line img').width($(this).width()-15) ;
    })
    set_submeniu_position();
    
  });

  var max_w = 0;
  var margin_left = Math.round(($(window).width()- $(".constrain").width())/2);
  $('.level1 a').each(function(){
    if($(this).width()>max_w){
      $('#navigation .line img').width($(this).width()) ;
      max_w = $(this).width();
    }

  });

  //adjust level2 menu position to compensate for diferent languages
  set_submeniu_position();

  $('li.l2').hover(function (e) {
    var sub_iden = e.currentTarget.id.substr(e.currentTarget.id.indexOf("_")+1,e.currentTarget.id.length);
    //$('li.l3').fadeOut("fast");
    $('li.l3').hide();
    //$('li.untermenu_'+sub_iden+'').fadeIn("fast");
    $('li.untermenu_'+sub_iden+'').show();
  });
  //show inital meniu, if in level2
  if($('.dropdown  .aktiverLink').length > 0){
    $($($('.dropdown  .aktiverLink')[0]).parents()[2]).show();
    set_submeniu_position();

    setTimeout("$($($('.dropdown  .aktiverLink')[0]).parents()[2]).show()",300);
    setTimeout("set_submeniu_position()",300);
  }
}

//fix menues on rezise
$(window).resize(function() {
  set_submeniu_position();
});

function set_submeniu_position(){
  var max_w = 0;
  var margin_left = Math.round(($(window).width()- $(".constrain").width())/2);
  $('.level1 a').each(function(){
    if($(this).width()>max_w){
      $('#navigation .line img').width($(this).width()) ;
      max_w = $(this).width();
    }

  });
  $('.dropdown').css({
    left: max_w +margin_left +107 +"px"
  });
}