var running = true;
var slides = setupSlideShow();
var runningTimerId = 0;

function setupSlideShow()
{
  var slides = new Array();
  var slideCounter = 1;
  
  $('#slideShowControls').append('<ul><li><span class="divider"></span></li></ul>');
  
  $('div.view-services-list div.item-list ul li').each(function(index) {
    $(this).hide();
    if ($('div.views-field-field-product-image-fid span', this).html() != null)
    {
      slides[slideCounter] = new Array();
      slides[slideCounter]['index'] = slideCounter;
      slides[slideCounter]['image_and_link'] = $('div.views-field-field-product-image-fid span', this).html();
      slides[slideCounter]['title'] = $('div.views-field-title span', this).html();
      
      $('#slideShowControls ul').append('<li class="control position_' + slideCounter + '"><span class="control_name">' + slides[slideCounter]['title'] + '</span><span class="position_id">' + slideCounter + ' </span><span class="divider"></span></li>');
      
      /* pause the slide show when the use hovers over a slide */
      $('a', this).mouseover(function() {
    	  running = false;
      });
      $('a', this).mouseout(function() {
    	  running = true;
      });
      
      slideCounter++;
    }
  });
  
  return slides;
}

function changeSlide(position, firstRun, manualClick)
{
  if (firstRun === true)
  {
    $('div.view-services-list div.item-list ul li.views-row-1').show();
  }
  
  if (manualClick === true)
  {
    $('div.view-services-list div.item-list ul li').hide();
    $('#slideShowControls li.control span.control_name').css('color', '#fff');
  }
  
  var previousPosition = position - 1;
  var nextPosition = position + 1;

  if (previousPosition <= 0)
  {
    previousPosition = slides.length - 1;
  }
  if (nextPosition >= (slides.length))
  {
    nextPosition = 1;
  }

  // console.log('previousPosition:' + previousPosition + ' position:' + position + ' nextPosition:' + nextPosition);

  if (running)
  {
	/* switch images in view */
    $('div.view-services-list div.item-list ul li.views-row-' + previousPosition).fadeOut('slow');
    $('div.view-services-list div.item-list ul li.views-row-' + position).fadeIn('slow');
    
    /* switch control button */
    $('#slideShowControls li.position_' + previousPosition + ' span.control_name').css('color', '#fff');
    $('#slideShowControls li.position_' + position + ' span.control_name').css('color', '#000');
  }
  else
  {
    nextPosition = position;
  }

  runningTimerId = setTimeout("changeSlide(" + nextPosition + ", false, false);", slideshowDisplayTime);
}

$(document).ready(function() {
  $('#slideShowControls li.control').each(function(index) {
    $(this).click(function() {
      /* stop the timer so that when the following slide is shown, it isn't out of sequence */
      clearTimeout(runningTimerId);

      var selectedSlidePosition = Number($('span.position_id', this).html());
      changeSlide(selectedSlidePosition, false, true);
    });
  });
  
  /* run the slide show */
  changeSlide(1, true, false);
});

