function renderDatepicker(){

  Date.format = 'dd.mm.yyyy';
  $(".datepicker").datePicker({
    colorstyle: "default",
    createButton: false,
    clickInput:true,
    verticalOffset:25,
    horizontalPosition:$.dpConst.POS_RIGHT
  });
};


function openOverlayMap(){
  if(!$("#mapLargeArea").hasClass("active"))$("#mapLargeArea").css({"position":"static", "display":"none"}).slideDown("slow").addClass("active");
}

var closeKeyvisualTeaserTimeout = 0;
function closeKeyvisualTeaser(){

  $("#CONT_keyvisual").find(".vacationsearch").find("div.small").show();
  $("#CONT_keyvisual").find(".vacationsearch").find("div.large").hide();

  $("#CONT_keyvisual").find("div.keyvisualTeaser:not(.none)").fadeIn();
  $("#CONT_keyvisual").find("a.keyvisualTeaserOpener:not(.none)").fadeIn();
  $("#CONT_keyvisual").find("a.keyvisualTeaserCloser:not(.none)").fadeIn();

}


$(document).ready(function(){

    renderDatepicker();

    $(".datepicker.arrival").live("change", function(){
        var compareId = $(this).attr("id") + "Departure";
        $(this).compareDates("#"+compareId);
    });
    $(".datepicker.arrival").each(function(){
        var compareId = $(this).attr("id") + "Departure";
        $(this).compareDates("#"+compareId);
    });

    //Blur/Focus-Behavior of Formelements
    $(document).inputPresets();

    //Add a colorbox for imagegallery
    $("a[rel='imagegallery']").colorbox({
        "next":"nächstes Bild",
        "previous":"vorheriges Bild",
        "current":"Bild {current} von {total}",
        "opacity":.75
    });

    //Add a colorbox for flickrImages on dashboard
    $("a[rel='dashboardFlickr']").colorbox({
        "next":"nächstes Bild",
        "previous":"vorheriges Bild",
        "current":"Bild {current} von {total}",
        "width":"600px",
        "opacity":.75
    });
    //Add a colorbox for youtubeVideos on dashboard
    $("a.dashboardYoutube").colorbox({
        "next":"nächstes Video",
        "previous":"vorheriges Video",
        "current":"Video {current} von {total}",
        "iframe":true,
        "width":"600px",
        "height":"400px",
        "opacity":.75
    });

    //Add a colorbox for webcams
    $("a.webcam").colorbox({
        "next":"nächste Webcam",
        "previous":"vorherige Webcam",
        "current":"Webcam {current} von {total}",
        "opacity":.75
    });

    //Add a colorbox for modal forms
    $("a[rel='modalForm']").colorbox({
        "iframe":true,
        "width":550,
        "height":600,
        "opacity":.75
    });

    //Add a colorbox for panoramas
    $("a.panoramaOverlay").colorbox({
        "iframe":true,
        "width":900,
        "height":600
    });

    //open/close overlaymap
    $("#mapLargeArea .header .close").click(function(){
        $(this).blur();
        if($("#mapLargeArea").hasClass("active"))$("#mapLargeArea").removeClass("active").slideUp("slow", function(){$(this).css({"position":"absolute", "display":"block"})});
    });
    $(".mapOpener").click(function(){
        $(this).blur();
        openOverlayMap();
    });


    //Hide show facebook comments on dashboard
    $(".dashboardColumn.facebook").find("a[rel=comments]").click(function(){
      $(this).blur().parents(".entry").children(".comments").slideToggle();
    });


    //Catchword Characterlisting
    $("#catchwordCharacters").find("a").click(function(){
      var currentCharacter = $(this).attr("rel");
      var characterTable = $("table.catchwordSearchTable");
      characterTable.show();
      var table = characterTable.children("tbody");
      var notTable = $("table.catchwordContentTable");
      table.find("tr:not(.catchwordCharacter" + currentCharacter + ")").hide();
      table.find("tr.catchwordCharacter" + currentCharacter).show();
      notTable.hide();
    });
    $(".catchwordSearchTable").find("a").click(function(){
      var currentCategory = $(this).attr("rel");
      var table = $("table.catchwordCategory" + currentCategory);
      var notTable = $("table.catchwordContentTable:not(.catchwordCategory" + currentCategory + ")");
      notTable.hide();
      table.show();
      var characterTable = $("table.catchwordSearchTable");
      characterTable.hide();
    });


    //Sitemap
    $("ul#sitemap").find("a").click(function(){
      $(this).parent().children("ul").slideToggle();
      $(this).toggleClass("open");
      $(this).blur();
    });
    $("a#sitemaplink").click(function(){
        if($(this).hasClass("open")){
            $("ul#sitemap").find("ul").slideUp();
            $("ul#sitemap").find("a").removeClass("open");
            $(this).children(".open").hide();
            $(this).children(".closed").show();
        }else{
          $("ul#sitemap").find("ul").slideDown();
          $("ul#sitemap").find("a").addClass("open");
          $(this).children(".open").show();
          $(this).children(".closed").hide();
        }
        $(this).toggleClass("open");
        $(this).blur();
    });


    //open/close mapnavigation
    $("#mapLargeArea").find(".mapNavi").find(".footer").find("a").click(function(){
        if($(this).hasClass("open")) $(this).parents(".mapNavi").children(".content").slideDown();
        else $(this).parents(".mapNavi").children(".content").slideUp();

        $(this).parent().children(".none").removeClass("none");
        $(this).addClass("none");
    });

    //open/close legendpoints in mapnavigation
    $("#mapLargeArea").find(".mapNavi").find(".content").find("a:not(.extern)").click(function(){
        $(this).blur();
        var listElement = $(this).parent();
        var linkElement = $(this);
        var checkboxElement = $(this).children("span.checkbox");

        listElement.toggleClass("active");

        if(listElement.find("li.active").length > 0){
          if(!listElement.hasClass("active")){
            linkElement.addClass("active");
            checkboxElement.addClass("active");
          }
        }else{
          linkElement.toggleClass("active");
          checkboxElement.toggleClass("active");
        }

        if(!linkElement.hasClass("single")) listElement.children("ul").slideToggle();          
    });


    //open/close sublists in tables
    $("table.commune").find("a[rel=sublist]").click(function(){
      $(this).blur();
      $(this).parent().children("div.sublist").slideToggle();
    });


    //Teaserflow for startpage teaser
    $(".startpageTeaserArea").find(".teaserFlow").find("a:not(.active)").click(function(){
        $(this).blur();
        newTarget = "layer" + $(this).attr("rel");
        $(this).parents(".inner").children(".content:not(." + newTarget + ")").fadeOut();
        $(this).parents(".inner").children("." + newTarget).fadeIn();
    });

    //Fadein/out of coloroverlay in rubricstart teaser
    $(".rubricTeaser").mouseenter(function(){ $(this).children(".image").find(".overlay").fadeIn();});
    $(".rubricTeaser").mouseleave(function(){ $(this).children(".image").find(".overlay").fadeOut();});


    //hide and show keyvisual teaser on keyvisualsearch mouseover
    $("#CONT_keyvisual").find(".vacationsearch").bind("mouseenter", function(){
      window.clearTimeout(closeKeyvisualTeaserTimeout);
      $(this).find("div.small").hide();
      $(this).find("div.large").show();

      $("#CONT_keyvisual").find("div.keyvisualTeaser:not(.none)").fadeOut();
      $("#CONT_keyvisual").find("a.keyvisualTeaserOpener:not(.none)").fadeOut();
      $("#CONT_keyvisual").find("a.keyvisualTeaserCloser:not(.none)").fadeOut();
    });
    $("#CONT_keyvisual").find(".vacationsearch").bind("mouseleave", function(){
        window.clearTimeout(closeKeyvisualTeaserTimeout);
        closeKeyvisualTeaserTimeout = window.setTimeout("closeKeyvisualTeaser()", 1000);
    });
    if($("#CONT_keyvisual").find(".vacationsearch").length > 0){
        $("#dp-popup").live("mouseenter", function(){
            window.clearTimeout(closeKeyvisualTeaserTimeout);
        });
    }


    //Switch KeyvisualTeaser Style between opened and closed
    $("#CONT_keyvisual").find("a.keyvisualTeaserOpener").click(function(){
        $(this).removeAttr("style");
        $(this).addClass("none").parent().find("a.keyvisualTeaserCloser").removeClass("none");
        $(this).parent().children("div.keyvisualTeaser.closed").animate({"height":"toggle","opacity":"toggle"}).addClass("none");
        $(this).parent().children("div.keyvisualTeaser.open").animate({"height":"toggle","opacity":"toggle"}).removeClass("none");
    });
    $("#CONT_keyvisual").find("a.keyvisualTeaserCloser").click(function(){
        $(this).removeAttr("style");
        $(this).addClass("none").parent().find("a.keyvisualTeaserOpener").removeClass("none");
        $(this).parent().children("div.keyvisualTeaser.closed").animate({"height":"toggle","opacity":"toggle"}).removeClass("none");
        $(this).parent().children("div.keyvisualTeaser.open").animate({"height":"toggle","opacity":"toggle"}).addClass("none");
    });

    //KeyvisualTeaser Teaserflow
    $("#CONT_keyvisual").find(".keyvisualTeaser").find(".teaserflow").find("a:not(.active)").live("click", function(){
        $(this).blur();
        currentRel = parseInt($(this).attr("rel"));
        currentActive = parseInt($(this).parents(".teaserflow").children("ul").find("a.active").attr("rel"));

        newRel = ($(this).hasClass("next") || $(this).hasClass("prev"))? currentRel + currentActive : currentRel;
        newTarget = "layer" + newRel;

        if($(this).parents(".keyvisualTeaser").children("." + newTarget).length > 0){
            $(this).parents(".teaserflow").find("a.active").removeClass("active");
            $(this).parents(".teaserflow").find("a[rel="+newRel+"]:not(.next)").addClass("active");

            $(this).parents(".keyvisualTeaser").children(".container:not(." + newTarget + ")").fadeOut();
            $(this).parents(".keyvisualTeaser").children("." + newTarget).fadeIn();
        }
    });

    //Keyvisual Fader
    var keyvisualInterval = window.setInterval(function(){
        var numberKeyvisuals = parseInt($("#CONT_keyvisual").find(".keyvisual.frontdoor").children("img:not(.logo)").length);
        //alert(numberKeyvisuals);
        if(numberKeyvisuals > 1){
            var currentKeyvisual = parseInt($("#CONT_keyvisual .keyvisual.frontdoor").children("img:visible:not(.logo)").attr("class").substr(9));
            var nextKeyvisual = currentKeyvisual + 1;

            if(nextKeyvisual >= numberKeyvisuals) nextKeyvisual = 0;
            $("#CONT_keyvisual .keyvisual.frontdoor").children("img.keysource"+currentKeyvisual).fadeOut(2000);
            $("#CONT_keyvisual").find(".keytext"+currentKeyvisual).fadeOut(2000);
            $("#CONT_keyvisual .keyvisual.frontdoor").children("img.keysource"+nextKeyvisual).fadeIn(2000);
            $("#CONT_keyvisual").find(".keytext"+nextKeyvisual).fadeIn(2000);
        }else window.clearInterval(keyvisualInterval);
    },10000);

});