var doKvInterval = true;

$(document).ready(function(){
  
  $.fn.superbgimage.options = {};
  $('#superbgimage').superbgimage().hide();
  
  $('a[rel="gallery"]').colorbox({
    previous: 'zurück',
    next: 'weiter',
    current: 'Bild {current} von {total}',
    close: 'schließen'
  });
  $('a.openGallery').click(function() {
    $(this).siblings('a[rel="gallery"]').click();
  });


  $('input').focus(function(){
    $(this).select();
  });
  
  $('input.search-term').change(function(){
    if ($(this).val() == '') {
      $(this).val('Ihr Suchbegriff...');
    }
  });
  
  jQuery('#b_availSubmit input').click(function() {
    jQuery(this).parents('form').attr('target', '_blank');
  });


  $('#top-teaser h3 a').click(function(){
    if (!$(this).parent('h3').hasClass('active')) {
      $('#top-teaser h3.active').siblings('div').slideUp();
      $('#top-teaser h3.active').removeClass('active');
      $(this).parent('h3').siblings('div').slideDown();
      $(this).parent('h3').addClass('active');
      return false;
    }
  });
  
  
  var countKeyvisuals = $('#keyvisuals img').length;
  if (countKeyvisuals > 1) {
    var kvInterval = window.setInterval(function(){
      if (doKvInterval) {
        var currentNr = parseInt($('#keyvisuals').children('img:visible').attr('rel'));
        var nextNr = 0;
    
        if (currentNr == countKeyvisuals){
          nextNr = 1;
        } else {
          nextNr = currentNr + 1;
        }
   
        $('#keyvisuals img.keyvisual-' + currentNr).fadeOut('medium', function(){
          $('#keyvisuals img.keyvisual-' + nextNr).fadeIn('medium');
          
          $('#keyvisualNavigation li[rel="' + currentNr + '"]').removeClass('active');
          $('#keyvisualNavigation li[rel="' + nextNr + '"]').addClass('active');
        });
      }
    }, 8000);
  }
  
  $('#keyvisualNavigation li').click(function(){
    doKvInterval = false;
    var nextNr = $(this).attr('rel');
    var currentNr = parseInt($('#keyvisuals img:visible').attr('rel'));
    
    
    if (nextNr == 'next') {
      if (currentNr == countKeyvisuals){
        nextNr = 1;
      } else {
        nextNr = currentNr + 1;
      }
    } else if(nextNr == 'back') {
      if (currentNr == 1){
        nextNr = countKeyvisuals;
      } else {
        nextNr = currentNr - 1;
      }
    } else {
      nextNr = parseInt(nextNr);
    }
      
    $('#keyvisuals').children('img:visible').fadeOut('medium', function(){
      $('#keyvisuals img.keyvisual-' + nextNr).fadeIn('medium');
      
      $('#keyvisualNavigation li[rel="' + currentNr + '"]').removeClass('active');
      $('#keyvisualNavigation li[rel="' + nextNr + '"]').addClass('active');
    });
  });
  
  
  
});

function scrollTo(object) {
  var objectOffset = object.offset().top;
  jQuery('html, body').animate({
    scrollTop: objectOffset
  }, 500);
} 
