/* Expects a global variable "animation_zero_image". */

/*
return this._this or this
*/
function get_this(t)
{
   if (!t)
      throw "get_this error";
   if (t._this)
      return t._this;
   return t;
}

/**
 * Create an image frame.
 * @param src              Image location
 */
function cnImageFrame ( src )
{
   this.src = src;
   
   // make blip image
   this.tracker_blip_element = document.createElement("img");
   if ( ! this.tracker_blip_element )
      throw "Can't make tracker blip";
   with (this.tracker_blip_element)
   {
      src       = animation_zero_image;
      className = "waiting";
      alt       = "@";
   }
}
cnImageFrame.prototype = {
   /**
    * Image requested
    */
   requested:
      false,
   
   /**
    * Image loaded
    */
   loaded:
      false,
   
   /**
    * Image timed out / failed
    */
   timedout:
      false,
   
   /**
    * Target image source
    */
   src:
      "",
   
   /**
    * Parent cnImageFrameset
    */
   parent:
      null,
   
   /**
    * ID number in sequence (from parent)
    */
   num:
      0,
   
   /**
    * Register image with a parent
    */
   register: function ( parentSet, num )
   {
      this.parent = parentSet;
      this.num = num;
      
      // attatch blip element image
      this.parent.tracker_div_element.appendChild
            ( this.tracker_blip_element );
      this.tracker_blip_element._this = this;
      this.tracker_blip_element.onclick = this.blip_show_image;
   },
   
   /**
    * Loads the image
    */
   load_image: function ()
   {
      if ( this.requested )
         return;
      this.requested = true;
      
      try
      {
         this.ImageElement = document.createElement("img"); //new Image();
         this.ImageElement._this = this;
         
         if (this.ImageElement.addEventListener)
         {
            /*
            we'll just ASSUME this will work - there's no frelling way to test it
            */
            this.ImageElement.addEventListener("load", this.imageLoaded, false);
            this.ImageElement.addEventListener("error", this.imageError, false);
         }
         else
         {
            this.ImageElement.onload = this.imageLoaded;
            this.ImageElement.onerror = this.imageError;
         }
         
         // now load image :D
         this.ImageElement.src = this.src;
      }
      catch (e)
      {
         // pretend it loaded ok :D
         this.imageLoaded();
      }
   },
   
   /**
    * Sets the style class of the trackerblip image
    * @param class         class to set tracker blip to, or null
    * @return              new class
    */
   set_trackerblip_class: function ( classname )
   {
      if ( classname )
         this.tracker_blip_element.className = classname;
      
      return this.tracker_blip_element.className;
   },
   
   /**
    * Sets the trackerlip to selected or normal style classes
    * @param selected      True if currently selected image
    */
   select_trackerblip: function ( selected )
   {
      var classname = this.set_trackerblip_class(null);
      if (classname == "blip" || classname == "selected")
         this.set_trackerblip_class ( selected ? "selected" : "blip" );
   },
   
   
   
   /* PRIVATE */
   
   /**
    * new Image() created goes here
    */
   ImageElement:
      null,
   
   /**
    * IMG for tracker div
    */
   tracker_blip_element:
      null,
   
   
   
   /* CALLBACKS */
   
   /**
    * Callback for onclick
    * This is a response to ONCLICK so "this" is the blip image.
    */
   blip_show_image: function()
   {
      var _this = get_this(this);
      
      if (! _this.parent)
         return;
      
      _this.parent.animate_stop();
      _this.parent.currentFrame = _this.num;
      _this.parent.show_image( _this.num );
   },
   
   /**
    * Called on image load
    * This is a response to an event so "this" is the blip image.
    */
   imageLoaded: function ()
   {
      //"this" here is the image
      var _this = get_this(this);
      
      if (_this.loaded || _this.timedout)
         return;  //already loaded
      
      _this.loaded = true;
      if (_this.tracker_blip_element)
         _this.set_trackerblip_class("blip");

      // if waiting on images to load, notify
      if (_this.parent && _this.parent.is_waiting && _this.parent.currentFrame == _this.num)
         _this.parent.resume_from_waiting();
      
      // load next image if needed
      if (_this.parent)
         _this.parent.load_next_image();
   },
   
   /**
    * Called on image error
    * This is a response to an event so "this" is the blip image.
    */
   imageError: function ()
   {
      //"this" here is the image
      var _this = get_this(this);
      
      if (_this.loaded || _this.timedout)
         return;  //already loaded
      
      _this.timedout = true;
      if (_this.tracker_blip_element)
         _this.set_trackerblip_class("error");

      // load next image if needed
      if (_this.parent)
         _this.parent.load_next_image();
   }//,
   
};

