SwapImage = function(name){

  this.name = name;

  this.images = {};

  this.restoresrc = null;

  this.nest = null;

}

SwapImage.prototype.add = function(state,src){

  this.images[state] = new Image();

  this.images[state].src = src;

}

SwapImage.prototype.swap = function(state){

  var img = this.getImage();

  this.restoresrc = img.src;

  img.src = this.images[state].src;

}

SwapImage.prototype.restore = function(){

  if(this.restoresrc){

    this.getImage().src = this.restoresrc;

    this.restoresrc = null;

  }

}

SwapImage.prototype.getImage = function(){// Private

  if(document.layers && this.nest == null) this.nest = this.getLayer();

  if(document.layers && this.nest != false){

    return this.nest.document.images[this.name];

  }else{

    return document.images[this.name];

  }

}

SwapImage.prototype.getLayer = function(root){// Private

  var i, k, layer, found = false;

  if(!root) root = window;

  for(i = 0; i < root.document.layers.length; i++){

    layer = root.document.layers[i];

    for(k = 0; k < layer.document.images.length; k++){

      if(layer.document.images[k].name == this.name){

        return layer;

      }

    }

    if(layer.document.layers.length) found = this.getLayer(layer);

    if(found) return found;

  }

  return false;

}