/// <reference path='jquery-1.4.1-vsdoc.js'/>
var current = 0, max = 0, interval, pause, playing = true;
var playSpeed = 4000, fadeSpeed = 800, animateSpeed = 300;
$(window).ready(function () {
  $('.homeSlide').each(function (i) {
    var btn = $('<a id="homeNavBtn_' + i + '" href="#">' + (i + 1) + '</a>').click(function (e) { e.preventDefault(); btnClicked(i, $(this)); }).focus(function () { $(this).blur(); });
    if (i == 0) {
      btn.addClass('homebtnOn');
      var el = $(this).children('.homecopy');
      $(this).children().fadeIn(fadeSpeed);
      el.animate({ left: '-=10' }, animateSpeed);
    }
    else btn.addClass('homeBtn');
    $('#slideshownav').append(btn);
    max++;
  });
  pause = $('<a href="#" id="homePause" class="homebtnOff"></a>').click(function (e) { e.preventDefault(); pauseSlide(); }).focus(function () { $(this).blur(); });
  $('#slideshownav').append(pause);
  interval = setInterval(nextSlide, playSpeed);
});

function btnClicked(index, btn) {
  playing = true;
  pauseSlide();
  goToSlide(index, btn);
}

function goToSlide(index, btn) {
  $('#slideshownav').children().removeClass('homebtnOn');
  btn.addClass('homebtnOn');

  $('#slideshow').children(':eq(' + current + ')').children().stop().fadeOut(fadeSpeed);
  var el = $('#slideshow').children(':eq(' + current + ')').children('.homecopy');
  el.animate({ left: '+=10' }, animateSpeed);

  $('#slideshow').children(':eq(' + index + ')').children().stop().fadeIn(fadeSpeed);
  var el1 = $('#slideshow').children(':eq(' + index + ')').children('.homecopy');
  el1.animate({ left: '-=10' }, animateSpeed);
  
  current = index;
}

function nextSlide() {
  var index;
  if (current == max - 1) index = 0;
  else index = current + 1;
  goToSlide(index, $('#homeNavBtn_' + index));
}

function pauseSlide() {
  if (playing) {
    clearInterval(interval);
    pause.addClass('homebtnPlay');
    playing = false;
  }
  else {
    interval = setInterval(nextSlide, playSpeed);
    pause.removeClass('homebtnPlay').addClass('homebtnOff');
    playing = true;
  }
}
