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 
  $('div.carousel').each(function() {
    var that = $(this);
    
    if(that.find('.gallery_item').length == 1) return;
    that.carousel({
      pagination: true,
      effect: "fade",
      animSpeed: 700,
      loop: true,
      autoSlide: true,
      autoSlideInterval: 9000,
      callback: function(step) {
        that.find('.carousel-pagination em').html(step + 1);
      }
    });
  });
});


$(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");
    }
  });


  // service-teaser
  $("#service div.service-teaser:last").css('margin-right','0px');

  var openedTeasers = new Object();
  openedTeasers['facebook'] = 0;
  openedTeasers['twitter'] = 0;
  openedTeasers['flickr'] = 0;
  openedTeasers['news'] = 0;
  openedTeasers['youtube'] = 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');

    openServiceTeaser(that, className, '192px');
  });
  $("#service div.service-teaser .high").click(function() {
    var that = $(this).parent().children('div:last');
    var className = $(this).children('h3').attr('class');

    openServiceTeaser(that, className, '0');
    ET_Event.eventStart('ServiceTeaser', className, 'open', '');
  });

  // schliesst service-teaser bei click auf schließen button
  $("#service div.service-teaser div.content a.close").click(function() {
    var that = $(this).parent();
    var className = that.attr('id');

    closeServiceTeaser(that, className);
    ET_Event.eventStart('ServiceTeaser', className, 'close', '');
  });

  /**
   *Opens the service teaser
 * @param {jQuery} container the container to open
 * @param {String} className the name of the teaser (facebook, twitter, ...)
 * @param {String} top distance from the top ('0' for high teasers, '192px' for low)
   */
  openServiceTeaser = function (container, className, top) {
    container.animate({
      top: top
    }, 500);
    if (className == 'facebook' || className == 'twitter' || className == 'news') {
      $('#tagcloud, #footermap').fadeOut();
      if(className == 'news') $('#partner').fadeOut();
      
      if(className == 'facebook') {
        container.find('iframe').attr('src',facebookUrl);
      }
    }
    openedTeasers[className] = 1;
  }

  /**
 * Closes the service teaser
 * @param {jQuery} container the container to close
 * @param {String} className the name of the teaser (facebook, twitter, ...)
 */
  closeServiceTeaser = function (container, className) {
    container.animate({
      top: '435px'
    }, 500);
    openedTeasers[className] = 0;

    if (className == 'facebook' || className == 'twitter' || className == 'news') {
      if (openedTeasers['facebook'] == 0 && openedTeasers['twitter'] == 0 && openedTeasers['news'] == 0) {
        $('#tagcloud, #footermap').fadeIn();
        if(className == 'news') $('#partner').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');
  }
}
