$(document).ready(function() {
  // #############
  // # ACCORDION #
  // #############
  
  $('div#content div.first_row div.news div.accordion').accordion({
    autoHeight: false,
    navigation: true,
    changestart: function(event, ui) { // Hook into 'changestart'
      if(browserDetect.browser == 'ie' && browserDetect.version == '6') { // Bugfixing 'IE6'
        ui.oldContent.find('a').hide();
      }
    },
    change: function(event, ui) { // Hook into 'change'
      if(browserDetect.browser == 'ie' && browserDetect.version == '6') { // Bugfixing 'IE6'
        ui.newContent.find('a').css({
          'position': 'relative',
          'top': '1.5px',
          'left': '5px'
        }).show();
      }
    }
  });
  
  // ############
  // # CAROUSEL #
  // ############
  
  var carousel = $('div#content div.second_row div.showcase div.carousel');
  var scrollable = carousel.find('div.scrollable');
  var slides = scrollable.find('div.slides div.slide');
  var previous = carousel.find('a.browse.previous').attr('href', '#'); // 'Clean up' TemplaVoilą href rewrite
  var next = carousel.find('a.browse.next').attr('href', '#'); // 'Clean up' TemplaVoilą href rewrite
  
  var onPreviousClick = function(event) {
    var scrollable = $(event.data.scrollable);
    
    scrollable.scrollable().prev();
    
    return false;
  }
  
  var onNextClick = function(event) {
    var scrollable = $(event.data.scrollable);
    
    scrollable.scrollable().next();
    
    return false;
  }
  
  if (slides.length >= 4) { // At least four 'slides' are available
    var api = scrollable.scrollable({ // Initialize 'scrollable'
      items: '.slides',
      item: '.slide',
      prev: '.previous',
      next: '.next',
      disabledClass: 'disabled',
      size: 4,
      easing: 'swing',
      speed: 500,
      clickable: false,
      loop: false,
      api: true
    });
    
    api.onBeforeSeek(function() { // Hook into 'onBeforeSeek'
      $(api.getVisibleItems()[api.getVisibleItems().length - 1]).css('border-right', '1px solid #FFFFFF'); // 'Border kung fu'
    });
    
    api.onSeek(function() { // Hook into 'onSeek'
      $(api.getVisibleItems()[api.getVisibleItems().length - 1]).css('border-right', '1px solid #BCBCBC'); // 'Border kung fu'
    });
    
    $(api.getVisibleItems()[api.getVisibleItems().length - 1]).css('border-right', '1px solid #BCBCBC');
    
    previous.unbind(); // Unbind default action from 'scrollable'
    previous.bind('click', { scrollable: scrollable }, onPreviousClick);
    
    next.unbind(); // Unbind default action from 'scrollable'
    next.bind('click', { scrollable: scrollable }, onNextClick);
  } else { // Disable 'previous' and 'next' because the are useless
    previous.addClass('disabled');
    next.addClass('disabled');
  }
});
