<!-- Javascript: display mouse coordinates -->

// Image object information
// Location in page
var imageXPos = 8;
var imageYPos = 8;
// Location of Left, Right, Top and Bottom of the map region (in the image)
var viewer = "image_holder";


//==> The following subroutines are used for Pointer, Map Coordinates and Origin fields

function move(e)	// When the mouse moves, update the pointer boxes.
{
   //ensure that the page is completely loaded and then run getXkm and getYkm, check that those fns are defined
  if ((typeof(getXpixel) != "undefined") && (typeof(getYpixel) != "undefined"))
   {
   		updateDisplay(e);

   }
}

// startxy is called once after the BODY has been loaded.
// It initalises x-y stuff and sets up mouse-event callbacks.
//
function startxy ()
{
   if (isMS)
   {
      //reposition function is called if window is resized
     	document.body.onmousemove=move;
   }
   
   else
   {
      
      // attaching event listeners to the viewing area
   		var loopArea = document.getElementById(viewer);
		loopArea.addEventListener("mousemove", move, true);
   }
}

function getYpixel(e)
{
   if (isMS || isOpera)
   {
      e = window.event;
      // X-Y relative to the container (i.e. the image).
      if (e.srcElement && e.srcElement.id) {
         if (e.srcElement.parentNode.id == viewer) {
            var bugTop = 0;
            yPixels = (e.offsetY + e.srcElement.offsetTop + 1);
            // Mac IE forgets to adjust for the scroll bars.
            if (isMac) {
               yPixels += document.body.scrollTop;
            }
         } else return -1;
      } else return -1;
   }
   
   else
   {
      var element = document.getElementById(viewer).childNodes[0];
      var distanceY = 0;
      
      var offsetTop = element.offsetTop;
      var offsetHeight = element.offsetHeight;
      
      while(element.offsetParent)
      {
         distanceY += element.offsetTop;
         element = element.offsetParent;
      }
      
      if (e.currentTarget.id == viewer)
      {
         if(isGecko || isOpera || isSafari)
            yVal = (e.pageY-distanceY);
         
         //take care e.clientY moves with scrolling - you don't want scrolling to have an effect
         else {
            yVal = ((e.clientY+document.body.scrollTop)-offsetTop);}
         if (yVal < offsetHeight && yVal >= 0 )
           	yPixels=yVal;
      }
   }
   
   return yPixels;
}

function getXpixel(e)
{
   if (isMS || isOpera)  //If we are using the MS or Opera clients.
   {
      e = window.event;
      // X-Y relative to the container (i.e. the image).
      if (e.srcElement && e.srcElement.id) {
         if (e.srcElement.parentNode.id == viewer) {
         		width = e.srcElement.width+1;
            xPixels = (e.offsetX + 1 );// bugLeft) + (e.srcElement.offsetLeft + bugLeft);
            // Mac IE forgets to adjust for the scroll bars.
            if (isMac) {
               xPixels += document.body.scrollLeft;
            } 
         } else return -1;
      } else return -1;
   }
   
   else
   {
      var element = document.getElementById(viewer).childNodes[0];
      var distanceX = 0;
      
      var offsetLeft = element.offsetLeft;
      var offsetWidth = element.offsetWidth;
      
      while(element.offsetParent)
      {
         distanceX += element.offsetLeft;
         element = element.offsetParent;
      }
      
      var xVal;
      
      if (e.currentTarget.id == viewer)
      {
         if(isGecko || isOpera || isSafari)
         {
            xVal=(e.pageX-distanceX);
         }
         else{
            xVal=((e.clientX+document.body.scrollLeft)-offsetLeft);
			}
      }
      if ((xVal>=0) && (xVal < offsetWidth))
         xPixels = xVal;
   }
   
   
   return xPixels;
}


// update the mouse coordinates display
function updateDisplay(event) {
	var XFixDeg; // To hold the X value
	var YFixDeg; // To hold the Y value

	testX = getXpixel(event);
	testY = getYpixel(event); 
	
	image = document.getElementById(viewer).childNodes[0];
  	coord = document.getElementById("coordinates");
	
	XFixDeg=convertXPos(image,testX);
	YFixDeg=convertYPos(image,testY);
	
	if (XFixDeg == "" || YFixDeg == ""){  //If we're outside the image boundary...
		document.coordinates.latitude.value="";
    	document.coordinates.longitude.value="";
		document.coordinates.latitudedeg.value="";
		document.coordinates.longitudedeg.value=""; //...display nothing in the windows.
	}
	
	else{ //But if we're not, show the coords.
	
		document.coordinates.latitude.value=XFixDeg;
    	document.coordinates.longitude.value=YFixDeg;
	
		var wordX=XFixDeg.split(" "); //Rather than going through everything again, just grab the numbers from the string
		var wordY=YFixDeg.split(" ");
	
		document.coordinates.latitudedeg.value="+" + Math.round(100*(parseFloat(wordX[0]) + (wordX[2]*60)/3600))/100;  //This horrible line is the decimal conversion.  Math.round, how I hate you.
    	document.coordinates.longitudedeg.value="-" + Math.round(100*(parseFloat(wordY[0]) + (wordY[2]*60)/3600))/100;

	}
	
}


function convertXPos(image,xpos) {
    attrs = image.attributes;
	
	Xpix1=attrs['Xpix1'].value;
	Xpix2=attrs['Xpix2'].value;
	XLeftPos=attrs['XLeftPos'].value;
	XRightPos=attrs['XRightPos'].value;
	
	if ((xpos < XLeftPos) || (xpos > XRightPos)) {
    //  Not in the actual map area of the image.
    // return the out of range string
	  return "";
  }
	

	Xdeg1=parseFloat(attrs['Xdeg1'].value);
	Xdeg2=parseFloat(attrs['Xdeg2'].value);

	pixXWidth = Xpix2-Xpix1;
	degXWidth = Xdeg2-Xdeg1;
	// Otherwise convert the value using the know coordinate info.
	degX = ((xpos-1 - Xpix1) / pixXWidth * degXWidth) + Xdeg1;
	
	return degrees(degX) + " E";
}

function convertYPos(image,ypos) {
    attrs = image.attributes;
	
	Ypix1=attrs['Ypix1'].value;
	Ypix2=attrs['Ypix2'].value;
	YTopPos=attrs['YTopPos'].value;
	YBottomPos=attrs['YBottomPos'].value;
	
	
	if ((ypos < YTopPos) || (ypos > YBottomPos)) {
    //  Not in the actual map area of the image.
    // return the out of range string
	  return "";
  }
	
	
	Ydeg1=parseFloat(attrs['Ydeg1'].value);
	Ydeg2=parseFloat(attrs['Ydeg2'].value);
	
	pixYWidth = Ypix2-Ypix1;
	degYWidth = Ydeg2-Ydeg1;
	// Otherwise convert the value using the know coordinate info.
	degY = ((ypos-1 - Ypix1) / pixYWidth * degYWidth) + Ydeg1;
	
	return degrees(degY) + " S";
}


function degrees(dec_val) {
    degs = Math.floor(dec_val);
    mins = Math.round((dec_val - degs) * 60);
    if (mins == 60) {
        // If we are rounding up to 60 mins then we are rounding up to the next degree.
        mins = 0;
        degs++;
    }
    return degs + " deg " + mins + " mins"; 
}

function dec_degrees(degs, mins) {
    return degs + mins/60;
}
