// JavaScript Document
var imgCount = 0;
var ssFlag = 1;
var timerId = window.setInterval( 'slideSwitchNext()', 5000 );

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function slideSwitchNext() {
	$('#inactivate').css('display','block');
	var $active = $('#ssElements img.active');
	
	if ( $active.length == 0 ) $active = $('#ssElements img:last');

	var $next =  $active.next().length ? $active.next()
						: $('#ssElements img:first');

	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1500, function() {
			$active.removeClass('active last-active');
			$('#inactivate').css('display','none');
		});
}

function slideSwitchPrev() {
	$('#inactivate').css('display','block');
	var $active = $('#ssElements img.active');
	
	if ( $active.length == imgCount ) $active = $('#ssElements img:first');

	var $next =  $active.prev().length ? $active.prev()
						: $('#ssElements img:last');

	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1200, function() {
			$active.removeClass('active last-active');
			$('#inactivate').css('display','none');
		});
}


function startSlideshow() {
	var $ssOn = $('#ssAutoOn');
	var $ssOff = $('#ssAutoOff');
	if ( ssFlag == 0 ) {
		$ssOn.css({ cursor: 'default', backgroundPosition: '-40px 0' });
		$ssOff.css({ cursor: 'pointer', backgroundPosition: '0 0' });
		timerId = window.setInterval( 'slideSwitchNext()', 5000 );
		ssFlag = 1;
	}
}

function stopSlideshow() {
	var $ssOn = $('#ssAutoOn');
	var $ssOff = $('#ssAutoOff');
	if ( ssFlag == 1 ) {
		$ssOff.css({ cursor: 'default', backgroundPosition: '-40px 0' });
		$ssOn.css({ cursor: 'pointer', backgroundPosition: '0 0' });
		window.clearInterval(timerId);
		timerId = ""; ssFlag = 0;
	}
}

$(function() {
	imgCount = $('#ssElements img').length;
	$.preloadImages("slideshow/slide_01.jpg", "slideshow/slide_02.jpg","slideshow/slide_03.jpg","slideshow/slide_04.jpg", "slideshow/slide_05.jpg","slideshow/slide_06.jpg","slideshow/slide_07.jpg");
});


