<!-- radar.loop.v02.js -->
<!-- Below is the Radar Loop code plus cursor Pointer & Origin code.-->
<!-- 010507 Clive Edington, Better logic for browser version vs actions.-->
<!-- 001207 Clive Edington, No html here, just js.  Allow No images.-->
<!-- 000728 Clive Edington, generalised the code for www.bom.gov.au-->
<!-- 000728 Mike Fewings, Modified for radar.strikeone.net.au and satellite pics-->
<!-- Thanks to Alf West and the Radar Section for the original code.-->

<!--
// Usage:
// (1) Call 'launch()' once (e.g. from BODY onload="..") to start this code.
// (2) Assumes that these Image dependent variables have already been defined:
//
// Km = nn;    // Standard 64km, 128km, 256km or 512km radar picture.
// nImages = n;
// theImageNames = new Array();
// theImageNames[0] = "filename1.gif";
// theImageNames[1] = "filename2.gif";
// ...etc...
// -----------------------------------------------------------------

// If there are some images, loop them.
// else when no images are available, exit now.
//    Assume that the enclosing html will provide a message.

// 
// 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.
  isMS = false;
  isNN = false;
  isNN6 = false;
  isCursor = false;
  var d = window.document;
  if (navigator.appName.indexOf("Microsoft") != -1) {
    if (parseFloat(navigator.appVersion) >= 4.) isMS = true;
  }   
  if (navigator.appName.indexOf("Netscape") != -1) {
    // Netscape 6 reports v5 !!!
    if  ( (parseFloat(navigator.appVersion) >= 4.) && (parseFloat(navigator.appVersion) < 5.) ) isNN = true;
    if  ( (parseFloat(navigator.appVersion) >= 5.) && (parseFloat(navigator.appVersion) < 6.) ) isNN6 = true;
  }   

//document.writeln ("<br> Browser = " + navigator.appName );
//document.writeln ("<br> Version = " + navigator.appVersion);
//document.writeln ("<br> isNN = " + isNN)
//document.writeln ("<br> isNN6 = " + isNN6)
//document.writeln ("<br> isMS = " + isMS)
////document.writeln ("<br> isCursor = " + isCursor);
//document.writeln ("<br>")

if (nImages>0) { 

  var doc;    // Holds a pointer to MS or NN document (see launch).
  
// Now the general Looping code.
//
//============================================================
//                >> jsImagePlayer 1.0 <<
//            for Netscape3.0+, September 1996
//============================================================
//                  by (c)BASTaRT 1996
//             Praha, Czech Republic, Europe
//
// feel free to copy and use as long as the credits are given
//          by having this header in the code
//
//          contact: xholecko@sgi.felk.cvut.cz
//          http://sgi.felk.cvut.cz/~xholecko
//
//============================================================
// Thanx to Karel & Martin for beta testing and suggestions!
//============================================================
//
//     modified by D. Watson and A. Earnhart (CIRA/CSU), 7/30/97
//     and Greg Thompson (NCAR/RAP) Dec. 11 1997

// step 1: define the images
//        See "theImageNames" set above.
 
//
// step 2: define variables used to control images
//

image_href = "";
first_image = 0;
last_image = nImages-1;

//
// step 3: define dimensions of image (would be nice if this were interactively done)
//         Presently these ARE NOT used below. See step 9
//

animation_height  = 521;
animation_width  = 640;
 
//**************************************************************************
 
//=== THE CODE STARTS HERE - no need to change anything below ===
 
//=== global variables ====

theImages = new Array();      //holds the images
imageNum = new Array();       //keeps track of which images to omit from loop
normal_delay = 300;
delay = normal_delay;         //delay between frames in 1/100 seconds
delay_max = 645;
delay_min = 20;
delay_step = 25;
speed_steps = 25;
delay_step = Math.floor((delay_max - delay_min )/ speed_steps);
thisCurrentSpeed = speed_steps-Math.floor(delay/delay_step);
dwell_multipler = 3;
dwell_step = 1;
end_dwell_multipler   = dwell_multipler;
start_dwell_multipler = dwell_multipler - 1;

current_image = first_image;     //number of the current image
timeID = null;
radarStatus = 0;                      // 0-stopped, 1-playing
play_mode = 0;                   // 0-normal, 1-loop, 2-sweep
size_valid = 0;
 
}  // (end of if there are images to loop)

//==============================================================
//== All previous statements are performed as the page loads. ==
//== The following functions are also defined at this time.   ==
//==============================================================
 
//===> Load and initialize everything once page is downloaded (called from 'onLoad' in <BODY>)

function launch()
{
  d.getElementById('xoffset').innerHTML ='&nbsp;'; 
  d.getElementById('yoffset').innerHTML ='Select an area on the scan'; 
  d.getElementById('zoffset').innerHTML ='with your mouse to view much'; 
  d.getElementById('aoffset').innerHTML ='better lightning resolution';
  d.getElementById('currentSpeed').innerHTML ="("+thisCurrentSpeed+"/"+speed_steps+")";
  if (nImages==0)  return;

  
   //display image number
   ld = new Date(parseInt(theImageTimes[nImages-1]))
   // format hours for am pm
    var ampm="am";
   if (ld.getHours()>=12||ld.getHours()==0) {
        hours=Math.abs(ld.getHours()-12);
        if(hours==0) {hours=12;}
        if (ld.getHours()!=0) {
            ampm="pm";
        }
    }
    else {
        hours=ld.getHours();
    }

   
   d.getElementById("lasttime").innerHTML = " "+hours+":"+(ld.getMinutes()<10?"0"+ld.getMinutes():ld.getMinutes())+" "+ampm+"";



  doc = document;
  d.getElementById('currentSpeed').innerHTML ="("+thisCurrentSpeed+"/"+speed_steps+")";
  if (isNN) doc = document.animationlayer.document;

  // If there is only 1 image, show it, but dont start the loop.
  if (nImages==1) {
    current_image = 0;
    theImages[current_image] = new Image(animation_width,animation_height);
    theImages[current_image].src = image_href + theImageNames[current_image];
    imageNum[current_image] = true;   // pretend it is ready.
    display_current_image();
    return;
  }
    
  
  //
  // step 5: construct filenames for all images 
  //
  for (var i = first_image; i <= last_image; i++)
  {
    current_image = i;
    theImages[current_image] = new Image(animation_width,animation_height);
    theImages[current_image].src = image_href + theImageNames[current_image];
    imageNum[current_image] = false;  // image is not ready yet.
  }
  current_image = first_image;
  imageNum[current_image] = true;   // pretend it is ready.
 
  // this needs to be done to set the right mode when the page is manually reloaded
  change_mode (1);
  fwd();
}
 
function display_slider()
{
    stop();
    current_image = document.control_form.frame_nr.value-1;
    display_current_image();
}

function display_current_image()
{
   //display image onto screen
   doc.animation.src = theImages[current_image].src;
   //display image number
   document.control_form.frame_nr.value = current_image+1;
   document.control_form.frame_nr.value = current_image+1;
   sd = new Date(theImageTimes[current_image])
    //alert(scan_date.getDate() );
   // format hours for am pm
    var ampm="am";
   if (sd.getHours()>=12||sd.getHours()==0) {
        hours=Math.abs(sd.getHours()-12);
        if(hours==0) {hours=12;}
        if (sd.getHours()!=0) {
            ampm="pm";
        }
    }
    else {
        hours=sd.getHours();
    }
   positionSliderImage(false,1);
   d.getElementById("localtime").innerHTML = "<strong> "+hours+":"+(sd.getMinutes()<10?"0"+sd.getMinutes():sd.getMinutes())+" "+ampm+"</strong> ";

//alert(scan_date.getHours() );


   d.getElementById("loopnumber").innerHTML = "<strong>"+eval(current_image+1)+"</strong>";
   //localtime
   //loopnumber
}
 
//===> Stop the animation

function stop()
{
//== cancel animation (timeID holds the expression which calls the fwd or bkwd function) ==
  if (radarStatus == 1) {
    clearTimeout (timeID);
  }
  radarStatus = 0;
  return;
}

//===> Display animation in fwd direction in either loop or sweep mode

function animate_fwd()
{
   if (nImages<=1) return;
   current_image++;                      //increment image number
 
  //== check if current image has exceeded loop bound ==
  if (current_image > last_image) {
    if (play_mode == 1) {              //fwd loop mode - skip to first image
      current_image = first_image;
    }
    if (play_mode == 2) {              //sweep mode - change directions (go bkwd)
      current_image = last_image;
      animate_rev();
      return;
    }
  }
 
  //== check to ensure that current image has not been deselected from the loop ==
  //== if it has, then find the next image that hasn't been ==
  while (imageNum[current_image] == false) {
    if (theImages[current_image].complete) {
        imageNum[current_image] = true;
        break;
    }
    current_image++;
    if (current_image > last_image) {
      if (play_mode == 1)
        current_image = first_image;
      if (play_mode == 2) {
        current_image = last_image;
        animate_rev();
        return;
      }
    }
  }
 
  display_current_image();

  delay_time = delay;
  if (current_image == first_image)  delay_time = start_dwell_multipler*delay;
  if (current_image == last_image)   delay_time =   end_dwell_multipler*delay;
 
  //== call "animate_fwd()" again after a set time (delay_time) has elapsed ==
  timeID = setTimeout("animate_fwd()", delay_time);
}
 
 
//===> Display animation in reverse direction

function animate_rev()
{
  if (nImages<=1) return;
  current_image--;                      //decrement image number
 
  //== check if image number is before lower loop bound ==
  if (current_image < first_image) {
    if (play_mode == 1) {               //rev loop mode - skip to last image
       current_image = last_image;
    }
    if (play_mode == 2) {
      current_image = first_image;     //sweep mode - change directions (go fwd)
      animate_fwd();
      return;
    }
  }
 
  //== check to ensure that current image has not been deselected from the loop ==
  //== if it has, then find the next image that hasn't been ==
  while (imageNum[current_image] == false) {
    if (theImages[current_image].complete) {
        imageNum[current_image] = true;
        break;
    }
    current_image--;
    if (current_image < first_image) {
      if (play_mode == 1)
        current_image = last_image;
      if (play_mode == 2) {
        current_image = first_image;
        animate_fwd();
        return;
      }
    }
  }
  
  display_current_image();

  delay_time = delay;
  if (current_image == first_image)  delay_time = start_dwell_multipler*delay;
  if (current_image == last_image)   delay_time =   end_dwell_multipler*delay;
 
  //== call "animate_rev()" again after a set amount of time (delay_time) has elapsed ==
  timeID = setTimeout("animate_rev()", delay_time);
}
 
 
//===> Changes playing speed by adding to or substracting from the delay between frames

function change_speed(dv)
{
  delay+=dv;
  //== check to ensure max and min delay constraints have not been crossed ==
  if(delay > delay_max) delay = delay_max;
  if(delay < delay_min) delay = delay_min;
  thisCurrentSpeed = speed_steps-Math.floor(delay/delay_step);
  d.getElementById('currentSpeed').innerHTML ="("+thisCurrentSpeed+"/"+speed_steps+")";
}
 
//===> functions that changed the dwell rates.

function change_end_dwell(dv) {
  end_dwell_multipler+=dv;
  if ( end_dwell_multipler < 1 ) end_dwell_multipler = 0;
}
 
function change_start_dwell(dv) {
  start_dwell_multipler+=dv;
  if ( start_dwell_multipler < 1 ) start_dwell_multipler = 0;
}
 
//===> Increment to next image

function incrementImage()
{
  var number;
  if (nImages<=1) return;
  stop();
  current_image++;
  number = current_image;

  //== if image is last in loop, increment to first image ==
  if (number > last_image) number = first_image;

  //== check to ensure that image has not been deselected from loop ==
  while (imageNum[number] == false) {
    if (theImages[number].complete) {
        imageNum[number] = true;
        break;
    }
    number++;
   if (number > last_image) number = first_image;
  }
 
  current_image = number;
  display_current_image();
}
 
//===> Decrement to next image

function decrementImage()
{
  var number;
  if (nImages<=1) return;
  stop();
  current_image--;
  number = current_image;
 
  //== if image is first in loop, decrement to last image ==
  if (number < first_image) number = last_image;
 
  //== check to ensure that image has not been deselected from loop ==
  while (imageNum[number] == false) {
    if (theImages[number].complete) {
        imageNum[number] = true;
        break;
    }
    number--;
   if (number < first_image) number = last_image;
  }
 
  current_image = number;
  display_current_image();
}

// captures a key stroke of forwards and backwards and moves the animation forwards or backwards

function stepThrough(ev)
{
    if (!document) {
        return false;
    }
    //alert(ev.type);
    if (!(ev.ctrlKey || ev.altKey)) {
        switch (ev.keyCode) {
            case 37: // KEY left
                decrementImage();
            break;
            case 39: // KEY right
                incrementImage();
            break;
            case 190: // KEY '.' or '>'
                change_speed(-delay_step);
            break;
            case 188: // KEY ',' or '<'
                change_speed(delay_step);
            break;
            default:
            return false;
        }
    }

}
//===> "Play forward"

function fwd()
{
  stop();
  radarStatus = 1;
  play_mode = 1;
  animate_fwd();
}
 
//===> "Play reverse"

function rrev()
{
  stop();
  radarStatus = 1;
  play_mode = 1;
  animate_rev();
}

//===> "play sweep"

function sweep() {
  stop();
  radarStatus = 1;
  play_mode = 2;
  animate_fwd();
}
 
//===> Change play mode (normal, loop, swing)

function change_mode(mode)
{
   play_mode = mode;
}
 
//===> Check selection status of image in animation loop
function checkImage(radarStatus,i)
{
  if (radarStatus == true)
    imageNum[i] = false;
  else imageNum[i] = true;
}
 
//==> Empty function - used to deal with image buttons rather than HTML buttons
function func()
{
}
 

// Netscape-only (maybe Xterms only), reload the image after a resize.
function resize () {
      document.animationlayer.document.animation.src = GifFileName;
}



 
// Empty function - used to deal with image buttons rather than HTML buttons
function xynullfunc()
{
}

// some extra functionality placed in hre by chop 20040227

function radarScanOpen (radarID) {
    // this number is now in milli seconds. devide it by 1000
    currentSeconds = theImageTimes[current_image]/1000

    // set the radar back 1 hour and forward 1.5 hours from the time of the click on the sat photo
    // this takes into account the delay of placing the sat photo on the server

    // forward1_5hours = (timeSatphotoOnServer) + (1.5 hour of seconds) - (24 minutes of seconds)
    forward1_5hours=currentSeconds+5400-1440;

    // forward1hour = (timeSatphotoOnServer) - (1- hour of seconds) + (36 minutes of seconds)
    back1hour=currentSeconds-2160;


    newWin = window.open('?page=loops.main&radar='+radarID+'&numberofImages=&dateStart='+back1hour+'&dateFinish='+forward1_5hours,radarID);
    setTimeout("history.go(0)",200); 
}


function addEvent(el, evname, func) {
    if (el.attachEvent) { // IE
        el.attachEvent("on" + evname, func);
    } else if (el.addEventListener) { // Gecko / W3C
        el.addEventListener(evname, func, true);
    } else {
        el["on" + evname] = func;
    }
}

// add keystroke listeners to the document
addEvent(document, "keydown", stepThrough);
//addEvent(document, "keypress", stepThrough);
//addEvent(document, "mousedown", Calendar._checkCalendar);


function startLRmouseMove ()
{
    //stopxy ();
  if (isMS || isNN6) {
    lastX=0;
    document.body.onmousemove=leftRight;
    document.body.onmousedown=stopLRmouseMove;
  }
  if (isNN) {
    document.captureEvents(Event.MOUSEMOVE);
    document.onMouseMove=leftRight;
    document.captureEvents(Event.MOUSEMOVE);
    document.onMouseDown=stopLRmouseMove;
  }
}

function stopLRmouseMove ()
{
  if (isMS || isNN6) {
    document.body.onmousemove=null;
    document.body.onmousedown=null;
  }
  if (isNN) {
    document.captureEvents(null);
    document.onMouseMove=null;
    document.captureEvents(null);
    document.onMouseDown=null;
  }
}

function leftRight(e)   // When the mouse moves, update the pointer boxes.
{
    if (isMS) {xCoord=event.x} else if (isNN || isNN6) {xCoord=e.pageX;}else{xCoord=0;}

    if(xCoord>lastX+15) { if(current_image+1<=last_image){incrementImage();}lastX=xCoord;}

    else if(xCoord<lastX-15) { if(current_image-1>=first_image){decrementImage();}lastX=xCoord;}
}





//-->

