//javascript document
//(c) 2009 Stefan Brand
//Picture Show 23.08.09

var debug = false;

//Saves the index of the picture currently shown in the minigallery to be able to show the previous/next pic
var currentpic = 0;

//**********
//** AJAX **
//**********
function CreateRequestObject()
{
	//IE 6 needs a different request object than IE7, Firefox, Opera etc.
	var ro;
	var browser = navigator.appName;
	if ( browser == "Microsoft Internet Explorer" )
	{
		ro = new ActiveXObject( "Microsoft.XMLHTTP" );
	}
	else
	{
		ro = new XMLHttpRequest();
	}
	return ro;
}

//Initialise the request object
var http = CreateRequestObject();

//Initialise Send Request (when page is loaded first random pic)
function InitialSendRequest()
{
	http.open( "get", "../php/pictureshow.php?init=1" );
	http.onreadystatechange = HandleResponse;
	http.send( null );
}

//Send Request (if next == true => next picure else previous picture)
function SendRequest(next)
{
	if (next == true)
	{ 
		currentpic += 1;
		if (debug == true)
			alert("Next(->" + currentpic + ")");

	}
	else 
	{ 
		currentpic -= 1;
		if (debug == true)
			alert("Prev(->" + currentpic + ")");

	}
	http.open( "get", "../php/pictureshow.php?pictureindex=" + currentpic );
	http.onreadystatechange = HandleResponse;
	http.send( null );
}

//Handle response
function HandleResponse()
{
	var pic = new Array(document.getElementById('pictureshow_previous'), document.getElementById('pictureshow_current'), document.getElementById('pictureshow_next'));
	if ( http.readyState == 4 )
	{
		//Update the image
		var response = http.responseText.split("|");
		if (debug == true)
			alert("Prev: " + response[0] + "\nCurrent: " + response[1] + "\nNext: " + response[2] + "\nPictureindex: " + response[3]);
		for (var i = 0; i < pic.length; i++)
		{
			pic[i].src = "../images/" + response[i];
		}
		currentpic = parseInt(response[3]);
	}
}
