/**
 * Change the source of an image to the value specified by its
 * swap_src property.
 *
 * This function is intended to be used as an event handler.  It
 * accepts the triggering event as a single parameter and will use it
 * to locate the image to modify.
 *
 * @param {Event} event  The event triggering the action
 */
function swapImage(event) {
  if (!event) return;
  
  var img = Event.findElement(event, 'img');
  if (img && (!img.tagName)) img = Event.findElement(event, 'input');
  if (img && (!img.tagName)) img = Event.findElement(event, 'span');
  if (!img) return;
  if (!img.swap_src) return;

  if (img.tagName.toUpperCase() == 'SPAN') {
    // this indicates a span in place of an image for alpha transparency support
    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.swap_src+"', sizingMethod='scale')";
  } else {
    img.src = img.swap_src;
  }
}

/**
 * Restores the source of an image to the value specified by its
 * restore_src property.  This function is intended to be the opposite
 * of swapImage().
 *
 * Like swapImage(), this function is intended to be used as an event
 * handler.  It accepts the triggering event as a single parameter and
 * will use it to locate the image to modify.
 *
 * @param {Event} event  The event triggering the action
 */
function restoreImage(event) {
  if (!event) return;
  
  var img = Event.findElement(event, 'img');
  if (img && (!img.tagName)) img = Event.findElement(event, 'input');
  if (img && (!img.tagName)) img = Event.findElement(event, 'span');
  if (!img) return;
  if (!img.restore_src) return;
  
  if (img.tagName.toUpperCase() == 'SPAN') {
    // this indicates a span in place of an image for alpha transparency support
    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.restore_src+"', sizingMethod='scale')";
  } else {
    img.src = img.restore_src;
  }
}

/**
 * For IE version 5.5-6, fakes support of 32 bit PNG alpha
 * transparency by converting the image to span with a background
 * image (applied using a filter).
 *
 * @param img  The image element or id
 */
function png32Support(img) {
  // get the image
  if (!img) return;
  img = $(img);
  if (img.tagName.toUpperCase() != 'IMG') return;
  
  // IE only
  if (!document.all) return;
  
  // versions 5.5 - 6 only
  var ieVersion = parseFloat(navigator.appVersion.split("MSIE")[1]);
  if ((ieVersion < 5.5) || (ieVersion >= 7.0)) return;
  
  var opts = {};
  if (img.id)        opts['id'] = img.id;
  if (img.className) opts['class'] = img.className;
  if (img.title)     opts['id'] = img.title;
  opts['style'] = "width: "+img.width+"px; height: "+img.height+"px; display: inline-block;" + img.style.cssText +
    "; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"', sizingMethod='scale');";
  
  var html = "<span";
  for (var attr in opts) {
    html += " "+attr+"=\""+opts[attr]+"\"";
  }
  html += "></span>";
  
  img.outerHTML = html;
}
