var currentPhoto=0;
var previousPhoto=0;
var photos=new Array();
var links=new Array();
var intervals=new Array();
var intervalID=0;
var mode=0;
var toggle=0;
var interval=5000;
var animation=3000;

function setupInterval()
{
 	clearInterval(intervalID);
	intervalID = setInterval('nextPhoto()', interval);
}

function setupSlideshow()
{
	var preloadedimages=new Array();

	for (i=0;i<photos.length; i++)
	{
		preloadedimages[i]=new Image();
		preloadedimages[i].src=photos[i];
	}
	setupInterval();
}

function setupPhotos()
{
	setupSlideshow();
}

function preloadPhotos()
{
	setupSlideshow();
}

function previousPhoto()
{
 	clearInterval(intervalID);
	previousPhoto=currentPhoto;
	currentPhoto--;
	if (currentPhoto<0){
		currentPhoto=photos.length-1;
	}
	changePhoto();
}

function nextPhoto()
{
 	clearInterval(intervalID);
	previousPhoto=currentPhoto;
	currentPhoto++;
	if (currentPhoto >= photos.length)
	{
		currentPhoto=0;
	}
	changePhoto();
}

function changePhoto()
{
	var $current;
	var $next;
	
	if(toggle==0)
	{
		$current = $('#slideshow2');
		$next = $('#slideshow1');
		toggle = 1;
	}
	else
	{
		$current = $('#slideshow1');
		$next = $('#slideshow2');
		toggle = 0;
	}

    $next.css({opacity: 0.0});
  	$current.css('z-index', '9');
  	$next.css('z-index', '10');
	    
 	$next.attr("src",photos[currentPhoto]);
 	
    $current.animate({opacity: 0.0}, animation,
    function()
    {
    	$current.css({opacity: 0.0});
    } );
        
    $next.animate({opacity: 1.0}, animation,
    function()
    {
    	$next.css({opacity: 1.0});
    } );

	setupInterval();
}