$(document).ready(function() {
    $('.sprachen,li.navi').show();
    $('.gastform').hide();
    $('#gastclick').show().click(function(){
      $(this).hide();
      $('.gastform').slideDown();
    })
    $('a[rel*=facebox]').facebox();
    $('.menu ul li').hide();
    $('.eye').mouseenter(function(){$("#content,#content a[rel=facebox]").fadeTo('slow',.25);$(this).fadeTo('fast',1);});
    $('.eye').mouseleave(function(){$("#content,#content a[rel=facebox]").fadeTo('slow',1,function(){if($.browser.msie)this.style.removeAttribute('filter')});$(this).fadeTo('fast',.4);});
    $(".sprachen").mouseleave(function(){
      $(".eye").animate({marginLeft:0});
      $(".lang").not(".selected").animate({width: 'hide',paddingLeft:0,paddingRight:0},function(){
        $(".lang").data("status","");
        $(".sprachen li.selected").fadeTo('fast',1);
      });
    });
    $(".lang").mouseenter(function(){
      $(".eye").animate({marginLeft:"27px"});
      $(".lang").not(".selected").animate({width:'show',paddingLeft:"20px",paddingRight:"10px"},function(){
        $(".lang").css("width","auto").data("status","open");
      });
      if($(".lang").data("status")!="open")
        $(".lang").fadeTo('fast',.4);
      else
        $(".lang").stop(true,false).css({width:'auto',paddingLeft:"20px",paddingRight:"10px"}).fadeTo('fast',.4);
      $(this).fadeTo('fast',1);
    });
    $(".lang").mouseleave(function(){
      if($(".lang").data("status")!="open")
        $(".lang").fadeTo('fast',.4);
      else
        $(".lang").stop(true,false).css({width:'auto',paddingLeft:"20px",paddingRight:"10px"}).fadeTo('fast',.4);
    });
    $('li.link').click(function(){
      if($(this).hasClass("navi")) {
        menuflip();
      } else {
        if(!$(this).find("ul li").is(":visible")) {
          $(".menu ul li").slideUp("Fast");
          $(this).find("ul li").slideDown();
        }
        clearTimeout(cwf);
        cwf=setTimeout("menuflip()",40000);
      }
    });
    staticNav(); //Execute function on load
    $('#content .list').masonry({ singleMode: true });
    $('.menu a.selected').parent().show();
    $('#content a').css("border-color",$('.menu a.selected').parent().parent().css("backgroundColor"));
    cwf=setTimeout("menuflip()",40000);
    $(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
        staticNav();
    });
});

    function menuflip() {
        if($(".menu").is(":visible")) {
          layot=".menu";
          layin=".adresse";
          if($(".sprachen").css("position")!="fixed"){layot=layot+",.sprachen";layin=layin+",.sprachen";}
          $(layot).animate({left:'-320'},500,function(){$(".menu").hide();$(layin).css("left",-320).show().animate({left:'0'},500)});
        } else {
          layot=".adresse";
          layin=".menu";
          if($(".sprachen").css("position")!="fixed"){layot=layot+",.sprachen";layin=layin+",.sprachen";}
          $(layot).animate({left:'-320'},500,function(){$(".adresse").hide();$(layin).css("left",-320).show().animate({left:'0'},500)});
        }
        clearTimeout(cwf);
        cwf=setTimeout("menuflip()",40000);
    }
    function textfade(mode) {
        if(mode=="in") {
          $("#content").fadeTo('slow',.25);
        } else {
          $("#content").fadeTo('slow',1);
        }
    }
    function staticNav() {
        var sidenavHeight = $("#sidenav").height(); //Get height of sidenav
        if(sidenavHeight<450) sidenavHeight=450;
        var winWidth  = $(window).width();  //Get width of viewport
        var winHeight = $(window).height(); //Get height of viewport
        var browserIE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false; //Check for IE6
        var browserIE7 = (navigator.userAgent.indexOf("MSIE 6")>=0 || navigator.userAgent.indexOf("MSIE 7")>=0) ? true : false; //Check for IE7

        if((winWidth-$("#sidenav").outerWidth(true))/2>=320) {
          $("#content").width(620);
          $(".galerie").width("auto").css("padding-left","5px");
          $(".max").width(580);
          $(".max img").css("max-width","580px");
        } else {
          $("#content").width(320);
          $(".galerie").width(255).css("padding-left","25px");
          $(".max").width(275);
          $(".max img").css("max-width","275px");
        }
/*
        if (browserIE6) { //if IE6...
            $("#sidenav,.sprachen").css({'position' : 'absolute'});  //reset the sidenav to be absolute
        } else { //if not IE6...
            $("#sidenav,.sprachen").css({'position' : 'fixed'}); //reset the sidenav to be fixed
        }
        if (browserIE7) { //if IE7...
          $("#sidenav,.sprachen").css({'position' : 'fixed'});
          $(".sprachen").css({'position':'fixed','bottom':0,'left':'30px','width':'260px'});
        }
*/
        if (sidenavHeight>winHeight) { //If sidenav is taller than viewport...
          $("#sidenav").css({'position':'absolute'});
          $(".sprachen").css({'position':'relative','left':0}); //switch the fixed positioning to static. Say good bye to sticky nav!
        } else {
          $("#sidenav,.sprachen").css({'position' : 'fixed'});
          $(".sprachen").css({'position':'fixed','bottom':0,'left':'30px','width':'auto'});
          if (browserIE7) { //if IE7...
            $(".sprachen").css({'width':'260px'});
          } else if (browserIE6) { //If sidenav is taller than viewport...
            $("#sidenav").css({'position':'absolute'});
            $(".sprachen").css({'position':'relative','left':0}); //switch the fixed positioning to static. Say good bye to sticky nav!
          }
        }
    }
