$(document).ready(function(){

  setCalculatedContentHeight();
  checkInputFields();
  
  // Browser switch for opera.
  if ($.browser.opera){
    $('head').append('<style type="text/css">@import "lib/styles/browser/opera.css";</style>;');
  }

  // Colorbox call, with the triggers of the openImage Buttons.
  $('a[rel=colorbox]').colorbox({
    opacity: 0.6,
    current: '{current} von {total}',
    previous: 'Zurück',
    next: 'Weiter',
    close: ''
  });

  jQuery('li.map a').colorbox({
    innerHeight: 715,
    initialHeight: 715,
    innerWidth: 700,
    initialWidth: 700,
    close: '',
    opacity: 0.6,
    onComplete: function() {
      if(typeof(mapOverlay) === 'object') {
        mapOverlay.checkResize();
        mapOverlay.setCenter(new GLatLng(51.430724, 9.246826), 6);
      }
    }
  });
  
  $('#snowheightsTable a.snowheightDetail').colorbox({
    height: '780',
    width: '820',
    initialHeight: 780,
    initialWidth: 820,
    close: '',
    opacity: 0.6,
    onComplete: function() {
      $('div.ovWebcams img').mouseover(function(){
        var nr = parseInt($(this).attr('class').substr(6,7));
        $(this).parent().parent('div').children('img.webcam' + nr).show();
      }).mouseout(function(){
        var nr = parseInt($(this).attr('class').substr(6,7));
        $(this).parent().parent('div').children('img.webcam' + nr).hide();
      });
    }
  });

  // Triggers the colorbox call, to have no picture twice in the imageGallery.
  $('a.colorboxMainTrigger').click(function(){
    $('a.colorboxMain').trigger('click');
  });

  $('a.colorboxPrevTrigger').click(function(){
    var count = $(this).attr('class').substr(20,1);
    $('a.colorboxCount' + count).trigger('click');
  });

  // Call for the teaser carousel on the index-page.
  $('#teaserSlide').jCarouselLite({
    btnNext: '.sliderRight',
    btnPrev: '.sliderLeft'
  });

  // Tab changing in the cablecar search.
  $('a.mapOpen').click(function(){
    if (!$(this).hasClass('active')){
      $(this).addClass('active');
      $('a.listOpen').removeClass('active');
      $('#tabListWrapper').hide();
      if ($('#tabMapWrapper').hasClass('positionHide')){
        $('#tabMapWrapper').removeClass('positionHide');
      }
      $('#contentLeftInnerWrapper').height(10);
      setCalculatedContentHeight();
    }
  });
  $('a.listOpen').click(function(){
    if (!$(this).hasClass('active')){
      $(this).addClass('active');
      $('a.mapOpen').removeClass('active');
      $('#tabListWrapper').show();
      if (!$('#tabMapWrapper').hasClass('positionHide')){
        $('#tabMapWrapper').addClass('positionHide');
      }
      setCalculatedContentHeight();
    }
  });

  // Show and hide endowments in cablecar search.
  $('a.endowment').click(function(){
    if($('div.checkboxCont').is(':hidden')){
      $('div.checkboxCont').slideDown('slow');
      $(this).removeClass('closed');
    } else {
      $('div.checkboxCont').slideUp('slow');
      $(this).addClass('closed');
    }
  });

  //main navi
  $('#mainnavi li').mouseenter(function(){
    $(this).children('ul').removeClass('hide');
  });
  $('#mainnavi li').mouseleave(function(){
    $(this).children('ul').addClass('hide');
  });

  var numberKeyvisuals = $('#kv_wrapper').children().size();
  if (numberKeyvisuals > 1){
    var keyvisualInterval = window.setInterval(function(){
      var currentKV = parseInt($('#kv_wrapper').children('img:visible').attr('id').substr(3,4));
      var nextKV;
      (currentKV + 1 > numberKeyvisuals) ? nextKV = 1 : nextKV = currentKV + 1;
      $('#kv_wrapper').children('#kv_' + currentKV).fadeOut(600, function(){
        $('#kv_wrapper').children('#kv_' + nextKV).fadeIn(600);
      });

      $('#kv_title_' + currentKV).hide();
      $('#kv_title_' + nextKV).show();
    }, 9000);
  }

  $('div.cablecarPagination a').click(function(){
    if ($(this).hasClass('back')|| $(this).hasClass('forwards')){
      if ($(this).hasClass('clickable')){
        var listpos = parseInt($(this).attr('id'));
      }
    } else {
      if (!$(this).hasClass('active')){
        var value = $(this).html();
        listpos = parseInt(value);
        listpos = listpos - 1;
      }
    }

    if (listpos || listpos === 0) {
      var hiddenListpos = '<input name="listpos" type="hidden" value="' + listpos + '" />';
      $('#formCablecar').append(hiddenListpos);
      var hiddenStayOnList = '<input name="stayOnList" type="hidden" value="true" />';
      $('#formCablecar').append(hiddenStayOnList);
      $('#cablecarSubmit').click();
    }
  });
});

