// **************************************************************************
//	Hacked from Martin Krolik -- http://krolik.net/PIV.jsp -- Version 3.4 
// **************************************************************************
// 2005-04-10 DRH	Initial
// 2005-04-12 DRH	Added keyboard scroll speed control checkKey(); 
// 2005-04-19	DRH	removed maxtravel (it was vestigal).
// 2005-04-27 DRH	Reduced autoStopTimer to 73 seconds.

var mytimer, xdist, ydist, myobject, mypframe, myimage;
 
var xoffset = 0;   
var yoffset = 0;
var xviewport = 591;   // size of view window ... must be less than size of image
var yviewport = 116;
var defaulttravel = 1; // how many pixels at a time we move .... choppy'ness VS. slowness
var defaulttimeout = 50; // how many milliseconds between moves
var borderheight = 0; // actually should be derived from NORTH or SOUTH BORDER image, but isn't
var borderwidth = 0; // actualy should be derived from EAST or WEST BORDER image, but isn't
var panoramaScrolling = true;
var savedOnKeyUp;

var initialized = false;
var loaded = false;

var vstate = false;
var startx = 0;
var starty = 0;
var autoStopTimer;

function initpiv() {
	var x = xGetElementById('PlaceHolderDiv');
	if (!x) return;
  myobject = xGetElementById("dimg");					// DIV containing the IMG
  outerimgdiv = xGetElementById("dimgOuter");	// DIV wrapping myobject
  mypframe = xGetElementById("mypicframe");
  myimage = xGetElementById("ione");
  loaded = true;
  positionpiv();
	autoStopTimer = setTimeout("togglePanoramaScrolling()", 73 * 1000);			// about enough for one round trip.
}

function positionpiv() {
	var x = xGetElementById('PlaceHolderDiv');
	if (!x) return;
  xoffset = xPageX('PlaceHolderDiv') + borderwidth;
  yoffset = xPageY('PlaceHolderDiv') + borderheight;
	if (window.opera) yoffset -= 10;
  if (loaded) {
    xMoveTo(outerimgdiv,xoffset,yoffset);
    xMoveTo(mypframe,xoffset-borderwidth,yoffset-borderheight+1);
  }
		
  xShow(outerimgdiv);
  xShow(myobject);
  xShow(mypframe);
  xClip(mypframe,0,(xviewport+borderwidth+borderwidth),(yviewport+borderheight+borderheight),0);
  xClip(mypframe,0,(xviewport+borderwidth+borderwidth),(yviewport+borderheight+borderheight),0);

  // lines below seem to add wierd blank pixel lines to top and bottom
  // of scroll in ie5
  xdist = 0;
  ydist = 0;
  shiftover();
  quitmoving();
  xdist=1;
  ydist=0;
  shiftover();  
}	

function shiftover() {
	if (!panoramaScrolling) return;
	if (loaded) {
  	quitmoving();
		/*if (!panoramaScrolling) return; 2005-04-19 DRH Removed*/
	  if ( ((xTop(myobject) - ydist ) >= 0) && (ydist < 0) ) {
			ydist *= -1;
	  } else if ( ((xTop(myobject) - ydist ) <= ( yviewport - xHeight(myimage) ) ) && (ydist > 0) ) {
			ydist *= -1; 
	  }
	  if ( ((xLeft(myobject) - xdist ) >= 0) && (xdist < 0) ) {
			xdist *= -1;
	  } else if ( ((xLeft(myobject) - xdist ) <= ( xviewport - xWidth(myimage) ) ) && (xdist > 0) ) {
			xdist *= -1;
	  }
	  xMoveTo(myobject, xLeft(myobject) - xdist, xTop(myobject) - ydist);
	
	  if (document.layers) {
			xClip(myobject,null, xviewport - xLeft(myobject) ,null,null);
	  }
	  initialized == true;
		if ((xdist != 0) || (ydist != 0)) {
			mytimer = setTimeout("shiftover()", defaulttimeout);
	  }
	}
}
function quitmoving() {
  if (mytimer) {
    clearTimeout(mytimer);
  }
}

function togglePanoramaScrolling() {
	if (autoStopTimer) clearTimeout(autoStopTimer);
	panoramaScrolling = !panoramaScrolling;
	if (panoramaScrolling) {
		myimage.title = "Click to stop scrolling."
		shiftover();
	} else {
		myimage.title = "Click to start scrolling."
	}
}

function checkKey( e) {
	if (!e) e = window.event;
	if (!e.shiftKey) return;
	if (autoStopTimer) clearTimeout(autoStopTimer);
	switch (e.keyCode) {
		case 65:						// (a)bort moving
			togglePanoramaScrolling();
			return;
		case 68:						// (d)irection change
			xdist *= -1;
			break;
		case 70:						// (f)aster
			xdist *= 2;
			break;
		case 82:						// (r)eset
			if (xdist < 0) {
				xdist = defaulttravel * -1;
			} else {
				xdist = defaulttravel;
			}
			break;
		case 83:						// (s)lower
			xdist /= 2;
			break;
	}
	xdist = Math.floor( xdist);
	if (xdist == 0) {
		xdist = defaulttravel;
	}
	quitmoving();
	shiftover();
}
function panoramaKeysOn() {
	savedOnKeyUp = document.onkeyup;
	document.onkeyup = checkKey;
}
function panoramaKeysOff() {
	document.onkeyup = savedOnKeyUp;
}

panoramaKeysOn();