function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

// Script to open new window for large photos (photo_enlarge.aspx)
// Programmed by Tim Surtell
// www.surtell.com
// Version 3   : Jan 2002
// Version 4   : May 2005 - partly nicked from my NCUS manuals redevelopment
// Version 4.1 : Novembmer 2005 - converted to ASPX
// version 5   : Janaury 2007 - zoom and pan features added

var photoStartX = 0;
var photoStartY = 0;
var photoCurrentX = 0;
var photoCurrentY = 0;
var photoCurrentWidth = 0;
var photoCurrentHeight = 0;
var photoOriginalWidth = 0;
var photoOriginalHeight = 0;
var mouseStartX = 0;
var mouseStartY = 0;
var mouseCurrentX = 0;
var mouseCurrentY = 0;
var mouseButtonState = 0;
var windowWidth = 0;
var windowHeight = 0;
var zoomState = 1;

function openNewWindow(filename, photoWidth, photoHeight)
	{
		if (photoHeight <= screen.height - 300 && photoWidth <= screen.width - 200)
			{
			//Make window the same size as the full-size photo
			windowWidth = photoWidth;
			windowHeight = photoHeight;
			}
		else
			{
			//Size window to the maximum possible so that small-size photo fits
			if (photoWidth / photoHeight > 1)
				{
				// Photo is landscape
				windowWidth = screen.width - 200;
				windowHeight = windowWidth / (photoWidth / photoHeight);
				}
			else
				{
				// Photo is portrait
				windowHeight = screen.height - 300;
				windowWidth = windowHeight / (photoHeight / photoWidth);
				}
		}

		//Centre window allowing for generous borders
		windowLeft = (screen.width / 2) - ((windowWidth + 10) / 2);
		windowTop = (screen.height / 2) - ((windowHeight + 85) / 2);
		
		//Compile URL and features
		navigateURL = "photo_enlarge.aspx?filename=" + filename + "&photoWidth=" + photoWidth + "&photoHeight=" + photoHeight;
		features = "scrollbars=no, resizable=yes, left=" + windowLeft + ", top=" + windowTop + ", width=" + windowWidth + ", height=" + windowHeight;
		
		//Open window and focus on it
		page = window.open(navigateURL, "SELMECPhotoEnlargementPopup", features);
		page.focus();
	}
	
function messageLoaded()
	{
	getWindowSize();

	//Position loading photo message in centre
	document.message.style.left = (windowWidth / 2) - 40;
	document.message.style.top = (windowHeight / 2) - 40;
	
	//Make message visible
	document.message.style.visibility = "visible";
	}

function photoLoaded(e, photoWidth, photoHeight)
	{
	//Store the original photo size
	photoOriginalWidth = photoWidth;
	photoOriginalHeight = photoHeight;

	//Start zoomed out
	startZoom(e);
			
	//Make photo visible and message invisible
	document.photo.style.visibility = "visible";
	document.message.style.visibility = "hidden";
	}

function resizePhoto()
  {
	getWindowSize();
	zoomOut();
  }
    
function startZoom(e)
	{
	getWindowSize();
	getMousePosition(e);

	//Toggle zoom action
	if (zoomState == 0)
		{
		zoomIn();
		}
	else
		{
		zoomOut();
		}
	}

function zoomIn()
	{
	if (windowWidth <= photoOriginalWidth || windowHeight <= photoOriginalHeight)
		{
		//Zoom in to original size
		photoCurrentWidth = photoOriginalWidth;
		photoCurrentHeight = photoOriginalHeight;
		document.photo.style.width = photoCurrentWidth;
		document.photo.style.height = photoCurrentHeight;
		
		//Set UI
		document.photo.style.cursor = "move";
		document.photo.alt = "Click and drag to move, double-click to zoom out";
		document.photo.title = "Click and drag to move, double-click to zoom out";

		//Centre on selected part of photo
		photoCurrentX = - photoOriginalWidth * (mouseCurrentX / windowWidth) + (windowWidth / 2);
		photoCurrentY = - photoOriginalHeight * (mouseCurrentY / windowHeight) + (windowHeight / 2);
		document.photo.style.left = photoCurrentX;
		document.photo.style.top = photoCurrentY;
		
		checkBounds()

		zoomState = 1;
		}
	}

function zoomOut()
	{
	//Zoom out to window size
	photoCurrentX = 0;
	photoCurrentY = 0;
	
	//Size photo to correct aspect ratio
	if ((photoOriginalWidth / photoOriginalHeight) > (windowWidth / windowHeight))
	    {
	    //Landscape
	    photoCurrentWidth = windowWidth
	    photoCurrentHeight = photoCurrentWidth / (photoOriginalWidth / photoOriginalHeight);
	    photoCurrentY = (windowHeight - photoCurrentHeight) / 2;
	    }
	    else
	    {
	    //Portrait
		photoCurrentHeight = windowHeight
		photoCurrentWidth = photoCurrentHeight / (photoOriginalHeight / photoOriginalWidth);
		photoCurrentX = (windowWidth - photoCurrentWidth) / 2;
	    }

	document.photo.style.left = photoCurrentX;
	document.photo.style.top = photoCurrentY;
	
	document.photo.style.width = photoCurrentWidth;
	document.photo.style.height = photoCurrentHeight;
	
	if (windowWidth <= photoOriginalWidth || windowHeight <= photoOriginalHeight)
		{
		//Set UI
		document.photo.style.cursor = "pointer";
		document.photo.alt = "Double-click to zoom in";
		document.photo.title = "Double-click to zoom in";
		}
		
	zoomState = 0;
	}

function startMovePhoto(e)
	{
	getMousePosition(e);

	//Record starting position
	photoStartX = photoCurrentX;
	photoStartY = photoCurrentY;
	mouseStartX = mouseCurrentX;
	mouseStartY = mouseCurrentY;
	mouseButtonState = 1;
	}

function endMovePhoto(e)
	{
	mouseButtonState = 0;
	}

function movePhoto(e)
	{
	getWindowSize();
	getMousePosition(e);

	//Move photo only when left button down
	if (mouseButtonState == 1 && zoomState ==1)
		{
		photoCurrentX = photoStartX + (mouseCurrentX - mouseStartX);
		photoCurrentY = photoStartY + (mouseCurrentY - mouseStartY);
		document.photo.style.left = photoCurrentX;
		document.photo.style.top = photoCurrentY;

		checkBounds()
		}
	}
	
function checkBounds()
	{
	//Stop photo going off page
	if (photoCurrentX > 0)
		{
		photoCurrentX = 0;
		}
	if (photoCurrentY > 0)
		{
		photoCurrentY = 0;
		}
	if (photoCurrentX + photoCurrentWidth < windowWidth)
		{
		photoCurrentX = windowWidth - photoCurrentWidth;
		}
	if (photoCurrentY + photoCurrentHeight < windowHeight)
		{
		photoCurrentY = windowHeight - photoCurrentHeight;
		}
	if (windowWidth > photoCurrentWidth)
	    {
   		photoCurrentX = (windowWidth - photoCurrentWidth) / 2;
	    }
	if (windowHeight > photoCurrentHeight)
	    {
   		photoCurrentY =  (windowHeight - photoCurrentHeight) / 2;
	    }

	document.photo.style.left = photoCurrentX;
	document.photo.style.top = photoCurrentY;
	}
	
function getMousePosition(e)
	{
		if (!e) var e = window.event;
		
		if (e.pageX || e.pageY)
			{
			mouseCurrentX = e.pageX;
			mouseCurrentY = e.pageY;
			}
		else if (e.clientX || e.clientY)
			{
			mouseCurrentX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseCurrentY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
	}

function getWindowSize()
	{
	if (document.body.clientWidth)
		{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		}
	if (window.innerWidth)
		{
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
		}
	}