var seconds = 8 ;
var slideWidth = 990 ;
var animationSpeed = 400 // milliseconds ;
var isPaused = false ;
var numberOfSlides ;
var numberOfQueuedSlides;
var t ;
var ready = true ;
$(document).ready(function() {
  numberOfSlides = $('#home-slides>li').size() ;
  numberOfQueuedSlides = (numberOfSlides > 4) ? 2 : 1 ;
  $('#home-slide-wrapper').css('width', (slideWidth*numberOfSlides)+'px') ;
  $('#home-slides').css('width', (slideWidth*(numberOfSlides+1))+'px') ;
  $('#home-slide-wrapper').css('margin-left', '-'+(numberOfQueuedSlides*slideWidth)+'px') ;
  $('#home-slide-wrapper>#cover1').css('width', (slideWidth*numberOfQueuedSlides-10)+'px') ;
  $('#home-slide-wrapper>#cover2').css('width', (slideWidth*(numberOfSlides-(numberOfQueuedSlides+1))-10)+'px') ;
  for (var i = 0; i < numberOfQueuedSlides; i++) {
    $('#home-slides>li:last').prependTo('#home-slides') ;
  }
  t = setTimeout("slideLeft()", seconds*1000) ;
  $('#home-slide-wrapper').hover(
    function() {
      $('#home-slide-wrapper>.nav').fadeTo(100, '1.0') ;
      isPaused = true ;
      clearTimeout(t) ;
    },
    function() {
      clearTimeout(t) ;
      $('#home-slide-wrapper>.nav').fadeTo(100, '.4') ;
      isPaused = false ;
      t = setTimeout("slideLeft()", seconds*500) ;
    }
  ) ;
  $('#nav-right>a').click(function() { clickForSlideLeft(); return false; }) ;
  $('#cover1').click(function() { clickForSlideRight(); return false; }) ;
  $('#nav-left>a').click(function() { clickForSlideRight(); return false; }) ;
  $('#cover2').click(function() { clickForSlideLeft(); return false; }) ;
});
function clickForSlideLeft() {
  isPaused = false;
  slideLeft('click');
  isPaused = true;
}
function clickForSlideRight() {
  isPaused = false;
  slideRight('click');
  isPaused = true; 
}

function slideLeft() {
  clearTimeout(t) ;
  if (!isPaused && ready) {
    ready = false ;
    var tempLi = $('#home-slides>li:first').clone() ;
    tempLi.appendTo('#home-slides') ;
    $('#home-slides').animate({"margin-left" : '-'+(slideWidth)+'px'}, animationSpeed) ;
    setTimeout("rotateLeft()",animationSpeed+100) ;
  }
  t = setTimeout("slideLeft()", seconds*1000) ;
}
function rotateLeft() {
  $('#home-slides>li:first').remove() ;
  $('#home-slides').css('margin-left', '0') ;
  ready = true ;
}
function slideRight() {
  clearTimeout(t) ;
  if (!isPaused && ready) {
    ready = false ;
    $('#home-slides').css("margin-left", '-'+(slideWidth)+'px') ;
    var tempLi = $('#home-slides>li:last').clone() ;
    tempLi.prependTo('#home-slides') ;
    $('#home-slides').animate({"margin-left" : '0px'}, animationSpeed) ;
    setTimeout("rotateRight()",animationSpeed+100) ;
  }
  t = setTimeout("slideLeft()", seconds*1000) ;
}
function rotateRight() {
  $('#home-slides>li:last').remove() ;
  ready = true ;
}

