// define a namespaced Image object
__BEA.com.bea.portal.common.js.Common.set("__BEA.com.bea.portal.common.js.Image", new function() { })

/**
 * Configure timers and event handlers to scale the image down
 * to not more than 100x100 (in aspect).
 *
 * <p>
 * A timeout is configured to handle invalid images.
 * If the image cannot be loaded, it is replaced with
 * the default image.
 *
 * @param refid the id of a &lt;span> or &lt;div> for the &lt;img>
 * @param src
 * @param defaultsrc
 **/

__BEA.com.bea.portal.common.js.Image.scaleImageAsync = function(refid, href, src, defaultsrc) {
  try {
    var placeholder = document.getElementById(refid)

    // Put the async image properties in the placeholder

    placeholder.bea = new function() { }
    placeholder.bea.href = href
    placeholder.bea.src = src
    placeholder.bea.defaultsrc = defaultsrc
    placeholder.bea.tries = 0

    // Create a 0x0, invisible span to hold an <img> tag to
    // load the image "offscreen".
    // The span is also absolute-positioned so that the image
    // doesn't cause the text layout to change as the image loads.

    placeholder.innerHTML =
      "<span style='position: absolute; height: 0; width: 0; visibility: hidden;'>" +
      "<img id='" + refid + "Img' onload=\"return __BEA.com.bea.portal.common.js.Image.scaleImageAsyncOnload('" +
        refid + "')\" src='" +
        src + "'></span>"

    // Configure a timer so that, if the image doesn't load
    // in a reasonable time (5 sec.), then drop in the default image

    placeholder.bea.timeout = setTimeout("__BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout('" + refid + "')", 5000)
  }
  catch (err) {
    __BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout(refid)
  }
}

/**
 * Called when the image is loaded.
 *
 * <p>
 * If the image never loads, the timeout will trigger and the
 * default image will be dropped in.</p>
 **/

__BEA.com.bea.portal.common.js.Image.scaleImageAsyncOnload = function(refid) {
  try {
    var placeholder = document.getElementById(refid)
    placeholder.bea.img = document.getElementById(refid + "Img")

    // Configure an interval to wait for the image to *really*
    // load.  It turns out that most browsers call onload
    // before they've actually processed the image, so properties
    // like height and width are not available yet.
    // Furthermore, onreadystatuschange doesn't work here either.
    // Browsers are stupid.

    placeholder.bea.interval = setInterval("__BEA.com.bea.portal.common.js.Image.scaleImageAsyncInterval('" + refid + "')", 100)
  }
  catch (err) {
    __BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout(refid)
  }
}

__BEA.com.bea.portal.common.js.Image.scaleImageAsyncInterval = function(refid) {
  try {
    var placeholder = document.getElementById(refid)

    if ((placeholder.bea.tries ++) >= 50) {
      clearTimeout(placeholder.bea.timeout)
      clearInterval(placeholder.bea.interval)
      __BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout(refid)
      return
    }

    var img = placeholder.bea['img']
    if (img == undefined) {
        return
    }
    var height = img.height
    var width = img.width
    if ((height <= 0) && (width <= 0)) {
        return
    }

    clearTimeout(placeholder.bea.timeout)
    clearInterval(placeholder.bea.interval)

    var innerHTML = "<a href='#' onclick=\"" + placeholder.bea.href + "\"><img border='0' src=\"" + placeholder.bea.src + "\""
    if ((height > width) && (height > 100)) {
      innerHTML += " height=\"100\""
    }
    else if ((width > height) && (width > 100)) {
      innerHTML += " width=\"100\""
    }

    placeholder.innerHTML = innerHTML + "></a>"
  }
  catch (err) {
    __BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout(refid)
  }
}

__BEA.com.bea.portal.common.js.Image.scaleImageAsyncTimeout = function(refid) {
  try {
    var placeholder = document.getElementById(refid)

    if (placeholder.bea['interval'] != undefined) {
      cancelInterval(placeholder.bea.interval)
    }

    placeholder.innerHTML = "<a href='#' onclick=\"" + placeholder.bea.href + "\"><img border='0' src=\"" + placeholder.bea.defaultsrc + "\"></a>"
  }
  catch (err) {
  }
}
