var allImages = new Array();
var selectedCatalogID = 0;
var selectedGalleryID = 0;
var currSlideID = 0;
var ssInterval;

var playInterval;
var playCounter;
var playStartOpacity;
var playTargetOpacity;
var currentOpacity=0;
var playContent1;
var playContent2;
var isLoading = false;

function gallerySelect(catID, gallID)
{
document.getElementById("INFO").style.display="none";
document.getElementById("IMGCONTROL").style.display="block";

if(!isLoading)
{
	isLoading = true;
	startLoading();

	selectedCatalogID = catID;
	selectedGalleryID = gallID;
	nextSlideID = 0;
	
	var newimg = new Image();
	newimg.onerror = function()
	{
		stopLoading();
		isLoading = false;
		currSlideID = 0;
		next();
	}
	newimg.onload = function()
	{
		document.images['bottomIMG'].src=document.images['topIMG'].src;
		playContent2 = document.images['bottomIMG'];
		playContent2.style.filter="alpha(opacity=100)";
		playContent2.style.opacity=1;	
	
		document.images['topIMG'].src=imgName;
		playContent1 = document.images['topIMG'];
		playContent1.style.filter="alpha(opacity=0)";
		playContent1.style.opacity=0;	

		playTransition();
		currSlideID = nextSlideID;
		SetINFO();
		stopLoading();
		isLoading = false;
	}
	var imgName = allImages[selectedCatalogID][selectedGalleryID][0];
	newimg.src = imgName;
}
}
function infoSelect()
{
	document.getElementById("INFO").style.display="block";
	document.getElementById("IMGCONTROL").style.display="none";
}


function setOpacity()
{
	if(playCounter>100)
	{
		playContent1.style.filter="alpha(opacity=100)";
		playContent1.style.opacity=1;	
		playContent2.style.filter="alpha(opacity=0)";
		playContent2.style.opacity=0;

		clearInterval(playInterval);
	}
	else
	{
		currentOpacity=playStartOpacity + (playTargetOpacity-playStartOpacity)*(playCounter/100);

		playContent1.style.filter="alpha(opacity=" + currentOpacity + ")";
		playContent1.style.opacity=currentOpacity/100;	
		playContent2.style.filter="alpha(opacity=" + (100-currentOpacity) + ")";
		playContent2.style.opacity=(100-currentOpacity)/100;	

		playCounter+=2;
	}
}

function playTransition()
{
	playTargetOpacity = 100;
	playStartOpacity = 0;
	playCounter = 0;


	clearInterval(playInterval);
	playInterval = setInterval("setOpacity()", 10);
}

function SetINFO()
{
	var txtPlayStop = "PLAY"
	if(ssRunning)txtPlayStop = "STOP";
	document.getElementById("imgcounter").firstChild.nodeValue = currSlideID + 1;
	document.getElementById("PLAYSTOP").firstChild.nodeValue = txtPlayStop;
}

function next()
{
if(!isLoading)
{
	startLoading();
	isLoading = true;
	nextSlideID = currSlideID+1;
	
	if(nextSlideID>=allImages[selectedCatalogID][selectedGalleryID].length)
		nextSlideID = 0;

	var newimg = new Image();
	newimg.onerror = function()
	{
		stopLoading();
		currSlideID = nextSlideID;
		isLoading = false;
		next();
	}
	newimg.onload = function()
	{
		document.images['bottomIMG'].src=document.images['topIMG'].src;
		playContent2 = document.images['bottomIMG'];
		playContent2.style.filter="alpha(opacity=100)";
		playContent2.style.opacity=1;	
	
		document.images['topIMG'].src=imgName;
		playContent1 = document.images['topIMG'];
		playContent1.style.filter="alpha(opacity=0)";
		playContent1.style.opacity=0;	

		playTransition();
		currSlideID = nextSlideID;
		SetINFO();
		stopLoading();
		isLoading = false;
	}
	var imgName = allImages[selectedCatalogID][selectedGalleryID][nextSlideID];
	newimg.src = imgName;
}
}

function prev()
{
if(!isLoading)
{
	startLoading();
	isLoading = true;
	nextSlideID = currSlideID-1;
	if(nextSlideID<0)
		nextSlideID = allImages[selectedCatalogID][selectedGalleryID].length-1;

	var newimg = new Image();
	newimg.onerror = function()
	{
		stopLoading();
		currSlideID = nextSlideID;
		isLoading = false;
		next();
	}
	newimg.onload = function()
	{
		document.images['bottomIMG'].src=document.images['topIMG'].src;
		playContent2 = document.images['bottomIMG'];
		playContent2.style.filter="alpha(opacity=100)";
		playContent2.style.opacity=1;	
	
		document.images['topIMG'].src=imgName;
		playContent1 = document.images['topIMG'];
		playContent1.style.filter="alpha(opacity=0)";
		playContent1.style.opacity=0;	

		playTransition();
		currSlideID = nextSlideID;
		SetINFO();
		stopLoading();
		isLoading = false;
	}
	var imgName = allImages[selectedCatalogID][selectedGalleryID][nextSlideID];
	newimg.src = imgName;
}
}


// SLIDESHOW!

var ssRunning = false;

function ssGo()
{
	ssStop();
	next();
	ssRunning = true;
	ssInterval = setInterval("next();", 3000);
}

function ssStop()
{
	ssRunning = false;
	clearInterval(ssInterval);
	SetINFO();
}

function ssToggle()
{
	if(ssRunning)ssStop();
	else ssGo();
	SetINFO();
}

// LOADING animation

var LoadingTxt = "loading";
var LoadingInterval;
var LoadingStep = 0;

function aniLoading()
{
	LoadingTxt = "loading";
	for(var i=0; i<LoadingStep; i++)
		LoadingTxt = LoadingTxt + ".";

	LoadingStep++;
	if(LoadingStep>3)LoadingStep = 0;

	document.getElementById("LOADINGPROGRESS").firstChild.nodeValue = LoadingTxt;	
}

function startLoading()
{

	document.getElementById("LOADINGPROGRESS").style.display = "block";
	clearInterval(LoadingInterval);
	LoadingInterval = setInterval("aniLoading()", 200);
}

function stopLoading()
{
	document.getElementById("LOADINGPROGRESS").style.display = "none";
	LoadingTxt = "LOADING";
	LoadingStep = 0;
	clearInterval(LoadingInterval);
}

