function scrollTo(selector) {
  if (typeof(selector) == 'object') {
    var targetOffset = selector.offset().top;
    $('html, body').animate({
      scrollTop: targetOffset
    }, 500);
  }
}

// topnavi
var naviTeaserRelation = "";
function executeNaviTeaserHide(){
  $('#naviTeaser').slideUp('fast');
  naviTeaserRelation = "";
}


$(document).ready(function() {
  // Bildrefektion bei Teasern
  $("img.reflect").reflect({
    height:0.17,
    opacity:0.33
  });

  $('body').find('input[type="text"]:not(.date-pick):not(.ignore), textarea').each(function() {
    $(this).focus(function(){
      if(!$(this).attr("rel"))$(this).attr("rel", $(this).val());
      if($(this).val() == $(this).attr("rel")){
        $(this).val('');
      }
    });
    $(this).blur(function(){
      if($(this).val() == '')$(this).val($(this).attr("rel"));
    });
  });
});

// Teaser-Karusell Rubrikenstartseite 
$(function(){
  $("div.carousel").carousel({
    pagination: true,
    effect: "fade",
    animSpeed: 700,
    loop: true,
    autoSlide: true,
    autoSlideInterval: 9000
  });
			
// die einzelnen bereiche koennnen einzeln angesprochen werden:
			
//$("div.gallery-1").carousel({ 
//	pagination: true,
//	effect: "fade",
//	animSpeed: 700,
//	loop: true,
//	autoSlide: true, 
//	autoSlideInterval: 9000
//});	
//$("div.gallery-2").carousel({ 
//	pagination: true,
//	effect: "fade",
//	animSpeed: 700,
//	loop: true,
//	autoSlide: true, 
//	autoSlideInterval: 9000
//});	
//$("div.gallery-3").carousel({ 
//	pagination: true,
//	effect: "fade",
//	animSpeed: 700,
//	loop: true,
//	autoSlide: true, 
//	autoSlideInterval: 9000
//});
//$("div.gallery-4").carousel({ 
//	pagination: true,
//	effect: "fade",
//	animSpeed: 700,
//	loop: true,
//	autoSlide: true, 
//	autoSlideInterval: 9000
//});
			
});


$(document).ready(function(){
  // Auswahl finder oeffnen/schliessen
  $("fieldset.complete a").click(function(){
    if ($(this).next().is(":hidden")) {
      $(this).next().slideDown("fast");
      $(this).text('Auswahl schließen').removeClass('open').addClass('close');
    } else {
      $(this).next().slideUp("fast");
      $(this).text('Auswahl öffnen').removeClass('close').addClass('open');
    }
  });

			   
			   							
  // metanavi
  $("ul#metatop li:last").css('border-style','none');
  $("#metabottom ul li:last").css('border-style','none');
	
  // teaser events
  $("ul.list li:first").css('border-top-style','none');
  $("ul.list li:last").css('border-bottom-style','none');
	
		
  // fahert sprachen aus/ein
  $('ul#metatop li.lang').mouseenter(function () {
    if ($("ul#lang").is(":hidden")) {
      $("ul#lang").slideDown("normal");
    }
  });
  $('ul#metatop li.lang').mouseleave(function () {
    if ($("ul#lang").is(":visible")) {
      $("ul#lang").slideUp("fast");
    }
  });
		
		
  // topnavi
  $("ul#naviTop li:last").css('background-image','none');
  $('ul#naviTop li').mouseenter(function () {
    $(this).prev().css('background-image','none');
  });
  $('ul#naviTop li').mouseleave(function () {
    $(this).prev().css('background-image','url(img/objects/separator_navi.gif)');
  })


  //Animation der NavigationsTeaser
  $("#naviTop li").children("a[rel='hasTeaser']").bind("mouseenter",function(){
    if(naviTeaserRelation != "")window.clearTimeout(naviTeaserHide);
    if(naviTeaserRelation != $(this).attr("rev")){
      naviTeaserRelation = $(this).attr("rev");
      $("#naviTeaser").slideUp("fast", function(){
        $("#naviTeaser .body").children(".teaser").hide();
        $("#naviTeaser .body").children("."+ naviTeaserRelation).show();
        $("#naviTeaser").slideDown("slow",function(){
          //TODO: Abgerundete Ecken für die Teaserbilder sind dank des Sliders bisher nicht möglich. EVTL Corner Plugin wechseln.
          //$("#naviTeaser .filler .teaser .teaserImage").children(".imageHelper").corner("10px cc:white");
          });
      });
    }
  });
  $("#naviTeaser").bind("mouseenter",function(){
    window.clearTimeout(naviTeaserHide);
  });
  $("#naviTeaser").bind("mouseleave",function(){
    naviTeaserHide = window.setTimeout("executeNaviTeaserHide()", 2000);
  });
  $("#naviTop li").children("a[rel='hasTeaser']").bind("mouseleave",function(){
    naviTeaserHide = window.setTimeout("executeNaviTeaserHide()", 2000);
  });
	
	
  // service-teaser
  $("#service div.service-teaser:last").css('margin-right','0px')
	

  //$("#service div.service-teaser .low").toggle(
  //  function () {
  //    $(this).parent().children('div:last').animate(  { top:"152px" }, 500  );
  //  },
  // function () {
  //    $(this).parent().children('div:last').animate(  { top:"335px" }, 500  );
  //  }
  //);
	
	
  //$("#service div.service-teaser .high").toggle(
  // function () {
  //    $(this).parent().children('div:last').animate(  { top:"0" }, 500  );
  //  },
  //  function () {
  //    $(this).parent().children('div:last').animate(  { top:"335px" }, 500  );
  //  }
  //);

  var openedTeasers = new Object();
  openedTeasers['facebook'] = 0;
  openedTeasers['twitter'] = 0;
  openedTeasers['flickr'] = 0;
  openedTeasers['news'] = 0;

  // oeffnet service-teaser
  $("#service div.service-teaser .low").click(function() {
    var that = $(this).parent().children('div:last');
    var className = $(this).children('h3').attr('class');

    if (openedTeasers[className] == 0) {
      that.animate({
        top: '192px'
      }, 500);
      openedTeasers[className] = 1;
    } else {
      that.animate({
        top: '435px'
      }, 500);
      openedTeasers[className] = 0;
    }
  });
  $("#service div.service-teaser .high").click(function() {
    var that = $(this).parent().children('div:last');
    var className = $(this).children('h3').attr('class');

    if (openedTeasers[className] == 0) {
      that.animate({
        top: '0'
      }, 500);
      if (className == 'twitter' || className == 'news') {
        $('#tagcloud').fadeOut();
      }
      openedTeasers[className] = 1;
    } else {
      that.animate({
        top: '435px'
      }, 500);
      openedTeasers[className] = 0;
      
      if (className == 'twitter' || className == 'news') {
        if (openedTeasers['twitter'] == 0 && openedTeasers['news'] == 0) {
          $('#tagcloud').fadeIn();
        }
      }
    }
  });
  // schliesst service-teaser
  $("#service div.service-teaser div.content a.close").click(function() {
    var that = $(this).parent();
    var className = that.attr('id');

    that.animate({
      top: '435px'
    }, 500);
    openedTeasers[className] = 0;

    if (className == 'twitter' || className == 'news') {
      if (openedTeasers['twitter'] == 0 && openedTeasers['news'] == 0) {
        $('#tagcloud').fadeIn();
      }
    }
  });


  // reiter tab-navigation
  //$('#rubrik_items').tabs({
  //	remote: true,
  //	fxFade: true,
  //	fxSpeed: 'fast'
  //});
  $('#rubrik_items').tabs({
    fxFade: true,
    fxSpeed: 'fast'
  });
  $('#content_gallery').tabs({
    fxFade: true,
    fxSpeed: 'fast'
  });
  $('#results_gallery').tabs({
    fxFade: true,
    fxSpeed: 'fast'
  });

	
	
  // reiter teaser schnellsuche
  $('#bookings').tabs({
    fxFade: true,
    fxSpeed: 'fast'
  });

  var dateTomorrow = new Date();
  var datePlusOneWeek = new Date().addDays(7);
  var datePlusOneYear = new Date().addYears(1);

  //datePicker
  var startDate = $('input[id^="start-date"]');
  var endDate = $('input[id^="end-date"]:not([id^="end-date-event"])');
  var endDateEvent = $('input[id^="end-date-event"]');

  startDate.datePicker({
    clickInput: true,
    createButton: false
  }) // apply datePicker to field
  startDate.each(function() { // fill empty fields (for which no data was posted)
    if($(this).val() == '') $(this).val(dateTomorrow.asString()).trigger('change');
  });

  endDate.datePicker({
    clickInput: true,
    createButton: false
  }) // apply datePicker to field
  endDate.each(function() { // fill empty fields (for which no data was posted)
    if($(this).val() == '') $(this).val(datePlusOneWeek.asString()).trigger('change');
  });

  endDateEvent.datePicker({
    clickInput: true,
    createButton: false
  }) // apply datePicker to field
  endDateEvent.each(function() { // fill empty fields (for which no data was posted)
    if($(this).val() == '') $(this).val(datePlusOneYear.asString()).trigger('change');
  });

  //start- u. end-datum
  $('input[id^="start-date"]').bind('dpClosed', function(e, selectedDates) {
    var d = selectedDates[0];
    var endDate = $('input[id^="end-date"]').dpGetSelected();
    if (d) {
      d = new Date(d);
      $('input[id^="end-date"]').dpSetStartDate(d.addDays(1).asString());
      if (d > endDate) {
        $('input[id^="end-date"]').val(d.addDays(7).asString()).trigger('change');
      }
    }
  });
  $('input[id^="end-date"]').bind('dpClosed',	function(e, selectedDates)  {
    var d = selectedDates[0];
    if (d) {
      d = new Date(d);
      $('input[id^="start-date"]').dpSetEndDate(d.addDays(-1).asString());
    }
  });

	
  // bildergalerie
  $("a.single").colorbox({
    transition:"elastic"
  });
  // einzelnes Bild
  $("a[rel='gallery']").colorbox({
    transition:"elastic"
  });
  // Scribd
  $("a[rel='scribd']").colorbox({
    transition: "elastic",
    iframe: true,
    width: '985px',
    height: '85%'
  });
  // DB Widget
  $("a[rel='dbWidget']").colorbox({
    transition: "elastic",
    iframe: true,
    width: '500px',
    height: '365px'
  });
  // Bildergalerie
  $("a.clipboard").colorbox({
    transition:"elastic",
    width:"560px",
    height:"605px"
  });
  // Merkliste
  $("a.request").colorbox({
    transition:"elastic",
    width:"660px",
    height:"605px"
  });
  // Angebote anfragen
  $("a.form").colorbox({
    transition:"elastic",
    iframe: true,
    width:"600px",
    height:"605px"
  });
  //// Formular
  //$("a.iframe").colorbox({width:"80%", height:"80%", iframe:true});
  //$(".youtube").colorbox({iframe:true, width:650, height:550});


  // Suchformulare oeffnen/schliessen
  $("#form_opener a").click(function(){
    if ($("div.form_block").is(":hidden")) {
      $("div.form_block").slideDown("slow");
      $('#form_opener span').text('Suche schließen').removeClass('closed').addClass('open');
    } else {
      $("div.form_block").slideUp("slow");
      $('#form_opener span').text('Suche öffnen').removeClass('open').addClass('closed');
    }

  });
	
	
  // hotel oeffnen/schliessen
  $("#selectHotel").click(function () {
    if ($("form#pension").is(":visible")) {
      $("form#pension").hide("fast").removeClass("checked");
      $('label.pension').removeClass("checked");
    }
    if ($("form#fewo").is(":visible")) {
      $("form#fewo").hide("fast").removeClass("checked");
      $('label.fewo').removeClass("checked");
    }
    if ($("form#hotel").is(":hidden")) {
      $("form#hotel").show("slow").addClass("checked");
      $('label.hotel').addClass("checked");
    }
  });
  // pensionen oeffnen/schliessen
  $("#selectPension").click(function () {
    if ($("form#hotel").is(":visible")) {
      $("form#hotel").hide("fast");
      $('label.hotel').removeClass("checked");
    }
    if ($("form#fewo").is(":visible")) {
      $("form#fewo").hide("fast");
      $('label.fewo').removeClass("checked");
    }
    if ($("form#pension").is(":hidden")) {
      $("form#pension").show("slow");
      $('label.pension').addClass("checked");
    }
  });
  // fewo oeffnen/schliessen
  $("#selectFewo").click(function () {
    if ($("form#pension").is(":visible")) {
      $("form#pension").hide("fast").removeClass("checked");
      $('label.pension').removeClass("checked");
    }
    if ($("form#hotel").is(":visible")) {
      $("form#hotel").hide("fast").removeClass("checked");
      $('label.hotel').removeClass("checked");
    }
    if ($("form#fewo").is(":hidden")) {
      $("form#fewo").show("slow").addClass("checked");
      $('label.fewo').addClass("checked");
    }
  });
	
	
  // faehrt zielgruppenblock bei infos anfordern ein/aus
  $("form#info #quickBookingForm_subscription_1, form#info #quickBookingFormAddress_subscription_1").click(function(){
    $('div.newsletter').slideToggle();
  });
	
	
  // faehrt anschriftenblock bei infos anfordern ein/aus
  $("form#info #quickBookingForm_infos_1, form#info #quickBookingFormAddress_infos_1").click(function(){
    $(this).parent().toggleClass('last');
    $('fieldset.contact').slideToggle();
  });
	
	
  // faehrt zielgruppenblock bei kontaktformular ein/aus
  $("#booking_request #news").click(function(){
    $('div.newsblock').slideToggle();
  });
	
	
  // faehrt kontaktformular bei katalogbestellung aus
  $("div.default_button a.openKatalog").click(function(){
    $('div.formKatalog').slideDown('slow');
    $('div.default_button a.openKatalog').fadeOut();
  });
  // faehrt kontaktformular bei gewinnspiel aus
  $("div#result a.openGewinnspiel").click(function(){
    $('div.formGewinnspiel').slideDown('slow');
    $('div#result a.openGewinnspiel').fadeOut();
  });

	
});

/**
 * Handle checkboxes in finders
 */
function handleFinderCheckboxes(par1, par2) {
  var numberChecked = $('#formContainer input[name^=' + par1 + ']:checked').length;
  if (numberChecked > 0) {
    $('#formContainer input[name=' + par2 + ']').removeAttr('checked');
  } else {
    $('#formContainer input[name=' + par2 + ']').attr('checked', 'checked');
  }
}

/**
 * Handle checkboxes in finders
 */
function handleFinderSingleCheckbox (par1, par2) {
  var countPar1 = $('#formContainer input[name=' + par1 + ']:checked').length;

  if (countPar1 == 1) {
    $('#formContainer input[name^=' + par2 + ']').removeAttr('checked');
  } else {
    $('#formContainer input[name^=' + par2 + ']').attr('checked', 'checked');
  }

  $('#formContainer input[name=' + par1 + ']').siblings('a').trigger('click');
}

/**
 * Handle checkboxes in finders
 */
function handleFinderCheckboxesAddressbase(par1, par2) {
  var numberChecked = $('#formContainer input[name="' + par1 + '[]"]:checked').length;
  if (numberChecked > 0) {
    $('#formContainer input[name="' + par2 + '[]"]').removeAttr('checked');
  } else {
    $('#formContainer input[name="' + par2 + '[]"]').attr('checked', 'checked');
  }
}

function setSelectsToDefault(first, second) {
  var defaultVal = first.find('option').eq(0).val();
  var otherDefaultVal = second.find('option').eq(0).val();

  first.removeClass('disabled');

  if (first.val() != defaultVal) {
    second.val(otherDefaultVal);
    second.addClass('disabled');
  } else {
    second.removeClass('disabled');
  }
}