  function startSlideshow() {
    new PeriodicalExecuter(function () {
        var index = $('slideshow').getAttribute('current_index') || 0;
        var images = $$('#slideshow .image');
        var nextIndex = ((1+parseInt(index)) >= images.length) ? 0 : (1+ parseInt(index));

        $('slideshow').setAttribute('current_index', nextIndex);

        new Effect.Parallel([
          new Effect.Fade(images[index], {sync:true,transition:Effect.Transitions.linear}),
          new Effect.Appear(images[nextIndex], {sync:true,transition:Effect.Transitions.linear})
        ], { duration:3});
      }, 5);
  }


  function galleryRandom(nextIndex) {

    showHideThumbnails('thumbnailbutton');

    var index = $('gallery').getAttribute('current_index') || 0;

    var images = $$('#gallery .image');
    $('gallery').setAttribute('current_index', nextIndex);

    $(images[index]).hide();
    $(images[nextIndex]).show();

    $('current').innerHTML = 1+nextIndex;

    return false;
  }

  function galleryNext() {
    $('gallery').show();
    $('thumbnails').hide();

    $('thumbnailbutton').innerHTML = 'THUMBNAILS';

    var index = $('gallery').getAttribute('current_index') || 0;

    var images = $$('#gallery .image');
    var nextIndex = ((1+parseInt(index)) >= images.length) ? 0 : (1+ parseInt(index));

    $('gallery').setAttribute('current_index', nextIndex);

    new Effect.Parallel([
      new Effect.Fade(images[index], {sync:true,transition:Effect.Transitions.linear}),
      new Effect.Appear(images[nextIndex], {sync:true,transition:Effect.Transitions.linear})
    ], { duration:2.0});

    $('current').innerHTML = 1+nextIndex;

    return false;
  }

  function galleryPrev() {
    $('gallery').show();
    $('thumbnails').hide();

    $('thumbnailbutton').innerHTML = 'THUMBNAILS';

    var index = $('gallery').getAttribute('current_index') || 0;

    var images = $$('#gallery .image');
    var prevIndex = ((parseInt(index)-1) <0 ) ? (images.length-1) : (parseInt(index)-1);

    $('gallery').setAttribute('current_index', prevIndex);

    new Effect.Parallel([
      new Effect.Fade(images[index], {sync:true,transition:Effect.Transitions.linear}),
      new Effect.Appear(images[prevIndex], {sync:true,transition:Effect.Transitions.linear})
    ], { duration:2.0});

    $('current').innerHTML = 1+prevIndex;

    return false;
  }


  function showHideThumbnails(button) {

    button = $(button);

    if($('gallery').visible()) {

        $('gallery').hide();
        $('thumbnails').show();

        button.innerHTML = 'HIDE THUMBNAILS';
    } else {

        $('gallery').show();
        $('thumbnails').hide();

        button.innerHTML = 'THUMBNAILS';
    }

    return false;
  }


  function prepLoadBgImages() {

    $$('#gallery .image').each(function(image) {

            if(image.style.backgroundImage != '') {
                var img = new Image();
                img.src = image.style.backgroundImage.substring(4, image.style.backgroundImage.length-1);
            }

        });
  }