<!-- $Revision: 1.6 $ -->

var maxWidth = 400;
var maxHeight = 400;

function zoom_out()
{
    Onload();
    posx = (pMainImage.eRezRight + pMainImage.eRezLeft) / 2.0;
    posy = (pMainImage.eRezBottom + pMainImage.eRezTop) / 2.0;
    doZoomOut(pMainImage, posx, posy);
    eRezUpdateImage(pMainImage);
}

function doZoomOut(img, posx, posy)
{
    xout = (img.eRezRight - img.eRezLeft) / 2.0;
    img.eRezLeft -= xout;
    if(img.eRezLeft < 0)
    {
	img.eRezRight -= img.eRezLeft;
	img.eRezLeft = 0.0;
    }
    img.eRezRight += xout;
    if(img.eRezRight > 1.0)
    {
	img.eRezLeft -= (img.eRezRight - 1.0);
	if(img.eRezLeft < 0.0)
	{
	    img.eRezLeft = 0.0;
	}
	img.eRezRight = 1.0;
    }
    yout = (img.eRezBottom - img.eRezTop) / 2.0;
    img.eRezTop -= yout;
    if(img.eRezTop < 0)
    {
	img.eRezBottom -= img.eRezTop;
	img.eRezTop = 0.0;
    }
    img.eRezBottom += xout;
    if(img.eRezBottom > 1.0)
    {
	img.eRezTop -= (img.eRezBottom - 1.0);
	if(img.eRezTop < 0.0)
	{
	    img.eRezTop = 0.0;
	}
	img.eRezBottom = 1.0;
    }
    eRezUpdateImage(img);
}

function zoom_in()
{
    Onload();
    posx = (pMainImage.eRezRight + pMainImage.eRezLeft) / 2.0;
    posy = (pMainImage.eRezBottom + pMainImage.eRezTop) / 2.0;
    doZoomIn(pMainImage, posx, posy);
    eRezUpdateImage(pMainImage);
}

function left()
{
    Onload();
    eRezPanLeft(pMainImage);
    eRezUpdateImage(pMainImage);
}

function up()
{
    Onload();
    eRezPanUp(pMainImage);
    eRezUpdateImage(pMainImage);
}

function down()
{
    Onload();
    eRezPanDown(pMainImage);
    eRezUpdateImage(pMainImage);
}

function right()
{
    Onload();
    eRezPanRight(pMainImage);
    eRezUpdateImage(pMainImage);
}

var pMainImage = null;

function Onload()
{
    pMainImage = document.images["eRezImg"];
    eRezPrepareImage(pMainImage);
}

function SwitchImage (ImageURL)
{
    Onload();
    pMainImage.src = ImageURL;
    pMainImage.eRezReady = null;
    eRezPrepareImage (pMainImage);
}

function Reset()
{
    Onload();
    pMainImage.eRezTop = 0;
    pMainImage.eRezBottom = 1;
    pMainImage.eRezLeft = 0;
    pMainImage.eRezRight = 1;
    eRezUpdateImage(pMainImage);
}

function doClick(evt, img)
{
	if(evt.button != 2)
	{
		if(eRezPrepareImage(img))
		{
			if(!handleEyecon(evt, img))
			{
				if(eRezAltKeyDown(evt))
				{
					eRezPan(evt, img);
					eRezUpdateImage(img);
				}
				else if(eRezShiftKeyDown(evt))
				{
					// eRezZoomOut(evt, img);
					posy = img.eRezTop + (eRezYPos2(evt, img) * (img.eRezBottom - img.eRezTop));
					posx = img.eRezLeft + (eRezXPos2(evt, img) * (img.eRezRight - img.eRezLeft));
					doZoomOut(img, posx, posy);
					eRezUpdateImage(img);
				}
				else if(!eRezCtrlKeyDown(evt))
				{
					posy = img.eRezTop + (eRezYPos2(evt, img) * (img.eRezBottom - img.eRezTop));
					posx = img.eRezLeft + (eRezXPos2(evt, img) * (img.eRezRight - img.eRezLeft));
					doZoomIn(img, posx, posy);
					eRezUpdateImage(img);
				}
			}
		}
	}
}

function doZoomIn(img, posx, posy)
{
	newheight = 0.75 * (img.eRezBottom - img.eRezTop);
	newwidth = 0.75 * (img.eRezRight - img.eRezLeft);
	
	if(img.width < maxWidth)
	{
		width = (newwidth *  maxWidth) / img.width;
		// posx -= (width - newwidth) / 2.0;
		newwidth = width;
	
		if((posx + (newwidth / 2.0))  > 1.0)
		{
			posx -= ((posx + (newwidth / 2.0))  - 1.0);
		}
	}
	
	if(img.height < maxHeight)
	{
		height = (newheight *  maxHeight) / img.height;
		// posy -= (height - newheight) / 2.0;
		newheight = height;
	
		if((posy + (newheight / 2.0))  > 1.0)
		{
			posy -= ((posy + (newheight / 2.0))  - 1.0);
		}
	}
	
	img.eRezTop = posy - (newheight  / 2.0);
	img.eRezLeft = posx - (newwidth / 2.0);
	
	if(img.eRezTop < 0.0)
	{
		img.eRezTop = 0.0;
	}
	if(img.eRezLeft  < 0.0)
	{
		img.eRezLeft  = 0.0;
	}
	img.eRezBottom = img.eRezTop + newheight;
	if(img.eRezBottom > 1.0)
	{
		img.eRezTop = 1.0 - newheight;
		img.eRezBottom = 1.0;
	}
	
	img.eRezRight = img.eRezLeft + newwidth;
	if(img.eRezRight  > 1.0)
	{
		img.eRezLeft = 1.0 - newwidth;
		img.eRezRight  = 1.0;
	}	

	if(eval(img.eRezTop ) < 0.0)
	{
		img.eRezTop = 0.0;
	}
	else if(eval(img.eRezTop ) > 1.0)
	{
		img.eRezTop = 1.0;
	}	

	if(img.eRezLeft < 0.0)
	{
		img.eRezLeft = 0.0;
	}
	else if(img.eRezLeft > 1.0)
	{
		img.eRezLeft = 1.0;
	}

	if(img.eRezBottom < 0.0)
	{
		img.erezBottom = 0.0;
	}
	else if(img.eRezBottom > 1.0)
	{
		img.eRezBottom = 1.0;
	}

	if(img.eRezRight < 0.0)
	{
		img.eRezRight = 0.0;
	}
	else if(img.eRezRight > 1.0)
	{
		img.eRezRight = 1.0;
	}
}
