$(document).ready(function() {
  
  //equalize height of boxes
  $('.box-container').each(function(){
    var i = 0; 
    var row = 0;
    var maxHeight = 0;
    var heights = new Array();
    $('.box .content', this).each(function(){ //loop to get heights
      i++;
      if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
      if(i % 3 == 0){
        heights[row] = maxHeight;
        row++;
        maxHeight = 0;
      }
    });
    var i = 0; 
    var row = 0;
    $('.box .content', this).each(function(){ //loop again to set heights
      i++;
      $(this).height(heights[row]);
      if(i % 3 == 0){
        row++;
      }
    });
  });
  
  //equalize width of other elements
  var maxWidth = 0;
  $('.equal-width-container .equal-width').each(function(){
    if($(this).width() > maxWidth) maxWidth = $(this).width();
  });
  $('.equal-width-container .equal-width').width(maxWidth+20);
  
  
  
  
  //start coda slider
  $('.coda-slider').codaSlider({
    autoSlide: true,
    autoSlideInterval: 4000,
    autoSlideStopWhenClicked: true,
    dynamicTabsPosition : 'bottom',
    panelTitleSelector : '.slider-title',
    dynamicArrowLeftText : 'einen Slide zurück',
    dynamicArrowRightText : 'einen Slide vor',
    dynamicTabsAlign : 'auto'
  });
  
  //increase Slider headlines when title is short
  $('.slider-subtitle').each(function(){
    if($(this).text().length < 22) $(this).addClass('slider-subtitle-large');
    if($(this).text().length > 50) $(this).addClass('slider-subtitle-small');
  });
  
  //contact form dynamics
  var expanded = true;
  $('#contact-button-expand').click(toogle_expand);
  function toogle_expand(){
    if(expanded){
      $('.row-contact-form .content').removeClass('expanded');
      $('#contact-button-expand span').html('&raquo;');
      expanded = false;
    }
    else{
      expand();
    }
  }
  function expand(){
    $('.row-contact-form .content').addClass('expanded');
    $('#contact-button-expand span').html('&laquo;');
    expanded = true;
  }
  toogle_expand();
  $('#contact-button-expand').removeClass('hidden');
  
  //opening triggers
  $('#name').focus(expand);
  if($('.row-contact-form .error').length > 0){
    expand();
    $('#contact-form').focus();
  }
  
  
  //center services nav
  var availablewidth = $('.nav-services nav').width();
  var totalwidth = 0;
  $('.nav-services li a').each(function (){
    totalwidth += $(this).width();
  });
  var margindelta = Math.floor((availablewidth-totalwidth)/($('.nav-services li a').length*2));
  var totalwidth = 0;
  var i = 0;
  $('.nav-services li a').each(function (){
    if(i == 0) $(this).css('padding-left', margindelta-21);
    else $(this).css('padding-left', margindelta-5);
    $(this).css('padding-right', margindelta+5);
    totalwidth += $(this).width() + 2*margindelta;
    i++;
  });
  //$('.nav-services ul').width(totalwidth);
  
  //add .first-child and .last-child to li
  $('.nav-services li:first-child').addClass('first-child');
  $('.nav-services li:last-child').addClass('last-child');
  if($('.nav-services li:last-child').hasClass('active'))
    $('.nav-services li:last-child').addClass('active-last-child');
  
  //Activate Lighbox
  $('a.lightbox').lightBox();
  

  //remove empty tags (should be done in typo3
  if($('.box-container').children().length < 1) $('.box-container').remove(); 
  
  //news
  if($('.news-ticker li').length > 0){
    $().newsTicker({
      newsList: '.news-ticker',
      placeHolder1: "_",
      controls : false,
      resumeOffHover : false,
      stopOnHover : false,
      tickerRate: 80,
      startDelay: 100,
      loopDelay: 3000
    });
  }
  
  //Change window size of popup windows for frontendediting
  //TODO!

    
});

