// spotlight.js
// Richard A. DeVenezia, Sep 2003
// http://www.devenezia.com
//

spot.onColor = '#DDDD99'
spot.offColor ='#FFFFFF'

//--------------------------------------------------------------
function spot (element) {
  var re = new RegExp ("(.*)#(.*)")
  var arr = re.exec (element.href)
  var pid = arr[2]

  po = document.getElementById('p'+pid)

  if (!po) return

  spot.set  (po,spot.onColor)            // set an eye grabbing color, not to garish now!
  spot.fade (po,spot.offColor,40,1250 )  // fade it out in 2.5 seconds
}

//--------------------------------------------------------------
spot.set = function (element,color)
{
  if ( element.transition )
    element.transition.doStop()

  element.style.backgroundColor = color
}

//--------------------------------------------------------------
spot.fade = function (element,color,steps,duration)
{
  element.transition = new transition (element, color, steps, duration)
  element.transition.doStart()
}

//--------------------------------------------------------------
transition = function (obj, color, steps, duration)
{
  this.obj  = obj      // element whose background will change
  this.from = hexColorString (obj.style.backgroundColor).substr(1)
  this.to   = hexColorString (color).substr(1)
  this.step = 0        // current step

  if (!steps || !duration) {
    this.steps = 20;       // defaults, 20 steps lasting 1 second
    this.interval = 50;
  }
  else
  {
    this.steps = steps               // number of steps to take
    this.interval = duration / steps // time between steps (ms)
  }
}

//--------------------------------------------------------------
transition.prototype.doStart = function ()
{
  var thisObj = this
  this.timerId = setInterval ( function () { thisObj.doStep() }, this.interval )
}

//--------------------------------------------------------------
transition.prototype.doStop = function ()
{
  clearInterval (this.timerId)
  this.obj.transition = null
}

//--------------------------------------------------------------
transition.prototype.doStep = function () {
  var from  = this.from
  var   to  = this.to
  var step  = this.step
  var steps = this.steps

  var r0 = parseInt (from.substr(0,2),16)
  var g0 = parseInt (from.substr(2,2),16)
  var b0 = parseInt (from.substr(4,2),16)

  var r1 = parseInt (  to.substr(0,2),16)
  var g1 = parseInt (  to.substr(2,2),16)
  var b1 = parseInt (  to.substr(4,2),16)

  var r = Math.floor (r0 * ((steps-step)/steps) + r1 * (step/steps))
  var g = Math.floor (g0 * ((steps-step)/steps) + g1 * (step/steps))
  var b = Math.floor (b0 * ((steps-step)/steps) + b1 * (step/steps))

  this.obj.style.backgroundColor = hexColorString (r,g,b)

  this.step ++

  if ( this.step > this.steps )
    this.doStop()
}

//--------------------------------------------------------------
function hexColorString (r,g,b) {
  // if only one argument presume it's a color specifier that
  // needs to be returned as #rgb, other construct #rgb from
  // r,g,b arguments (presumed to be numbers)

  if (arguments.length == 1) {
    var reColorHex = /#([a-f0-9]){2}([a-f0-9]){2}([a-f0-9]){2}/i ;

    if (r.match (reColorHex))
      return r

    var reColorRgb = /rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/ ;

    var c
    if (c = r.match (reColorRgb))
      return hexColorString (parseInt(c[1],10)
                            ,parseInt(c[2],10)
                            ,parseInt(c[3],10))

    return '#FF0000' // a color to indicate a problem
  }

  var r = r.toString(16); if (r.length == 1) r = '0'+r;
  var g = g.toString(16); if (g.length == 1) g = '0'+g;
  var b = b.toString(16); if (b.length == 1) b = '0'+b;

  return "#" + r + g + b
}
