/********************checkCoords**********************************
	used for ensuring pan/click coordinates don't return an image
	outside the outer boundries of the original image
**************************************************************************/
function checkCoords()
{
	var CropWidth, CropHeight;
	
	CropWidth = g_ImageWidth / aryZoom[g_CurrentZoomIndex];
	CropHeight = g_ImageHeight / aryZoom[g_CurrentZoomIndex];
	
	if (g_CurrentX < 0)
		g_CurrentX = 0;
	if(g_CurrentY < 0)
		g_CurrentY = 0;
	if (g_CurrentX + CropWidth > g_ImageWidth)
		g_CurrentX = g_ImageWidth - CropWidth;
	if (g_CurrentY + CropHeight > g_ImageHeight)
		g_CurrentY = g_ImageHeight - CropHeight;
}
/*********************swapMainImg*****************************************
	Used for changing out the main image
**************************************************************************/
function swapMainImg(intX, intY)
{
	var tImg = getImgObj("mainImg");
	g_CurrentX = intX;
	g_CurrentY = intY;
	
	// make sure the returned image isn't outside of boundries
	checkCoords();

	mrlArgs = "img=" +  g_zoomImg + "&outputX=" + g_ImageWidth + "&outputY=" + g_ImageHeight + "&level=" +  aryZoom[g_CurrentZoomIndex] + "&x=" + g_CurrentX + "&y=" + g_CurrentY + "&type=jpg";
	// change main image
	tImg.src = g_mrlPath + mrlArgs;
}

/*********************zoomIn********************************************
Used for changing the zoom level and calling swapMainImg. This function
is called when the main image is clicked.
**************************************************************************/
function zoomIn(x, y, NewZoomIndex)
{
	//if x and/or y are < 0 then they will be calculated based on current x and y position
	
	var CroppedSize;
	var CenterPos;
	
	if (NewZoomIndex >= aryZoom.length)
	{
		//alert message notify user of max zoom
		alert("You have reached the maximum zoom level.");
	}
	else if (NewZoomIndex < 0)
	{
		//alert message notify user of min zoom
		alert("You have reached the minimum zoom level.");
	}
	else
	{
		if (x < 0)
			x = GetNewPosition(g_ImageWidth, g_CurrentX, NewZoomIndex)
		if (y < 0)
			y = GetNewPosition(g_ImageHeight, g_CurrentY, NewZoomIndex)
	
		g_CurrentZoomIndex = NewZoomIndex;
		
		swapMainImg(x, y);
	}
}

function ZoomClick(CursorX, CursorY)
{
	//cursorPageX + getDocScrollLeft() - layerX, cursorPageY + getDocScrollTop() - layerY
	var CenterX, CenterY;
	var x, y;
	var CroppedSize;
	var NewZoomIndex;
	
	NewZoomIndex = g_CurrentZoomIndex + 1;


	CenterX = (CursorX + getDocScrollLeft() - layerX + (g_CurrentX * aryZoom[g_CurrentZoomIndex])) / aryZoom[g_CurrentZoomIndex];
	CenterY = (CursorY + getDocScrollTop() - layerY + (g_CurrentY * aryZoom[g_CurrentZoomIndex])) / aryZoom[g_CurrentZoomIndex];

	CroppedSize = g_ImageWidth / aryZoom[NewZoomIndex];
	x = CenterX - (CroppedSize / 2);
	if (x < 0)
		x = 0;

	CroppedSize = g_ImageHeight / aryZoom[NewZoomIndex];
	y = CenterY - (CroppedSize / 2);
	if (y < 0)
		y = 0;

	zoomIn(x, y, NewZoomIndex)
}

function GetNewPosition(ImageSize, CurrentPos, NewZoomIndex)
{
	//gets new x or y position for the new zoom level
	//ImageSize = g_ImageWidth or g_ImageHeight
	//CurrentPos = g_CurrentX or g_CurrentY
	var CroppedSize;
	
	CroppedSize = ImageSize / aryZoom[g_CurrentZoomIndex];
	CenterPos = CurrentPos + (CroppedSize / 2);
	
	CroppedSize = ImageSize / aryZoom[NewZoomIndex];
	return CenterPos - (CroppedSize / 2);
}

/************************zoomImage************************************
used for zooming in and out of image. called when the "+" and "-"
are clicked on the page
**************************************************************************/
function zoomImage(strDirection)
{
	switch (strDirection)
	{
		case "IN":
			zoomIn(-1, -1, g_CurrentZoomIndex + 1);
			break;
		default: //OUT
			zoomIn(-1, -1, g_CurrentZoomIndex - 1);
			break;
	}
}

/************************panImage***************************************
used to pan image. Called when clicking the arrows on the zoom
window
**************************************************************************/
function panImage(strDirection)
{
	var iCellSize = 50;
	var x, y;

	x = g_CurrentX;
	y = g_CurrentY;
	
	switch (strDirection)
	{
		case "UP":
			y -= iCellSize;
			break;
		case "DOWN":
			y += iCellSize;
			break;
		case "RIGHT":
			x += iCellSize;
			break;
		case "LEFT":
			x -= iCellSize;
			break;
	}

	swapMainImg(x, y);
}


/**************************restore***************************************
restore to the original image
**************************************************************************/
function restoreImage()
{
	zoomIn(0, 0, g_DefaultZoomIndex);
}

function closeWindow() 
{
		window.close();
}

