// settings for scroller
var speed = 5500;
var run = setInterval('spotlightscroller(0)', speed);

function spotlightscroller(prev) {
  //get the current selected item (with selected class), if none was found, get the first item
  var current_image = $('#spotlight-gallery li.selected').length ? $('#spotlight-gallery li.selected') : $('#spotlight-gallery li:first');
  var current_locator = $('#spotlight-picnav li.selected').length ? $('#spotlight-picnav li.selected') : $('#spotlight-picnav li:first');

  //if prev is set to 1 (previous item)
  if (prev) {
    //get previous sibling
    var next_image = (current_image.prev().length) ? current_image.prev() : $('#spotlight-gallery li:last');
    var next_locator = (current_locator.prev().length) ? current_locator.prev() : $('#spotlight-picnav li:last');
    //if prev is set to 0 (next item)
  } else {
    //get next sibling
    var next_image = (current_image.next().length) ? current_image.next() : $('#spotlight-gallery li:first');
    var next_locator = (current_locator.next().length) ? current_locator.next() : $('#spotlight-picnav li:first');
  }

  //clear the selected class
  $('#spotlight-gallery li, #spotlight-picnav li').removeClass('selected');

  //reassign the selected class to current items
  next_image.addClass('selected');
  next_locator.addClass('selected');

  //scroll the items
  $('#spotlight-mask-gallery').scrollTo(next_image, 800, {easing:'easeOutCirc'});
}

function spotlight() {
	// code vor project-page

    // calculation of containers
    $('#spotlight-mask-gallery, #spotlight-gallery li').width($('#spotlight-slider').width());	
    $('#spotlight-gallery').width($('#spotlight-slider').width() * $('#spotlight-gallery li').length);
    $('#spotlight-mask-gallery, #spotlight-gallery li').height($('#spotlight-slider').height());
    
    // set the first container to be active
    $('#spotlight-gallery li:first, #spotlight-picnav li:first').addClass('selected');
    
    // settings for scroller
    var speed = 3500;
    var run = setInterval('spotlightscroller(0)', speed);
    var slideshowstate = 0;
    
    // pause it on default
    clearInterval(run);
    
    // start/stop slideshow
    $('a.slideshow').click(function () {
      // if slideshow is not running, start it
      if (slideshowstate == 0) {
        run = setInterval('spotlightscroller(0)', speed);
        slideshowstate = 1;
        // add selected class
        $(this).addClass("slideshows");
      } else { // if slideshow is running, stop it
        clearInterval(run);
        slideshowstate = 0;
        // remove selected class
        $(this).removeClass("slideshows");
      }
      return false;
    });
    
    // get the list of navelements in an array
    var navelements = $('#spotlight-picnav li');
    // loop through the navelements
    $.each(navelements,function(index, item) {
      $(item).click(function(){
        // only animate if image/box is not the active one
        if (!$('#spotlight-mask-gallery li:eq('+index+')').hasClass("selected")) {
          // get the correct index of the corresponding image/box
          var image = $('#spotlight-mask-gallery li:eq('+index+')');
          
          //remove all selected classes
          $('#spotlight-mask-gallery li, #spotlight-picnav li').removeClass("selected");
          // set the selected class to the current image/excerpt/nav
          $('#spotlight-mask-gallery li:eq('+index+')').addClass("selected");
          $(item).addClass("selected");
          // scroll image/excerpt
          $('#spotlight-mask-gallery').scrollTo(image, 800, {easing:'easeOutCirc'});
        };
        return false;
      });

    });
	 $("ul#spotlight-picnav").show('fast');

};
function slideshow() {
	var spotlightslideshowstate = 0;
  // pause it on default
  clearInterval(run);
  if (spotlightslideshowstate == 0) {
    run = setInterval('spotlightscroller(0)', speed);
    spotlightslideshowstate = 1;
    // add selected class
    $(this).addClass("slideshows");
  } else { // if slideshow is running, stop it
    clearInterval(run);
    spotlightslideshowstate = 0;
    // remove selected class
    $(this).removeClass("slideshows");
  };
	$('#spotlight').mouseover(function(){
		clearInterval(run);
    spotlightslideshowstate = 0;
    $(this).removeClass("slideshows");
	}).mouseout(function(){
		run = setInterval('spotlightscroller(0)', speed);
    spotlightslideshowstate = 1;
    // add selected class
    $(this).addClass("slideshows");
  });
	
};
