


/*
    Functions in this file depend on the following global variables being set
		externally.
		
			- globalWidth_internal
			- globalHeight_internal
			- imgWidth_internal
			- imgHeight_internal
			
		For example:
			var globalWidth_internal        = "8000";
			var globalHeight_internal       = "6000";
			var imgWidth_internal           = "400";
			var imgHeight_internal          = "400";
	
*/


// ------------------------------------------------------------------------
// GLOBAL DECLARATIONS
// ------------------------------------------------------------------------

// Global Variables
var fullAxsImgUrl;
var globalWidth_internal;
var globalHeight_internal;
var imgWidth_internal;
var imgHeight_internal;


var resized;
var resizedWidth;
var resizedHeight;



// Convenience variables
var globalWidthCenter;
var globalHeightCenter;
var imgWidthCenter;
var imgHeightCenter;

// Miscellaneous variables
var isN4 = (window.Event)? true : false ;



// Demo variables and default values
var maxRedFactor;
var redFactor;

var oldGlobalWidthCenter;
var oldGlobalHeightCenter;

var newGlobalWidthCenter;
var newGlobalHeightCenter;

var demoImg_x_click;
var demoImg_y_click;

var canPanLeft;
var canPanRight;
var canPanUp;
var canPanDown;
var canZoomIn;
var canZoomOut;

var zoomIn_onClick;
var zoomOut_onClick;

var eyespyImgHost ;



// ------------------------------------------------------------------------
// FUNCTION: initDemo
// ------------------------------------------------------------------------
function initDemo(imgName_param, globalWidth_param, globalHeight_param, imgWidth_param, imgHeight_param)
{
	
	// Set global variables
	globalWidth_internal 	= globalWidth_param;
	globalHeight_internal 	= globalHeight_param;
	imgWidth_internal 	= imgWidth_param;
	imgHeight_internal 	= imgHeight_param;
	
	fullAxsImgUrl = imgHostScript + "?getjpeg&" + imgName_param;
        fullAxsImgResizeUrl = imgHostScript + "?resizeimage&" + imgName_param;
        //allow for resize 
	// Reset global variables in case image has been changed
	globalWidthCenter  = Math.round(globalWidth_internal / 2);
	globalHeightCenter = Math.round(globalHeight_internal / 2);
	imgWidthCenter     = Math.round(imgWidth_internal / 2);
	imgHeightCenter    = Math.round(imgHeight_internal / 2);
	
        eyespyImgHost = '' ;
        if (imgHostScript.substring (0,4) == 'http') {
             var endOfHostName = imgHostScript.indexOf ('/', 8) ;
             if (buttonsPath.substring (0,4) != 'http') {
               eyespyImgHost = imgHostScript.substring (0, endOfHostName) ;
             }
        }
	
	// Reset all defaults
	zoomIn_onClick     = true;
	zoomOut_onClick    = false;
	
	demoImg_x_click = Math.round(imgWidth_internal / 2);
	demoImg_y_click = Math.round(imgHeight_internal / 2);
	
	oldGlobalWidthCenter  = Math.round(globalWidth_internal / 2);
	oldGlobalHeightCenter = Math.round(globalHeight_internal / 2);
	
	newGlobalWidthCenter 	= Math.round(globalWidth_internal / 2);
	newGlobalHeightCenter = Math.round(globalHeight_internal / 2);
	

	// Calculate an appropriate initial reduction factor.
	maxRedFactor  = 1024;  // Randomly picked large (very large) reduction factor to start at
	while ( ((globalWidth_internal / maxRedFactor) < imgWidth_internal) || ((globalHeight_internal / maxRedFactor) < imgHeight_internal) )
	{		
		maxRedFactor = maxRedFactor / 2;
	}
	

	if (maxRedFactor < 1)
	{
		maxRedFactor = 1;
	}
	 
	// Set redFactor to max soom out
	redFactor = maxRedFactor; 
	
	// Update canXXX variables
	updateCanXXXVars();
}




// ------------------------------------------------------------------------
// FUNCTION: resetDemo()
// ------------------------------------------------------------------------
function resetDemo(imgName_param, globalWidth_param, globalHeight_param, imgWidth_param, imgHeight_param)
{
        initDemo(imgName_param, globalWidth_param, globalHeight_param, imgWidth_param, imgHeight_param);
        document.images["demoImg"].src = thumbnailName;       // Load demo image
}





// ------------------------------------------------------------------------
// FUNCTION: demoZoomIn();
// ------------------------------------------------------------------------
function demoZoomIn()
{
	redFactor = redFactor / 2;
	if (redFactor < 1)
	{
		redFactor = 1;
	}

	updateCanXXXVars();

	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image

	
}
		
// ------------------------------------------------------------------------
// FUNCTION: demoZoomOut();
// ------------------------------------------------------------------------
function demoZoomOut()
{
        redFactor = redFactor * 2;
        updateCanXXXVars();
        document.images["demoImg"].src = getDemoImgUrl();       // Load demo image

	if (redFactor >= maxRedFactor)
        {
          resetDemo(imgName, imgWidth, imgHeight, closeupWidth, closeupHeight);
        }
        
}


// ------------------------------------------------------------------------
// FUNCTION: demoPanLeft
// ------------------------------------------------------------------------
function demoPanLeft()
{
	newGlobalWidthCenter  = oldGlobalWidthCenter  - (redFactor * (imgWidth_internal / 2));

	// Left boundary check
	if (newGlobalWidthCenter - ((imgWidth_internal / 2) * redFactor) < 0)
	{
		newGlobalWidthCenter = (imgWidth_internal / 2) * redFactor;
	}
	
	
	newGlobalWidthCenter = Math.round(newGlobalWidthCenter);

	updateCanXXXVars();
	
	oldGlobalWidthCenter = newGlobalWidthCenter;
	
	
	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image
}


// ------------------------------------------------------------------------
// FUNCTION: demoPanRight
// ------------------------------------------------------------------------
function demoPanRight()
{
	newGlobalWidthCenter  = oldGlobalWidthCenter  + (redFactor * (imgWidth_internal / 2));
	
	// Right boundary check
	if (newGlobalWidthCenter + ((imgWidth_internal * redFactor) / 2) > globalWidth_internal)
	{
		newGlobalWidthCenter = globalWidth_internal - ((imgWidth_internal * redFactor) / 2);
	}

	newGlobalWidthCenter = Math.round(newGlobalWidthCenter);

	updateCanXXXVars();
	
	oldGlobalWidthCenter = newGlobalWidthCenter;
	
	
	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image
}


// ------------------------------------------------------------------------
// FUNCTION: demoPanUp
// ------------------------------------------------------------------------
function demoPanUp()
{
	newGlobalHeightCenter  = oldGlobalHeightCenter - (redFactor * (imgHeight_internal / 2));

	// Top boundary check
	if (newGlobalHeightCenter - ((imgHeight_internal * redFactor) / 2) < 0)
	{
		newGlobalHeightCenter = (imgHeight_internal / 2) * redFactor;
	}
	
	newGlobalHeightCenter = Math.round(newGlobalHeightCenter);
	updateCanXXXVars();

	oldGlobalHeightCenter = newGlobalHeightCenter;
	
	
	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image
}

// ------------------------------------------------------------------------
// FUNCTION: demoPanDown
// ------------------------------------------------------------------------
function demoPanDown()
{
	newGlobalHeightCenter  = oldGlobalHeightCenter  + (redFactor * (imgHeight_internal / 2));

	// Bottom boundary check
	if (newGlobalHeightCenter + ((imgHeight_internal * redFactor) / 2) > globalHeight_internal)
	{
		newGlobalHeightCenter = globalHeight_internal  - (imgHeight_internal * redFactor) / 2;
	}
	
	newGlobalHeightCenter = Math.round(newGlobalHeightCenter);
	updateCanXXXVars();

	oldGlobalHeightCenter = newGlobalHeightCenter;
	
	
	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image
}


// ------------------------------------------------------------------------
// FUNCTION: updateCanXXXVars()
// ------------------------------------------------------------------------
function updateCanXXXVars()
{
	// Determine canZoomIn
	if (redFactor <= 1)
		canZoomIn = false;
	else
		canZoomIn = true;

	// Determine canZoomOut
	if (redFactor >= maxRedFactor)
		canZoomOut = false;
	else
		canZoomOut = true;

	// Determine can panLeft
	if (newGlobalWidthCenter - ((imgWidth_internal / 2) * redFactor) <= 1)
		canPanLeft = false;
	else
		canPanLeft = true;

	// Determine can panRight
	if (newGlobalWidthCenter + ((imgWidth_internal * redFactor) / 2) >= globalWidth_internal - 1)
		canPanRight = false;
	else
		canPanRight = true;

	// Determine can panUp
	if (newGlobalHeightCenter - ((imgHeight_internal * redFactor) / 2) <= 1)
		canPanUp = false;
	else
		canPanUp = true;

	// Determine can panDown
	if (newGlobalHeightCenter + ((imgHeight_internal * redFactor) / 2) >= globalHeight_internal - 1)
		canPanDown = false;
	else
		canPanDown = true;
}


// ------------------------------------------------------------------------
// FUNCTION: demoImgClick
// ------------------------------------------------------------------------
function demoImgClick(e)
{
	// Get the click coordinate
	
	if (isN4)
	{
		// Handle Netscape 6 and other browers that look like netscape 4, but don't work
		if (document.anchors["demoImgAnchor"].x == null)
		{
			// Default to center of image click.
			demoImg_x_click = Math.round(imgWidth_internal / 2);
			demoImg_x_click = Math.round(imgHeight_internal / 2);
		}
		else
		{
			// Netscape 4
			demoImg_x_click =  e.pageX - document.anchors["demoImgAnchor"].x;
			demoImg_y_click =  e.pageY - document.anchors["demoImgAnchor"].y;
		}

	}
	else
	{
		// Hopefully IE 4 or 5
		demoImg_x_click =  event.offsetX;
		demoImg_y_click =  event.offsetY;
	}
	
	// Uncomment to debug image click coordinate
	

        //alert("demoImg_x_click: " + demoImg_x_click + ", demoImg_y_click: " + demoImg_y_click + ", redFactor=" + redFactor);
	

	
	redFactorX = imgWidth / closeupWidth;
	redFactorY = imgHeight / closeupHeight;

	// X coordinate transformation
	if (resized != true)
	{
	  if (demoImg_x_click < imgWidthCenter)
	  {
		newGlobalWidthCenter = oldGlobalWidthCenter - ((imgWidthCenter - demoImg_x_click) * redFactor );
	  }
	  else
	  {
		newGlobalWidthCenter = oldGlobalWidthCenter + ((demoImg_x_click - imgWidthCenter) * redFactor);
	  }
	
	  // Y coordinate transformation
	  if (demoImg_y_click < imgHeightCenter )
	  {
		newGlobalHeightCenter = oldGlobalHeightCenter - ((imgHeightCenter - demoImg_y_click) * redFactor );
	  }
	  else
	  {
		newGlobalHeightCenter = oldGlobalHeightCenter + ((demoImg_y_click - imgHeightCenter) * redFactor);
	  }
	}
	else
	{
	  if (demoImg_x_click < imgWidthCenter)
	  {
		newGlobalWidthCenter = oldGlobalWidthCenter - ((imgWidthCenter - demoImg_x_click) * redFactorX );
	  }
	  else
	  {
		newGlobalWidthCenter = oldGlobalWidthCenter + ((demoImg_x_click - imgWidthCenter) * redFactorX);
	  }
	
	  // Y coordinate transformation
	  if (demoImg_y_click < imgHeightCenter )
	  {
		newGlobalHeightCenter = oldGlobalHeightCenter - ((imgHeightCenter - demoImg_y_click) * redFactorY );
	  }
	  else
	  {
		newGlobalHeightCenter = oldGlobalHeightCenter + ((demoImg_y_click - imgHeightCenter) * redFactorY);
	  }
	}
	


	// Calculate and check reduction factor if needed.
	// NOTE:  May need additional logic here for upper bounds checking of scale.
	if (resized != true)
	{
	  if (zoomIn_onClick == true)
	  {
		redFactor = (String)(redFactor / 2);
	  }

	  if (zoomOut_onClick == true)
	  {
		redFactor = (String)(redFactor * 2);
	  }
  
	  if (redFactor < 1)
	  {
		redFactor = 1; 
	  }
	}
	else
	  resized = false;
	
	// Boundary Checks
	// Right boundary check
	if (newGlobalWidthCenter + ((imgWidth_internal * redFactor) / 2) > globalWidth_internal)
	{
		newGlobalWidthCenter = globalWidth_internal - ((imgWidth_internal * redFactor) / 2);
	}

	// Left boundary check
	if (newGlobalWidthCenter - ((imgWidth_internal / 2) * redFactor) < 0)
	{
		newGlobalWidthCenter = (imgWidth_internal / 2) * redFactor;
	}

	// Bottom boundary check
	if (newGlobalHeightCenter + ((imgHeight_internal * redFactor) / 2) > globalHeight_internal)
	{
		newGlobalHeightCenter = globalHeight_internal  - (imgHeight_internal * redFactor) / 2;
	}

	// Top boundary check
	if (newGlobalHeightCenter - ((imgHeight_internal * redFactor) / 2) < 0)
	{
		newGlobalHeightCenter = (imgHeight_internal / 2) * redFactor;
	}

	// Set variables. Needed?
	newGlobalWidthCenter = Math.round (newGlobalWidthCenter) ;
	newGlobalHeightCenter = Math.round (newGlobalHeightCenter) ;
	oldGlobalWidthCenter = Math.round (newGlobalWidthCenter) ;
	oldGlobalHeightCenter = Math.round (newGlobalHeightCenter) ;
	
	updateCanXXXVars();

	
	document.images["demoImg"].src = getDemoImgUrl();	// Load demo image

return;
}



// ------------------------------------------------------------------------
// FUNCTION: getDemoImgUrl
// ------------------------------------------------------------------------
function getDemoImgUrl()
{	
	return fullAxsImgUrl +
							 "&" + globalWidth_internal +
							 "&" + globalHeight_internal + 
							 "&" + imgWidth_internal +
							 "&" + imgHeight_internal +
							 "&" + redFactor +
							 "?" + newGlobalWidthCenter +
							 "," + newGlobalHeightCenter;

}




// ------------------------------------------------------------------------
//FUNCTION: UpdateAllHomeButtons()
// ------------------------------------------------------------------------
function updateAllHomeBtns(buttonsPath)
{
	// Zoom In
	if (canZoomIn)
		document.images["btn_zoomIn"].src = eyespyImgHost + buttonsPath + "/zoomin.gif";
	else
		document.images["btn_zoomIn"].src = eyespyImgHost + buttonsPath + "/zoomin_f.gif";
	
	// Zoom Out
	if (canZoomOut)
		document.images["btn_zoomOut"].src = eyespyImgHost + buttonsPath + "/zoomout.gif";
	else
		document.images["btn_zoomOut"].src = eyespyImgHost + buttonsPath + "/zoomout_f.gif";

	// Pan Left
	if (canPanLeft)
		document.images["btn_panLeft"].src = eyespyImgHost + buttonsPath + "/panleft.gif";
	else
		document.images["btn_panLeft"].src = eyespyImgHost + buttonsPath + "/panleft_f.gif";
	
	// Pan Right
	if (canPanRight)
		document.images["btn_panRight"].src = eyespyImgHost + buttonsPath + "/panright.gif";
	else
		document.images["btn_panRight"].src = eyespyImgHost + buttonsPath + "/panright_f.gif";
	
	// Pan Up
	if (canPanUp)
		document.images["btn_panUp"].src = eyespyImgHost + buttonsPath + "/panup.gif";
	else
		document.images["btn_panUp"].src = eyespyImgHost + buttonsPath + "/panup_f.gif";
	
	// Pan Down
	if (canPanDown)
		document.images["btn_panDown"].src = eyespyImgHost + buttonsPath + "/pandown.gif";
	else
		document.images["btn_panDown"].src = eyespyImgHost + buttonsPath + "/pandown_f.gif";

        document.images["btn_Reset"].src = eyespyImgHost + buttonsPath + "/reset.gif";
}


// ------------------------------------------------------------------------
// FUNCTION: getResizedSize
// ------------------------------------------------------------------------
function getResizedSize()
{	
	var widthRatio = imgWidth / closeupWidth;
  	var heightRatio = imgHeight / closeupHeight;	
  	if (widthRatio > heightRatio)
		{
	 	resizedWidth = imgWidth / widthRatio;
	 	resizedHeight = imgHeight / widthRatio;
		}
  	else
		{
		resizedWidth = imgWidth / heightRatio;
	 	resizedHeight = imgHeight / heightRatio;
		}
}

// ------------------------------------------------------------------------
// FUNCTION: initeyespy
// ------------------------------------------------------------------------
function initeyespy() {
 	getResizedSize();
 	setresized();
  	initDemo(imgName, imgWidth, imgHeight, closeupWidth, closeupHeight);   
  	updateAllHomeBtns(buttonsPath);
}

// ------------------------------------------------------------------------
// FUNCTION: setresized
// ------------------------------------------------------------------------
function setresized() {
 	var difWidth = resizedWidth - closeupWidth;
	var difHeight = resizedHeight - closeupHeight;
	if (difWidth < 0)
		difWidth = difWidth * (-1);
	if (difHeight < 0)
		difHeight = difHeight * (-1);
	
	
	if (difWidth > 1 || difHeight > 1)
		resized = true;
	else 
		resized = false;
}


// ------------------------------------------------------------------------
// FUNCTION: eyespyviewer
// ------------------------------------------------------------------------
function eyespyviewer() {
 
  var tableWidth = closeupWidth - 0 + 1 ;
  var tableHeight = closeupHeight ;
      
 
    if (typeof thumbnailName == 'undefined')
       thumbnailName = imgHostScript + '?resizeimage&' + imgName +  '&' + 'wsx=' + closeupWidth + '&' + 'wsy=' + closeupHeight ;
 
  var popupUrl = imgHostScript + '?launchpad&' + imgName + '&' + closeupWidth + '&' + closeupHeight;
 
  document.writeln (' <DIV align="right">');
  document.writeln ('  <TABLE width="' + 199 + '" border="0" cellspacing="0" cellpadding="0">') ;
  document.writeln ('    <TR>') ;
  document.writeln ('      <TD  align="center" colspan=7 width="' + 199 + '" height="' + tableHeight + '" border="0"><A name="demoImgAnchor" href="' + popupUrl + '" target="_blank" onClick=" demoImgClick(event); updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + thumbnailName +  '" name="demoImg" width="' + closeupWidth + '" height="' + closeupHeight + '"  border="0"></A></TD>') ;
  
  document.writeln ('    </TR><tr>') ;
  document.writeln ('      <td align="center"><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoZoomIn();    updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/zoomin.gif"   width="22" height="24" alt="Zoom In"   name="btn_zoomIn"   border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoZoomOut();   updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/zoomout.gif"  width="21" height="24" alt="Zoom Out"  name="btn_zoomOut"  border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoPanLeft() ;  updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/panleft.gif"  width="21" height="24" alt="Pan Left"  name="btn_panLeft"  border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoPanRight();  updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/panright.gif" width="20" height="24" alt="Pan Right" name="btn_panRight" border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoPanUp();     updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/panup.gif"    width="21" height="24" alt="Pan Up"    name="btn_panUp"    border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_blank" onClick="demoPanDown();   updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/pandown.gif"  width="26" height="24" alt="Pan Down"  name="btn_panDown"  border="0"></A><A') ;
  document.writeln ('        href="' + popupUrl + '" target="_top" onClick=" setresized(); resetDemo(imgName, imgWidth, imgHeight, closeupWidth, closeupHeight); updateAllHomeBtns(buttonsPath); return false;"><IMG src="' + buttonsPath + '/reset.gif" width="68" height="24" alt="Reset" name="btn_Reset" border="0"></a></td></TR>') ;
  document.writeln ('</table>') ;
  document.writeln (' </DIV>');
  
}
<!--
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) { //v3.0
  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); 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];}
}
//-->
