var ie = (!window.opera && document.all);
var dom = (!document.all && document.getElementById);
var gallery_index = 0;
var description_index = 0;

function ajaxManager()
{
	var args = ajaxManager.arguments;
	switch (args[0])
	{
		case "array_setup":
			galleryArray = new Array();
			galleryArray[0]  = "ShowPic.php?id=IMG_000";
			galleryArray[1]  = "ShowPic.php?id=IMG_001";
			galleryArray[2]  = "ShowPic.php?id=IMG_002";
			galleryArray[3]  = "ShowPic.php?id=IMG_003";
			galleryArray[4]  = "ShowPic.php?id=IMG_004";
			galleryArray[5]  = "ShowPic.php?id=IMG_005";
			galleryArray[6]  = "ShowPic.php?id=IMG_006";
			galleryArray[7]  = "ShowPic.php?id=IMG_007";
			galleryArray[8]  = "ShowPic.php?id=IMG_008";
			galleryArray[9]  = "ShowPic.php?id=IMG_009";
			galleryArray[10]  = "ShowPic.php?id=IMG_010";
			galleryArray[11] = "ShowPic.php?id=IMG_011";

//			descriptionArray = new Array();
//			descriptionArray[0] = "description1.xml";
//			descriptionArray[1] = "description2.xml";
//			descriptionArray[2] = "description3.xml";
//			descriptionArray[3] = "description4.xml";
			break;
		case "load_page":
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			pageWidth = document.body.clientWidth;
//			pageWidth = (dom) ? innerWidth : document.body.clientWidth;
			preload = document.getElementById("preloadLYR");
			gallery = document.getElementById("galleryLYR");
			if (x)
				{
				x.onreadystatechange = function()
					{
					if (x.readyState == 0)
						{
							preload.innerHTML = "initializing";
						}
					else if (x.readyState == 1)
						{
							preload.innerHTML = "processing request";
						}
					else if (x.readyState == 2)
						{
							preload.innerHTML = "request acknowledged";
						}
					else if (x.readyState == 3)
						{
							preload.innerHTML = "loading data..";
							setOpacity(0, 'galleryLYR');
//							setOpacity(0, 'captionLYR');
						}
					else if (x.readyState == 4 && x.status == 200)
						{
							el = document.getElementById(args[2]);
							el.innerHTML = x.responseText;
							preload.innerHTML = "<img src='loading.gif' width='32' height='32' align='left'>";
							getImageSize = document.getElementsByTagName("img")[0].getAttribute("width");
							gallery.style.left = (pageWidth - getImageSize) / 2+141 +"px";
							gallery.style.top = "167px";
							preload.style.left = (pageWidth - getImageSize) / 2+308+"px";
							preload.style.top = "308px";
//							document.getElementById('captionLYR').style.left = (pageWidth -	getImageSize) / 2+"px";
							document.getElementById('accessLYR').style.visibility = "hidden";
							setTimeout("fadeIn('galleryLYR', 0, '99.99')", 1200);
//							setTimeout("fadeIn('captionLYR', 0, '99.99')", 1300);
						}
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;

		case "cycle_gallery":
			if (args[1] == 'forw' && gallery_index != galleryArray.length - 1)
				{
					gallery_index++;
					description_index++;
					if(gallery_index >= 0 || gallery_index <= 3)
						ajaxManager('load_page', galleryArray[gallery_index], 'galleryLYR');
//						ajaxManager('load_page', descriptionArray[description_index], 'captionLYR');
				}
			else if (args[1] == 'rev' && gallery_index != 0)
				{
					gallery_index--;
					description_index--;
					if(gallery_index >= 0 || gallery_index <= 3)
						ajaxManager('load_page', galleryArray[gallery_index], 'galleryLYR');
//						ajaxManager('load_page', descriptionArray[description_index], 'captionLYR');
				}
			else
				{
					gallery_index = args[1]-1;
					description_index = args[1]-1;
					if(gallery_index >= 0 || gallery_index <= 3)
						ajaxManager('load_page', galleryArray[args[1]-1], 'galleryLYR');
				}
			break;
		case "hide_access":
			document.getElementById('accessLYR').style.visibility = "hidden";
			break;
		case "load_fx":
			document.getElementById('accessLYR').style.visibility = "hidden";
			break;
		case "start_up":
			ajaxManager('hide_access');
			ajaxManager('array_setup');
			ajaxManager('load_page', galleryArray[gallery_index], 'galleryLYR');
//			ajaxManager('load_page', descriptionArray[description_index], 'captionLYR');
			break;
	}
}

function fadeIn(id, startfade, endfade)
{
	timer = 0;
	if (startfade < endfade)
	{
		for (i = startfade; i <= endfade; i++)
		{
			setTimeout("setOpacity(" + i + ",'" + id + "')", (timer * 20));
			timer++;
		}
	}
}

function setOpacity(opacity, id)
{
	var el = document.getElementById(id).style;
	el.opacity = (opacity / 100);
	el.MozOpacity = (opacity / 100);
	el.KhtmlOpacity = (opacity / 100);
	el.filter = "alpha(opacity=" + opacity + ")";
}
