 //
   // Internal Controls
   //
   var playing = false;
   var cur_idx;
   var thisForm;
   var image_cache = Array(stop_idx - start_idx);
   var image_page_idx = 0;
   
   //
   // Initialize the viewer
   //
   function init(imageForm) {
      thisForm = imageForm;
      
      //
      // Make sure we have our variables
      //
      var required = Array( "base_url", "game", "image_dir", "image_prefix", "image_suffix",
                            "start_idx", "stop_idx" );
      for (var ctr = 0; ctr < required.length; ctr++) {
         var cmd = "var ok = (typeof(" + required[ctr] + ") != \"undefined\");";         
         eval(cmd);
         if (!ok) {
            var error = "Missing variable: " + required[ctr];
            document.getElementById("showing").innerHTML = "<B>ERROR:</B> " + error;
            alert(error);
            return;
         }
      } // FOR

      cur_idx = start_idx;
      display(cur_idx);
      controlStop();
   }
   
   //
   // Display an image
   //
   function display(idx) {
      //
      // First look to see if we have a cache for this
      //
      var cache_idx = idx - start_idx;
      if (typeof(image_cache[cache_idx]) == "undefined") {
         //
         // Load this image but then call our preloader
         //
         image_cache[cache_idx] = new Image();
         image_cache[cache_idx].onload = loadCache;
         image_cache[cache_idx].src = generateURL(idx);
      }
      //
      // Display the image from the cache
      //
      thisForm._control.src = image_cache[cache_idx].src;
      //
      // Update the now showing window
      //
      var temp = "Image #" + (cache_idx + 1) + " of " + ((stop_idx - start_idx) + 1);
      document.getElementById("showing").innerHTML = temp;
   }
   //
   // Generate the url for the image
   //
   function generateURL(idx) {
      //
      // Leading Zeros?
      //
      var formatted_idx = idx;
      if (leading_zeros > 0) {
         formatted_idx = '' + formatted_idx;
         while (formatted_idx.length < 3) formatted_idx = '0' + formatted_idx;
      }
      return (base_url + image_dir + "/" + image_prefix + formatted_idx + image_suffix);
   } 
   //
   // Preload images
   //
   function loadCache() {
      //
      // Preload as much as we were told we can out from the
      // current position
      //
      var idx = cur_idx + 1;
      var cnt = ((idx + preload) < stop_idx ? (idx + preload) : stop_idx);
      for (var ctr = idx; ctr < cnt; ctr++) {
         var cache_idx = ctr - start_idx;
         //
         // If we already have a cache, skip
         //
         if (typeof(image_cache[cache_idx]) != "undefined") continue;         
         //
         // Load cache
         //
         image_cache[cache_idx] = new Image();
         image_cache[cache_idx].src = generateURL(ctr);
      } // FOR
   }
   //
   // Play
   //
   function controlPlay() {
      playing = true;
      thisForm.stop.disabled = false;
      thisForm.play.disabled = true;
      setTimeout("_play()", speed * 1000);
   }
   //
   // Callback method for play button
   //
   function _play() {
      //
      // Keep doing this until we stop
      //
      if (playing) {
         controlNext(false);
         //
         // Stop if we've hit the end and we're not looping
         //
         if (cur_idx == stop_idx && !loop) {
            controlStop();
         } else {
            setTimeout("_play()", speed * 1000);
         }
      }
   }
   //
   // Reset
   //
   function controlReset(idx) {
      controlStop();
      cur_idx = idx;
      display(cur_idx);
   }
   //
   // Stop
   //
   function controlStop() {
      playing = false;
      thisForm.stop.disabled = true;
      thisForm.play.disabled = false;
   }
   
   //
   // Next
   //
   function controlNext(force_stop) {
      if (force_stop) controlStop();
      cur_idx++;
      if (cur_idx > stop_idx) cur_idx = start_idx;
      display(cur_idx);
   }
   //
   // Prev
   //
   function controlPrev(force_stop) {
      if (force_stop) controlStop();
      cur_idx--;
      if (cur_idx < start_idx) cur_idx = stop_idx;
      display(cur_idx);
   }
