var slideWidth = 354;
var currentPosition = 0;
var slides = null;
var numberOfSlides = 0;
var playInterval;
var slideshowState = 'playing';
var inMotion = false;
var playInterval;
var inc=0;
var slides = 0;
var numberOfSlides = 0;

function loadSlideshow() {
  currentPosition = 0;
  slides = $('.slide');
  numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  playInterval = setInterval('autoplay()',6000);
  
}

function slideOver(slide) {
	clearInterval(playInterval);
}
function slideOut() {
	if (slideshowState == 'playing') {
		playSlideshow(currentPosition);
	} else {
		setSlide(currentPosition);
	}
}

function playSlideshow(slide) {
	clearInterval(playInterval);
	//autoplay(slide);
	playInterval = setInterval('autoplay()',6000);
}
function stopSlideshow() {
	clearInterval(playInterval);
	slideshowState = 'stopped';
}

function autoplay(slide) {
	if(slide!= undefined){
		slide = parseInt(slide)+1;
		setCurrentSlide(slide);
	}else{
	  if (currentPosition < numberOfSlides-1) {
		setCurrentSlide(currentPosition+1);
	  } else {
		setCurrentSlide(0);
	  }
	}
	hideSlideDetails(currentPosition);
	inMotion = true;
  // Move slideInner using margin-left
  $('#slideInner').animate({
	  'marginLeft' : slideWidth*(-currentPosition)
  },750, function() { inMotion = false } );
}
function setSlide(slide){
    // Determine new position
	if (currentPosition != slide) {
		inMotion = true;
	}
	setCurrentSlide(slide);
	hideSlideDetails(currentPosition);
	
	// Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    },750, function() { inMotion = false });
	
	// disable autoplay
	clearInterval(playInterval);
}
function setCurrentSlide(inc) {
	$('#navTitle_'+currentPosition).removeClass('currentSlide');
	$('#navTitle_'+currentPosition).find('img').removeClass('currentSlideThumb');
	$('#navTitle_'+inc).addClass('currentSlide');
	$('#navTitle_'+inc).find('img').addClass('currentSlideThumb');
	currentPosition = inc;
}
function hideSlideDetails(slide) {
	$("#home_slide_"+slide).find(".home_img_title").animate({ opacity: 0.8 }, 150 );
	var detailH = $("#home_slide_"+slide).find(".home_img_description").outerHeight();
	$("#home_slide_"+slide).find(".home_img_description").animate({ opacity: 0, bottom: '-'+detailH+'px' }, 150 );
}
function showSlideDetails(slide) {
	if (inMotion == false) {
		slide = currentPosition;
		$("#home_slide_"+slide).find(".home_img_title").animate({ opacity: 1 }, 200 );
		var detailH = $("#home_slide_"+slide).find(".home_img_description").outerHeight();
		$("#home_slide_"+slide).find(".home_img_description").css('bottom','-'+detailH+'px');
		$("#home_slide_"+slide).find(".home_img_description").animate({ opacity: 1, bottom: 0 }, 200 );
	} else {
		setTimeout("showSlideDetails('"+slide+"')",800);
	}
}
addLoadEvent(loadSlideshow);
