// #############
// # SLIDESHOW #
// #############

function slideshow(element, crawl_images_from) {
  var onBefore = function() { };
  
  var onAfter = function() { };
  
  jQuery.fn.cycle.defaults = {
      fx:           'fade',                   // One of: fade, shuffle, zoom, slideX, slideY, scrollUp/Down/Left/Right
      timeout:       3500,                    // Milliseconds between slide transitions (0 to disable auto advance)
      speed:         1500,                    // Speed of the transition (any valid fx speed value)
      speedIn:       null,                    // Speed of the 'in' transition
      speedOut:      null,                    // Speed of the 'out' transition
      before:        onBefore,                // Transition callback (scope set to element to be shown)
      after:         onAfter,                 // Transition callback (scope set to element that was shown)
      easing:        null,                    // Easing method for both in and out transitions
      easeIn:        null,                    // Easing for 'in' transition
      easeOut:       null,                    // Easing for 'out' transition
      shuffle:       { top: 15, left: -100 }, // Coords for shuffle animation, ex: { top:15, left: 200 }
      animIn:        null,                    // Properties that define how the slide animates in
      animOut:       null,                    // Properties that define how the slide animates out
      startingSlide: 0,                       // Zero-based index of the first slide to be displayed
      sync:          1,                       // True if in/out transitions should occur simultaneously
      random:        1,                       // True for random, false for sequence (not applicable to shuffle fx)
      fit:           1,                       // Force slides to fit container
      pause:         1,                       // True to enable 'pause on hover'
      autostop:      0,                       // True to end slideshow after X transitions (where X == slide count)
      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)
      cleartype:     1                        // True if clearType corrections should be applied (for IE)
  };
  
  var cycle = jQuery(element).cycle(jQuery.fn.cycle.defaults);
  
  var restart = function() {
    cycle.stop();
    cycle = jQuery(element).cycle(jQuery.fn.cycle.defaults);
  };
  
  var slides = new Array();
  
  jQuery(crawl_images_from).each(function(i) { slides.push(jQuery(this).attr('href')); });
  
  var img = document.createElement('img');
  
  jQuery(img).bind('load', function() {
    if(slides[0]) {
      this.src = slides.shift();
      jQuery(element).append('<div class="slide"><img src="' + this.src + '" height="198" width="450" title="metafinanz - living dynamic X&sup2;cellence" alt="metafinanz - living dynamic X&sup2;cellence" /></div>');
    } else { restart(); }
  }).trigger('load');
};

jQuery(document).ready(function() {
  // #############
  // # SLIDESHOW #
  // #############
  
  slideshow('div#header div#teaser div.image', 'div#header div#teaser div.slides a.slide');
  
  // #############
  // # ACCORDION #
  // #############
  
  jQuery('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 = jQuery('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 = jQuery(event.data.scrollable);
    
    scrollable.scrollable().prev();
    
    return false;
  }
  
  var onNextClick = function(event) {
    var scrollable = jQuery(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'
      jQuery(api.getVisibleItems()[api.getVisibleItems().length - 1]).css('border-right', '1px solid #FFFFFF'); // 'Border kung fu'
    });
    
    api.onSeek(function() { // Hook into 'onSeek'
      jQuery(api.getVisibleItems()[api.getVisibleItems().length - 1]).css('border-right', '1px solid #BCBCBC'); // 'Border kung fu'
    });
    
    jQuery(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');
  }
});