function ImageManager(span) {
  var mySelf = this

  // Superclass Constructor
  top.glbDict.makeWidgetManager(this,span)

  mySelf.applyResize = function() {
    if ( mySelf.span.getAttribute('heightunit') == '%' ) {
      mySelf.div.style.height = parseInt(mySelf.div.ownerDocument.body.clientHeight * parseFloat(mySelf.span.style.height) * .01 ) + 'px'
    }
    if ( mySelf.span.getAttribute('widthunit') == '%' ) {
      mySelf.div.style.width = parseInt(mySelf.div.ownerDocument.body.clientWidth * parseFloat(mySelf.span.style.width) * .01 ) + 'px'
    }

    mySelf.paint()
    // Send a re-size event if the client cares about this
    if (mySelf.eventIsRegistered('resize')) {
      mySelf.doNotifyResize()
    }
  }

  mySelf.doNotifyResize = function() {
    var width = parseInt( mySelf.div.style.width )
    var height = parseInt( mySelf.div.style.height )
    if (! top.glbDict.postIsBusy) {
      // We allow a resize event to be sent even when the image is disabled
      top.glbDict.queueServerEvent(mySelf.span,'_resize', false , [ width , height ]  )
    }
  }

  mySelf.paint = function() {
    // Redo the border in case it changed
    mySelf.div.style.border = mySelf.span.getAttribute('frameborder')
    mySelf.div.style.position = 'relative'
    mySelf.div.style.backgroundColor = mySelf.span.getAttribute('bgcolor')

    // Don't bother painting ANYTHING unless the 'src' attribute is set
    if ( mySelf.div.childNodes[0].getAttribute('src') != '' ) {
      top.glbDict.innerHTML(mySelf.div, '<IMG src="' + mySelf.div.childNodes[0].getAttribute('src') + '"></IMG>' )
      mySelf.child = mySelf.div.getElementsByTagName('img')[0]
      mySelf.adjustEvents()
    }
    else {
      return
    }

    // Set overflow based on 'scroll' setting
    if (mySelf.span.getAttribute('scroll') == 'true') {
      mySelf.div.style.overflow = 'auto'
    }
    else {
      mySelf.div.style.overflow = 'hidden'
    }

    var borderwidth = parseInt(mySelf.div.style.borderWidth) 

    // Obtain the source image size and available container size attributes
    // in handy variables.
    var sourcewidth = parseInt(mySelf.span.getAttribute('sourcewidth'))
    var sourceheight = parseInt(mySelf.span.getAttribute('sourceheight'))
    var divwidth = parseInt(mySelf.div.style.width) - borderwidth * 2
    var divheight = parseInt(mySelf.div.style.height) - borderwidth * 2


    // If we're NOT allowed to stretch, then we MUST consider the original
    // 'source' size of the image
    if (mySelf.span.getAttribute('stretch') == 'false') { 
      // If we can't stretch, we will want to size our image to ACTUAL
      var imgwidth = sourcewidth 
      var imgheight = sourceheight
    }
    else {  // we ARE allowed to stretch.  
      // If we DO NOT have to enforce the aspect ratio, then use ALL available
      // size.
      if ( mySelf.span.getAttribute('enforceaspect') == 'false' ) {
        var imgwidth = divwidth
        var imgheight = divheight
      }
      else { // We DO care about the aspect ratio
        // Get the maximum scale to fit source file size into allowable div size,
        // and set image size to that

        // Unfortunately we're not going to be able to pull this off if we
        // don't have source dimensions from the server.  In this case,
        // just use available space.
        if ( sourcewidth == 0 ) sourcewidth = divwidth
        if ( sourceheight == 0 ) sourceheight = divheight


        var maxScaleW = divwidth / sourcewidth
        var maxScaleH = divheight / sourceheight
        var maxScale = Math.min( maxScaleW , maxScaleH )
        var imgwidth = maxScale * sourcewidth
        var imgheight = maxScale * sourceheight
      }
    }

    // Set the image's size
    mySelf.child.style.width = imgwidth + 'px'
    mySelf.child.style.height = imgheight + 'px'

    // Center the image inside the div, accordingly
    var itop = Math.max( ( divheight - imgheight ) / 2 , borderwidth )
    var ileft = Math.max( ( divwidth - imgwidth ) / 2 , borderwidth )
    mySelf.child.style.top = itop + 'px'
    mySelf.child.style.width = imgwidth + 'px'
    mySelf.child.style.height = imgheight + 'px'
    mySelf.child.style.left = ileft + 'px'
    mySelf.child.style.position = 'absolute'
    mySelf.child.style.border = mySelf.span.getAttribute('border')
    mySelf.child.style.backgrounColor = mySelf.span.getAttribute('bgcolor')
    mySelf.child.title = decodeURIComponent(mySelf.span.getAttribute('tooltip'))

    // Set the CLIP region of the image if overflow hidden (for Mozilla.  IE Ignores)
    if (mySelf.span.getAttribute('scroll') == 'false') {
      var clipStr = 'rect(0px,'
          clipStr += (divwidth + borderwidth * 2) + 'px,'
          clipStr += (divheight + borderwidth * 2) + 'px,'
          clipStr += '0px)'
      mySelf.child.style.clip = clipStr
    }


  }

  mySelf.attrChange = function(attrName,attrVal) {
    // Deal with change in the 'disabled' property
    if (attrName == 'disabled') {
      if (attrVal != 'false') {
        mySelf.enable(0)
      }
      else mySelf.enable(1)
    }

    if (attrName == 'srvevents') { // Server has changed event handling
      mySelf.adjustEvents()
      return
    }

    if (attrName == 'tooltip') {
      mySelf.child.title = decodeURIComponent(attrVal)
    }

    // Deal with change to aspect ratio or stretch settings
    mySelf.paint()
  }

  mySelf.adjustEvents = function() {

    if (mySelf.eventIsRegistered('mouseover')) {
      f_hdlr = function(ev) {
        mySelf.sendEvent('mouseover','')
      }
      mySelf.child.onmouseover = f_hdlr
    }

    if (mySelf.eventIsRegistered('mouseout')) {
      f_hdlr = function(ev) {
        mySelf.sendEvent('mouseout','')
      }
      mySelf.child.onmouseout = f_hdlr
    }

    if (mySelf.eventIsRegistered('onclick')) {
      f_hdlr = function(ev) {
        mySelf.sendEvent('onclick','')
      }
      top.glbDict.addEvent( mySelf.child,'click' , f_hdlr )
    }

  }

  mySelf.sendEvent = function(evName,data) {
    if ( mySelf.isEnabled() ) {
        top.glbDict.queueServerEvent(mySelf.span,evName, false , data )
    }
  }

  mySelf.initialize = function() {
    mySelf.div = mySelf.span.getElementsByTagName('div')[0]
    mySelf.paint()
  }

  return mySelf
}

top.glbDict.uwWidgetFactory.registerWidgetClass('Image',ImageManager)

