﻿// jQuery SlideShow

// Settings
var slideshow = {
  first: 'ul#event',
  selector: '#selector',
  delay: 9000,
  fadeDelay: 1000
};

slideshow._interval = false;

// Switch the slides
function slideshowGoto(index) {
  var $item = $(slideshow.first);
  if ('undefined' === typeof(index)) {
      
    // Go to next on the list
    if ($item.find('li:last').hasClass('active')) {
      $item
        .find('li.active')
          .stop(true,true)
          .removeClass('active')
          .fadeOut(slideshow.fadeDelay)
          .end()
        .find('li:first')
          .addClass('active')
          .fadeIn(slideshow.fadeDelay);
    } else {
      $item
        .find('li.active')
          .stop(true,true)
          .removeClass('active')
          .fadeOut(slideshow.fadeDelay)
        .next()
          .addClass('active')
          .fadeIn(slideshow.fadeDelay);
    }

    updateSelector();

  } else {
    $item
      .find('li.active')
        .stop(true,true)
        .removeClass('active')
        .fadeOut(slideshow.fadeDelay)
        .end()
      .find('li').eq(index)
        .addClass('active')
        .fadeIn(slideshow.fadeDelay);

    updateSelector(index);
  }
}

// Switch the dotted element
function updateSelector(index) {
  var $item = $(slideshow.selector);
  if ('undefined' === typeof(index)) {
    
    //Move the active class to the next element
    if ($item.find('li:last').hasClass('active')) {
      $item
        .find('li.active')
          .removeClass('active')
          .end()
        .find('li:first')
          .addClass('active');      

    } else {
      $item
        .find('li.active')
          .removeClass('active')
        .next()
          .addClass('active');
    }

  } else {
    
    $item
      .find('li.active')
        .removeClass('active')
        .end()
      .find('li').eq(index)
        .addClass('active');
  }
}


$(document).ready(function() {
  
  //
  // Start slideshow
  //
  
  var $item = $(slideshow.first);
  
  $item.find('li').hide();

  if (1 < $item.find('li').length) {
    
    // Show first
    $item.find('li:first')
      .show()
      .addClass('active');
    
  } else if (1 == $item.length) {
    
    // Show first one that is present
    $item.show();
  }

  //
  // Handle the selector
  //
  
  var $item = $(slideshow.selector);
  // Set the first to active
  $item.find('li:first').addClass('active');

  // Add click event for links
  $item.find('a').click(function(e) {
    
    // Find out if this already the active li
    if (!$(this).closest('li').hasClass('active')) {
      // Start the animation - it will also handle animation for the selectorbar
      slideshowGoto($(this).closest('ul').find('a').index(this));
    }
    if (slideshow._interval) clearInterval(slideshow._interval);
    slideshow._interval = setInterval('slideshowGoto()', slideshow.delay);
    // Do not follow the link!
    e.preventDefault();
  });

  // Go go go
  slideshow._interval = setInterval('slideshowGoto()', slideshow.delay);
});
