<!--
// flightPlanner.js
// Original from http://mirror.bom.gov.au/products/IDR032.loop.shtml
//	(Australian Government Bureau of Meteorology web site www.bom.gov.au - with Permission)
//	(Many thanks to Clive Edington)
// 2005-02-08 Clive Edington, mouse-distance javascript.

// Usage:
// (1) Call 'launch()' once (e.g. from BODY onload="..") to start this code.
// (2) Assumes that variables have already been defined in the HTML page,
//     outside of this file, e.g.
//     debug = true  //(when you are doing debugging)
//     Km = nn;	// Standard 64km, 128km, 256km or 512km radar picture.
// -----------------------------------------------------------------

// 
// Microsoft vs. Netscape.  See below for safer definitions.
// If either of these are true, then the mouse-distances (cursor) shows.
// If both false, then we get simple loops, but no mouse.

debug = 0; // 0=silent, 1=Print debug msgs

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) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); 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];}
}

  isMS  = false;
  isNN4 = false;
  isNN6 = false;
  isMac = false;
  doMouse = false;
  //debug=1;
  
  if (debug) {
    document.writeln ("<br> Browser = " + navigator.appName );
    document.writeln ("<br> Version = " + navigator.appVersion);
  }
  browser_version = parseFloat(navigator.appVersion)
  if (!browser_version) browser_version = 0.
  //browser_version = 0.  // only do this during testing.
  if (debug) {
    document.writeln ("<br> extracted Browser version = " + browser_version );
  }

  if (navigator.appName.indexOf("Microsoft") != -1) {
    if ( browser_version >= 4.) isMS = true;
    // IE on Mac has different Mouse calculations.
    if ( navigator.appVersion.indexOf("Mac") != -1) isMac = true;
  }

  if (navigator.appName.indexOf("Netscape") != -1) {
    // Netscape 6 reports v5 !!!
    if  ( (browser_version >= 4.) && (browser_version < 5.) ) isNN4 = true;
    if  (  browser_version >= 5.                            ) isNN6 = true;
  }   

  if (navigator.appName.indexOf("Opera") != -1) {
    // Opera v7 is MS compatible for mouse distances.    
    if  (  browser_version >= 7. ) isMS = true;
  }   

  if (isMS || isNN4) doMouse = true;
  doMouse = true;

  // Do not show mouse if Km is not defined.
  if (Km<1) { doMouse = false }
  
  if (debug) {
    document.writeln ("<br> isNN4 = " + isNN4 + ", isNN6=" + isNN6)
    document.writeln ("<br> isMS = "  + isMS  + ", isMac=" + isMac)
    document.writeln ("<br> Km = " + Km);
    document.writeln ("<br> doMouse = " + doMouse);
    document.writeln ("<br> image Name = " + theImageNames[0]);
    document.writeln ("<br>")
  }

//==============================================================
 
//===> Load and initialize everything once page is downloaded 
//	(called from 'onLoad' in <BODY>)

function launch()
{
  if (doMouse)  startxy ();	// Start pointer-origin code.

}
 
//==> Empty function - used to deal with image buttons rather than HTML buttons
function func()
{
}
 
<!-- Below is the Radar moving cursor Pointer & Origin code.-->
<!-- 2000-07-28 Clive Edington, generalised the code to NN4.-->
<!-- Thanks to Alf West and the Radar Section for the original IE code.-->

  // Usage:
  // (1) Call 'startxy()' once (e.g. from BODY onload="..") to start this code.
  // (2) Assumes that these Image dependent variables have already been defined:
  //
  // GifFileName = "??.gif";
  // Km = nn;	// Standard 64km, 128km, 256km or 512km radar picture.
  // -----------------------------------------------------------------
  //
  // This code assumes a TABLE with a radar image and X, Y outputs, etc,
  // and then does the logic of updating X & Y whenever the mouse moves.

  // Compute some internal globals.
  var maxKm = Km-1;
  //  128km is .5 KmPerPixel,  512km is 2 KmPerPixel
  //  (Radar maps are always 512 pixels diameter, i.e. 256 pixels radius)
  //var KmPerPixel = Km/256; (now set in calling html)

 
  // Internal Global variables.
  var xx=0;
  var yy=0;
  var zz=0;
  var aa=0;
  var xKm=0;
  var yKm=0;
  var xKmOrigin=0;
  var yKmOrigin=0;

// startxy is called once after the BODY has been loaded.

//	It initalises x-y stuff and sets up mouse-event-callbacks.
//
function startxy ()
{
  // Setup mouse-move & mouse-click(down) callbacks.
  //if (isMS || isNN6) {
    document.body.onmousemove=move;
    document.body.onmousedown=down;
    //document.animation.src = GifFileName;
  //}
  if (isNN4  || isNN6) {
    document.captureEvents(Event.MOUSEMOVE);
    document.onMouseMove=move;
    document.captureEvents(Event.MOUSEDOWN);
    document.onMouseDown=down;
    //window.onresize = resize;  // Not needed because the loop will reload it.
    //document.animationlayer.document.animation.src = GifFileName;
  }
  resetOrigin ();
}

// Netscape-only (maybe Xterms only), reload the image after a resize.
function resize () {
      document.flightMapLayer.document.flightMap.src = GifFileName;
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function move(e)	// When the mouse moves, update the pointer boxes.
{
    //if (isMS || isNN6) {
	e = window.event;
        // X-Y relative to the container (i.e. the image).
        xKm=9999
        yKm=9999
        if (e.srcElement && e.srcElement.name) {
          if (e.srcElement.name == "flightMap") {
	    // 2003-03-11 reduce +2 to +1.
            //xPixels = window.event.offsetX-262+1
            //yPixels = window.event.offsetY-262+1
            xPixels = window.event.offsetX-ImgCentre+1
            yPixels = window.event.offsetY-ImgCentre+1
            // Mac IE forgets to adjust for the scroll bars.
            if (isMac) {
              xPixels += document.body.scrollLeft
              yPixels += document.body.scrollTop
            }
            xKm= xPixels*KmPerPixel
            yKm=-yPixels*KmPerPixel
          }
        } 
    //}
    //if (isNN4 || isNN6) {
    //    xKm=9999
    //    yKm=9999
    //    if (e.target.name) {
   //       if (e.target.name == "flightMap") {
            // Radar maps have 6-pixel borders, hence the centre is 256+6=262
            // xKm= (e.pageX-document.animationlayer.pageX-262+1)*KmPerPixel
            // yKm=-(e.pageY-document.animationlayer.pageY-262+1)*KmPerPixel
            // new version:-
   //         xKm= (e.pageX-document.flightMapLayer.pageX-ImgCentre+1)*KmPerPixel
   //         yKm=-(e.pageY-document.flightMapLayer.pageY-ImgCentre+1)*KmPerPixel
   //       }
   //     } 
    //}
    if (Math.abs(xKm)>maxKm || Math.abs(yKm)>maxKm) {
      //document.myForm.x.value="" 
      //document.myForm.y.value=""
      document.myForm.z.value="please move your"
      //document.myForm.a.value=""        
      document.myForm.t.value="mouse over"        
      document.myForm.p.value="the map"        
      //document.myForm.info1.value=""        
      //document.myForm.info2.value=""        
    }
    else {
      xx=xKm-xKmOrigin
      yy=yKm-yKmOrigin
      zz=Math.round(Math.sqrt(xx*xx+yy*yy)-.01)
      aa=450-Math.round(Math.atan2(yy,xx)*57.29)
	  //KmPerHour=Math.round(270*1.852) (set in calling html)
	  //CostPerHour=1350 (set in calling html)
	  tt=zz/KmPerHour
	  pp=tt*CostPerHour
      if (aa>359) { aa=aa-360 }
      if (zz<1)   { aa="0"    }
      xx=Math.round(xx) 
      yy=Math.round(yy)
      hh=Math.floor(tt+0.0001)
	  mm=(tt-hh)*60
      mm=Math.round(mm)
      pp=Math.round(pp)
      // Turn -0 into +0
      if (xx == 0) { xx=0; }
      if (yy == 0) { yy=0; }
      if (zz == 0) { zz=0; }
      if (tt == 0) { tt=0; }
      if (pp == 0) { pp=0; }
      //if (xx>=0) document.myForm.x.value = xx + ' km East'
      //else       document.myForm.x.value =-xx + ' km West'
      //if (yy>=0) document.myForm.y.value = yy + ' km North'
      //else       document.myForm.y.value =-yy + ' km South'
      document.myForm.z.value = 'Dist: ' + zz + ' (km)'
      //document.myForm.a.value = aa + ' Degrees'        
	  
      document.myForm.t.value = 'Time: ' + pad(hh,2) + ':' + pad(mm,2)
      document.myForm.p.value = 'Cost: ' + pp  + ' ($AUS)'  
      //document.myForm.info1.value = 'x/Km=' + Math.round(xKm) + '/' + Math.round(yKm) 
      //document.myForm.info2.value = 'cost per hour = $' + CostPerHour  
    }
}

function down()		// A left-click (mouse-down) to move the Origin. 
{
    // xKm & yKm are the distances from the centre of the radar image.
    // They were already computed in (mouse) move() above.
    // (or are zero at startup.)
    
    // Maybe set the new origin.
    if (Math.abs(xKm)<maxKm && Math.abs(yKm)<maxKm) {
        xKmOrigin=xKm
        yKmOrigin=yKm
    }
    //if (xKmOrigin>=0) document.offsets.xo.value =  Math.round(xKmOrigin) + ' km East' 
    //else              document.offsets.xo.value = -Math.round(xKmOrigin) + ' km West' 
    //if (yKmOrigin>=0) document.offsets.yo.value =  Math.round(yKmOrigin) + ' km North' 
    //else              document.offsets.yo.value = -Math.round(yKmOrigin) + ' km South' 
}

function resetOrigin () 
{
   xKm = 0;
   yKm = 0;
   down ();
}
 
// Empty function - used to deal with image buttons rather than HTML buttons
function xynullfunc()
{
}

//-->
