// Utility functions
// December 2007 : Javascript
// Tim Surtell @ Clario

// Setup variables
var DoShortcut = false;
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 CheckShortcuts()
	{
	if (window.event)
		{
		if (window.event.keyCode == 49 && DoShortcut == true)
			{
			location.href = PageBase + "admin.aspx";
			}
			
		DoShortcut = false;

		if (window.event.keyCode == 96)
			{
			DoShortcut = true;
			}
		}
	}
//************************************************************************************************************
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 = PageBase + "photo_enlarge.aspx?filename=" + filename + "&photoWidth=" + photoWidth + "&photoHeight=" + photoHeight;
		features = "scrollbars=no, resizable=yes, left=" + windowLeft + ", top=" + windowTop + ", width=" + (windowWidth + 38) + ", height=" + (windowHeight + 48);
		
		//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) - 8;
	document.message.style.top = (windowHeight / 2) - 23;
	
	//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;
		}
	}
