
//Photo control script
var NUMBUTTONS = 6;
var CENTREBUTTON = 3;
var CentreButtonThNo;
var DisplayedPhotoNo;
function button_press(n)
{	var th = n - CENTREBUTTON + CentreButtonThNo; //no of thumbnail in the button clicked. May be out of range.
	if (th >= 1 && th <= NUMPHOTOS)
	{	DisplayPhoto(th);
		//UpdateButtons(th);
		//If first or last button clicked, scroll one position in appropriate direction
		if (n == 1) UpdateButtons(CentreButtonThNo - 1);
		if (n == NUMBUTTONS) UpdateButtons(CentreButtonThNo + 1);
	}
}
//Displays photo n. Does not check if argument in range.
function DisplayPhoto(n)
{	//alert(PHPATH + PHPREFIX + PhotoFileName[n])
	document.images['main'].src = PHPATH + PHPREFIX + PhotoFileName[n];
	DisplayedPhotoNo = n;
	//document.images['main'].alt
	document.getElementById("caption").innerHTML = Caption[n];
	document.getElementById("PhotoNo").value = n;
	SetMarker();
}
//Updates the buttons, putting the thumbnail for photo number "n" in the centre button if possible while leaving all the buttons populated.
//If not, puts it as close to the centre as possible.
//Sets "CentreButtonThNo" equal to n.
function UpdateButtons(n)
{	var th, b;
	if (n < CENTREBUTTON) n = CENTREBUTTON;
	if(n > NUMPHOTOS - (NUMBUTTONS-CENTREBUTTON)) n = NUMPHOTOS - (NUMBUTTONS-CENTREBUTTON);

	for(b = 1; b <= NUMBUTTONS; b++)
	{
		th = b - CENTREBUTTON + n;			//th = thumbnail number to go in button b.
		//alert("document.images[" + b + "].src = '" + THPATH + THPREFIX + PhotoFileName[th] + "';");
		eval("document.images['B" + b + "'].src = '" + THPATH + THPREFIX + PhotoFileName[th] + "';");
	}
	CentreButtonThNo = n;
	SetMarker();
}
function ShiftLeft()
{	UpdateButtons(CentreButtonThNo - NUMBUTTONS);
}
function ShiftRight()
{	UpdateButtons(CentreButtonThNo + NUMBUTTONS);
}
function MainPhotoClick()
{	//Move buttons so that main photo is in centre
	UpdateButtons(DisplayedPhotoNo);
}
function NextPhoto()
{	if(DisplayedPhotoNo == NUMPHOTOS)
	{	alert("You are viewing the last photo.");
	}
	else
	{	DisplayPhoto(DisplayedPhotoNo + 1);
		if (DisplayedPhotoNo - CentreButtonThNo > (NUMBUTTONS-CENTREBUTTON)) UpdateButtons(DisplayedPhotoNo - (NUMBUTTONS-CENTREBUTTON));
		if (CentreButtonThNo - DisplayedPhotoNo > (CENTREBUTTON-1)) UpdateButtons(DisplayedPhotoNo + (CENTREBUTTON-1));
	}
}
function PrevPhoto()
{	if(DisplayedPhotoNo == 1)
	{	alert("You are viewing the first photo.");
	}
	else
	{	DisplayPhoto(DisplayedPhotoNo - 1);
		if (DisplayedPhotoNo - CentreButtonThNo > (NUMBUTTONS-CENTREBUTTON)) UpdateButtons(DisplayedPhotoNo - (NUMBUTTONS-CENTREBUTTON));
		if (CentreButtonThNo - DisplayedPhotoNo > (CENTREBUTTON-1)) UpdateButtons(DisplayedPhotoNo + (CENTREBUTTON-1));
	}
}
function FirstPhoto()
{	UpdateButtons(1);
}
function LastPhoto()
{	UpdateButtons(NUMPHOTOS);
}
function GoToPhoto()
{	var v = document.getElementById("PhotoNo").value;
	var reject = 0, i, ch;
	var vstr = v + "";
	
	for (i = 0; i < vstr.length; i++)
	{	ch = vstr.charAt(i);
		if (ch < "0" || ch > "9") reject = 1;
	}
	if (reject == 1 || v < 1 || v > NUMPHOTOS)
	{	alert("Please enter a number between 1 and " + NUMPHOTOS + ".");
		document.getElementById("PhotoNo").value = DisplayedPhotoNo;
	}
	else DisplayPhoto(eval(v));
}	
function SetMarker()
{	//var CentreButtonThNo;
	//var DisplayedPhotoNo;
	var i, str, ThNo;
	for (i = 1; i <= NUMBUTTONS; i++)
	{	if (CentreButtonThNo + i - CENTREBUTTON == DisplayedPhotoNo)
		{	str = "red";
			eval("document.images['B" + i + "'].border = 2");		
			//eval("document.images['M" + i + "'].src = 'arrow.jpg'");		
		}
		else
		{	eval("document.images['B" + i + "'].border = 0");		
			//eval("document.images['M" + i + "'].src = 'noarrow.gif'");
			str = "#ccccff";
		}
		ThNo = CentreButtonThNo + i - CENTREBUTTON;
		eval('document.getElementById("M' + i + '").innerHTML = ' + ThNo + ' + "";');		

		//alert("document.getElementById('marker"+i+"').style.backgroundColor = '" + str + "';");
		//eval("document.getElementById('marker"+i+"').style.backgroundColor = '" + str + "';");
		//eval("document.getElementById('M"+i+"').style.borderColor = '" + str + "';");
	}
}
function Initialise()
{	DisplayedPhotoNo=1;
	CentreButtonThNo = CENTREBUTTON;
	UpdateButtons(1);
	DisplayPhoto(1);
}
