$(document).ready(function() {


  $("label.help").tipTip({
    edgeOffset: 10,
    defaultPosition: 'right',
    maxWidth: '250px'
  });


  $('#nav ul li').hoverIntent({
    over: function() { $(this).find('ul').slideDown(); }, 
    timeout: 750,
    out: function() { $(this).find('ul').slideUp(); }
  });
  
  $('#nav ul li ul').hide();





  $("a.inline").colorbox({
    inline: true,
    href: function() {
      return $(this).attr('href');
    },
    width: '80%'
  });


  // init bubble
  bubble = '<div style="display:none" id="bubble"> <\/div';
  $('#header').append(bubble);
  bubble_beingShown = false;
  bubble_shown = false;

  // open all pdf-links in blank window
  $("a[href*=.pdf], a.blank").attr('target', '_blank');
  

  $('ul.list_hover li a span').hide();

  $('ul.list_hover li a').click(function() {
    active = $(this).find('span');
    $(this).parent().parent().find('a span').hide();
    active.fadeIn();
    return false;
  });

  
  // autocomplete anmelder

  $('#booked_teilnehmer_1_anrede').blur(function() {
    $('#booked_anmelder_anrede').val($(this).val());
  })
  $('#booked_teilnehmer_1_titel').blur(function() {
    $('#booked_anmelder_titel').val($(this).val());
  })
  $('#booked_teilnehmer_1_vorname').blur(function() {
    $('#booked_anmelder_vorname').val($(this).val());
  })
  $('#booked_teilnehmer_1_nachname').blur(function() {
    $('#booked_anmelder_nachname').val($(this).val());
  })
  $('#booked_teilnehmer_1_geburtstag').blur(function() {
    $('#booked_anmelder_geburtstag').val($(this).val());
  })


  // bubble action
  $('#nav ul a').hover(function() {
    // if(!bubble_shown) {
    //   // alert($(this).find('i').html());
    //   // alert(width + ", " + x.left);
      position = $(this).position();
      bubble_position = $('#bubble').position();
      nav_width = $(this).innerWidth();
      bubble_width = $('#bubble').innerWidth();
      center = position.left + nav_width / 2 - bubble_width / 2;
      $('#bubble').html($(this).find('i').html());
      $('#bubble').css('left', center);
      $('#bubble').show();
      height = $('#bubble b').height();
      margin_top = 20 - height/2;
      $('#bubble b').css('margin-top', margin_top);
      $('#bubble').hide();
      $('#bubble').fadeIn(300, function() {
        height = $('#bubble b').height();
        margin_top = 20 - height/2;
        $('#bubble b').css('margin-top', margin_top);
      });
    }, function() {
      $('#bubble').hide();
    });









  // TAB ACTION

  $('ul.tabs li a').hover(function() {
    // Stuff to do when the mouse enters the element;
    init_id = $('ul.tabs li.current a').parent().index();
    id = $(this).parent().index();
    $('ul.tabs li').removeClass('current');
    $('#tabsTop ul.tabs li:eq(' + id + ')').addClass('current');
    $('#tabsBottom ul.tabs li:eq(' + id + ')').addClass('current');
  }, function() {
    // Stuff to do when the mouse leaves the element;
    $('ul.tabs li').removeClass('current');
    $('#tabsTop ul.tabs li:eq(' + init_id + ')').addClass('current');
    $('#tabsBottom ul.tabs li:eq(' + init_id + ')').addClass('current');
  });


  $('ul.tabs li a').click(function() {
    id = $(this).parent().index();
    init_id = id;

    toggleTabs(id);

    scrollUp();
  });
  




  function toggleTabs(id) {
    $('ul.tabs li').removeClass('current');
    $('#tabsTop ul.tabs li:eq(' + id + ')').addClass('current');
    $('#tabsBottom ul.tabs li:eq(' + id + ')').addClass('current');

    $('ul#booking_steps li').removeClass('current');
    $('ul#booking_steps li:eq(' + id + ')').addClass('current');

    $('.tab_contents .tab_current').hide();
    $('.tab_contents .tab_content').removeClass('tab_current');
    $('#tab_' + (id+1)).addClass('tab_current');
    $('#tab_' + (id+1)).show();
  }
  
  
  
  

  // END TAB ACTION



  // INIT

  $("a[rel='gallery']").colorbox({
    current: "{current} / {total}"
  });

  $("a[rel='map']").colorbox({
    current: ""
  });


  // http://jquery.malsup.com/cycle/lite/

  $('.gallery').cycle({
    fx:           'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    timeout:       5000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          null,  // id of element to use as click trigger for next slide 
    prev:          null,  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          1,     // true if in/out transitions should occur simultaneously 
    fit:           1,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     'li.horizontal'   // expression for selecting slides (if something other than all children is required)
  });

  $('.teasers_1 .cycle').cycle({
    fx:           'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    timeout:       7500,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          '#next',  // id of element to use as click trigger for next slide 
    prev:          '#prev',  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          1,     // true if in/out transitions should occur simultaneously 
    fit:           1,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     null   // expression for selecting slides (if something other than all children is required)
  });





  // hide all tabs which are not current
  $('.tab_contents .tab_content:not(.tab_current)').hide();
  $('.tab_contents .tab_content .title').hide();

  var myFile = document.location.toString();
  if (myFile.match('#')) { // the URL contains an anchor
    // click the navigation item corresponding to the anchor
    var myAnchor = '#' + myFile.split('#')[1];
    $('#tabsTop ul.tabs li a[href="' + myAnchor + '"]').click();
  } else {
    // click the first navigation item
    // $('#tabsTop ul.tabs li a:first').click();
  }

  $('.footer_info').click(function() {
    $(this).find('div').toggle();
  })



  $('.submitFormOriginal').hide();
  $('.submitForm').show();

  $('.submitForm').click(function() {
    $(this).next('input.submitFormOriginal').click();
  });


  $(".colorbox_links a").colorbox({
    href: function() {
      var url = $(this).attr('href');
      return url;
    },
    iframe: true,
    width: '90%',
    height: '90%'
  });

  $("a[rel='popup']").colorbox({
    href: function() {
      var url = $(this).attr('href');
      return url;
    },
    iframe: true,
    width: '90%',
    height: '90%'
  });

  $("a[rel='popup_1']").colorbox({
    href: function() {
      var url = $(this).attr('href');
      return url;
    },
    iframe: true,
    width: '90%',
    height: '90%'
  });

  $("a[rel='popup_2']").colorbox({
    href: function() {
      var url = $(this).attr('href');
      return url;
    },
    iframe: true,
    width: '90%',
    height: '90%'
  });


  // TODO
  // http://keith-wood.name/datepick.html

  $(".datepicker").datepick({
    showTrigger: '#calImg'
  });
  


  // reposition log
  $('.log').prependTo('#main');

  $('.log ul').hide();
  
  $('.log .toggle_log a .hide').hide();
  $('.log .toggle_log a').click(function() {
    $('.log .toggle_log a span').toggle();
    $('.log ul').slideToggle();
    return false;
  });








});    








headers = Array();






function maxCols(which) {
  $(which).each(function(index) {
    if(index == 0) {
      height_max = 0;
    }
    height = $(this).height();
    if(height > height_max) {
      height_max = height;
    }
  });
  $(which).css('height', height_max + "px");
}









function scrollUp() {
  if(window.pageYOffset > $('#header').height()) {
    // CONFIG
    $.scrollTo({top:'0px', left:'0px'}, 750);
  }
}



function in_array(item,arr) {
  for(p=0; p<arr.length; p++) { 
    if (item === arr[p]) {
      return p+1;
    }
  }
  return false;
}


function addZero(which) {
  if(parseInt(which) < 10) {
    return "0" + which;
  } else {
    return parseInt(which);
  }
}




