// JavaScript Document
var IMAGESPERPAGE = 9;
var FSIZE = 400;
var FLEFT = 50;
var FTOP = 50;
var FOPACITY = 0.8;
var LEAP = 30;
var BLACKSQUARE;
var FULLIMAGE;
var NEWIMAGE;
var NUM;
var NUMOFIMAGES;
var ALBUM;
var DONE = true;

window.onload = function()
{
	BasicOnLoad()
	BLACKSQUARE = document.getElementById("BlackSquare");
	ALBUM = 1;
	NUMOFIMAGES = 0;
	while (document.getElementById("Thumb" + (NUMOFIMAGES*1 + 1)))
	{
		NUMOFIMAGES++;
		if (NUMOFIMAGES <= IMAGESPERPAGE)
			document.getElementById("Thumb" + NUMOFIMAGES).style.display = "block";
		else
			document.getElementById("Thumb" + NUMOFIMAGES).style.display = "none";
	}
	
	
	
	document.getElementById("Loading").style.display = "none";
	
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.9;", 100);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.8;", 150);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.7;", 200);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.6;", 250);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.5;", 300);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.4;", 350);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.3;", 400);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.2;", 450);
	window.setTimeout ("BLACKSQUARE.style.opacity = 0.1;", 500);
			
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 90)';", 100);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 80)';", 150);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 70)';", 200);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 60)';", 250);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 50)';", 300);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 40)';", 350);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 30)';", 400);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 20)';", 450);
	window.setTimeout ("BLACKSQUARE.style.filter = 'alpha(opacity = 10)';", 500);
	
	window.setTimeout ("BLACKSQUARE.style.display = 'none';", 550);
	window.setTimeout ("HideShowAlbumNav()", 600);
}

function ThumbClick(num)
{
	if (DONE)
	{
		DONE = false;
		NUM = num;
		
		FULLIMAGE = document.getElementById("Image" + NUM);
		
		FULLIMAGE.style.width = "0px";
		FULLIMAGE.style.height = "0px";
		BLACKSQUARE.style.opacity = 0;
		BLACKSQUARE.style.filter = "alpha(opacity=0)";
		
		FULLIMAGE.style.display = "block";
		BLACKSQUARE.style.display = "block";
		
		ToImage(0, 0, 0);
	}
	return false;
}

function BackToGallery()
{	
	if (DONE)
	{
		DONE = false;
		FULLIMAGE = document.getElementById("Image" + NUM);
		
		document.getElementById("BackToGallery").style.display = "";
		document.getElementById("Previous").style.display = "";
		document.getElementById("Next").style.display = "";
		
		ToGallery(FSIZE, FSIZE, FOPACITY);
	}
	return false;
}

function ToImage(width, height, opacity)
{
	var left;
	var top;
	var end = false;
	
	HideAlbumNav()
	
	if (opacity < FOPACITY)
	{
		opacity += 0.05;
		BLACKSQUARE.style.opacity = opacity;
		BLACKSQUARE.style.filter = "alpha(opacity=" + opacity * 100 + ")";
	}
	else
	{
		if (height < FSIZE)
		{
			width = 2;
			height += FSIZE / 10;
		}
		else if (width < FSIZE)
		{
			width += FSIZE / 8;
			height = FSIZE;
		}
		else
		{
			width = FSIZE;
			height = FSIZE;
			
			document.getElementById("BackToGallery").style.display = "block";
			document.getElementById("Previous").style.display = "block";
			document.getElementById("Next").style.display = "block";
			
			end = true;
		}
		
		left = Math.round((FSIZE - width) / 2 + FLEFT);
		top = Math.round((FSIZE - height) / 2 + FTOP);
		
		FULLIMAGE.style.width = Math.round(width) + "px";
		FULLIMAGE.style.height = Math.round(height) + "px";
		FULLIMAGE.style.left = left + "px";
		FULLIMAGE.style.top = top + "px";
	}
	
	if (!end)
		window.setTimeout("ToImage(" + width + ", " + height + ", " + opacity + ");", LEAP);
	else
		DONE = true;
}

function ToGallery(width, height, opacity)
{
	var left;
	var top;
	var end = false;
	
	
	if (height > 0)
	{
		if (width > 2 + FSIZE / 8)
		{
			width -= FSIZE / 8;
			height = FSIZE;
		}
		else if (height > 0 + FSIZE / 10)
		{
			width = 2;
			height -= FSIZE / 10;
		}
		else
		{
			width = 2;
			height = 0;
			FULLIMAGE.style.display = "none";
		}
		
		left = Math.round((FSIZE - width) / 2 + FLEFT);
		top = Math.round((FSIZE - height) / 2 + FTOP);
		
		FULLIMAGE.style.width = Math.round(width) + "px";
		FULLIMAGE.style.height = Math.round(height) + "px";
		FULLIMAGE.style.left = left + "px";
		FULLIMAGE.style.top = top + "px";
	}
	else
	{
		if (opacity > 0)
		{
			opacity -= 0.05;
			BLACKSQUARE.style.opacity = opacity;
			BLACKSQUARE.style.filter = "alpha(opacity=" + opacity * 100 + ")";
		}
		else
		{
			BLACKSQUARE.style.display = "none";
			end = true
		}
	}
	if (!end)
		window.setTimeout("ToGallery(" + width + ", " + height + ", " + opacity + ");", LEAP);
	else
	{
		HideShowAlbumNav()
		DONE = true;
	}
}

function GalleryPrevious()
{
	if (DONE)
	{
		DONE = false;
		if (NUM > 1)
			NUM--;
		else
			NUM = NUMOFIMAGES;
		NEWIMAGE = document.getElementById("Image" + NUM);
		
		NEWIMAGE.style.opacity = 0;
		NEWIMAGE.style.filter = "alpha(opacity=0)";
		NEWIMAGE.style.display = "block";
		NEWIMAGE.style.zIndex = 1000;
		
		FadeImage(0);
	}
	return false;
}

function GalleryNext()
{
	if (DONE)
	{
		DONE = false;
		if (NUM < NUMOFIMAGES)
			NUM++;
		else
			NUM = 1;
		NEWIMAGE = document.getElementById("Image" + NUM);
		
		NEWIMAGE.style.opacity = 0;
		NEWIMAGE.style.filter = "alpha(opacity=0)";
		NEWIMAGE.style.display = "block";
		NEWIMAGE.style.zIndex = 1000;
		
		FadeImage(0);
	}
	return false;
}

function FadeImage(opacity)
{
	if (opacity < 1)
	{
		opacity += 0.05;
		NEWIMAGE.style.opacity = opacity;
		NEWIMAGE.style.filter = "alpha(opacity=" + opacity * 100 + ")";
		window.setTimeout("FadeImage(" + opacity + ");", LEAP);
	}
	else
	{
		FULLIMAGE.style.display = "none";
		NEWIMAGE.style.zIndex = "";
		FULLIMAGE = NEWIMAGE;
		DONE = true;
	}
}


function AlbumMove(dir)
{
	if (DONE)
	{
		DONE = false;
		var prevAlbum = ALBUM;
		if (dir == "Next")
			ALBUM++;
		else if (dir == "Previous")
			ALBUM--;
		
		HideShowAlbumNav();
		
		window.setTimeout("FadeImages(0.9, " + prevAlbum + ");", 30);
		window.setTimeout("FadeImages(0.8, " + prevAlbum + ");", 60);
		window.setTimeout("FadeImages(0.7, " + prevAlbum + ");", 90);
		window.setTimeout("FadeImages(0.6, " + prevAlbum + ");", 120);
		window.setTimeout("FadeImages(0.5, " + prevAlbum + ");", 150);
		window.setTimeout("FadeImages(0.4, " + prevAlbum + ");", 180);
		window.setTimeout("FadeImages(0.3, " + prevAlbum + ");", 210);
		window.setTimeout("FadeImages(0.2, " + prevAlbum + ");", 240);
		window.setTimeout("FadeImages(0.1, " + prevAlbum + ");", 270);
		window.setTimeout("FadeImages(0.0, " + prevAlbum + ");", 300);
		window.setTimeout("SwitchImages('" + dir + "')", 300);
		window.setTimeout("FadeImages(0.1, ALBUM);", 480);
		window.setTimeout("FadeImages(0.2, ALBUM);", 510);
		window.setTimeout("FadeImages(0.3, ALBUM);", 540);
		window.setTimeout("FadeImages(0.4, ALBUM);", 570);
		window.setTimeout("FadeImages(0.5, ALBUM);", 600);
		window.setTimeout("FadeImages(0.6, ALBUM);", 630);
		window.setTimeout("FadeImages(0.7, ALBUM);", 660);
		window.setTimeout("FadeImages(0.8, ALBUM);", 690);
		window.setTimeout("FadeImages(0.9, ALBUM);", 720);
		window.setTimeout("FadeImages(1.0, ALBUM);", 750);
		window.setTimeout("DONE = true;", 750);
	}
	return false;
}

function HideShowAlbumNav()
{
	if (document.getElementById("Thumb" + (ALBUM * IMAGESPERPAGE + 1)))
		document.getElementById("AlbumNext").style.display = "block";
	else
		document.getElementById("AlbumNext").style.display = "none";
	if (ALBUM > 1)
		document.getElementById("AlbumPrevious").style.display = "block";
	else
		document.getElementById("AlbumPrevious").style.display = "none";
}

function HideAlbumNav()
{
	document.getElementById("AlbumNext").style.display = "none";
	document.getElementById("AlbumPrevious").style.display = "none";
}


function FadeImages(opacity, album)
{
	for (var i=1; i<=IMAGESPERPAGE; i++)
	{
		document.getElementById("Thumb" + ((album - 1) * IMAGESPERPAGE + i)).style.opacity = opacity;
		document.getElementById("Thumb" + ((album - 1) * IMAGESPERPAGE + i)).style.filter = "alpha(opacity=" + opacity * 100 + ")";
	}
}

function SwitchImages(dir)
{
	for (var i=1; i<=IMAGESPERPAGE; i++)
	{
		if (dir == "Next")
		{
			if (document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)))
			{
				document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.display = "block";
				document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.opacity = 0;
				document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.filter = "alpha(opacity=0)";
			}
			document.getElementById("Thumb" + ((ALBUM - 2) * IMAGESPERPAGE + i)).style.display = "none";
		}
		else if (dir == "Previous")
		{
			if (document.getElementById("Thumb" + (ALBUM * IMAGESPERPAGE + i)))
				document.getElementById("Thumb" + (ALBUM * IMAGESPERPAGE + i)).style.display = "none";
			document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.display = "block";
			document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.opacity = 0;
			document.getElementById("Thumb" + ((ALBUM - 1) * IMAGESPERPAGE + i)).style.filter = "alpha(opacity=0)";
		}
	}
}