function create_request_object()
{
	var xmlhttp;
	if (window.XMLHttpRequest)
    {
    	//IE 7+, Firefox, Opera, Chrome, Safari
    	xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
    	//IE 5, 6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
    	alert("Your browser does not support AJAX! Please update your browser, or go to <a href='http://www.firefox.com'>firefox.com</a> to download an up-to-date browser");
        return false;
    }

    return xmlhttp;
}

function setup_gallery()
{
	image_location = 0;
	images = new Array();
	xml = create_request_object();
	if(xml == null)
	{
		alert('A problem occurred while creating the AJAX object\n');
	}

	xml.onreadystatechange = state_action;

	try
	{
		xml.open("GET", "frame/scan_dir.php", true);
		xml.send(null);
	}
	catch(e)
	{
		alert("Could not send request: "+e);
	}
}

function change_image(loc)
{
	var temp = images[loc].split(".");

	try
	{
		$("display_image").src = "/images/"+images[loc];
	}
	catch(e)
	{
		alert('could not change image: '+e);
	}
}

function first()
{
	fadeout("display_image");
	var a = setTimeout('fade("display_image")', 500);
	image_location = 0;
	var t=setTimeout('change_image(image_location)', 450);
}

function last()
{
	fadeout("display_image");
	var a=setTimeout('fade("display_image")', 500);
	image_location = images.length-1;
	var t=setTimeout('change_image(image_location)', 450);
}

function next()
{
	fadeout("display_image");
	var a=setTimeout('fade("display_image")', 500);
	image_location++;
	if(image_location >= images.length)
	{
		image_location = 0;
	}
	var t=setTimeout('change_image(image_location)', 450);
}

function previous()
{
	fadeout("display_image");
	var a=setTimeout('fade("display_image")', 500);
	image_location--;
	if(image_location < 0)
	{
		image_location = images.length-1;
	}
	var t=setTimeout('change_image(image_location)', 450);
}

function state_action()
{
	if(xml.readyState == 4 || xml.readyState == "complete")
	{
		images = eval("["+xml.responseText+"]");
		change_image(0);
	}
}

function fade(element)
{
	new Effect.Opacity(element, {duration:1, from:0, to:1.0});
}
function fadeout(element)
{
	new Effect.Opacity(element, {duration:.5, from:1.0, to:0});
}
