spot.colorArr = new Array()


spot.colorArr[0] = '#ff9400'
spot.colorArr[1] = '#ed145a'
spot.colorArr[2] = '#ffcc33'
spot.colorArr[3] = '#678a19'
spot.colorArr[4] = '#99ccff'
spot.colorArr[5] = '#678a19'
spot.colorArr[6] = '#ffcc33'
spot.colorArr[7] = '#ed145a'
spot.colorArr[8] = '#ff9400'


spot.duration = 11250
spot.step = 40

//--------------------------------------------------------------
function spot (element, index) {
  //var re = new RegExp ("(.*)k(.*)")
  //var arr = re.exec (element.id)
  //var pid = arr[2]
	

  	po = document.getElementById(element)

  	if (!po) {
		document.write("error");
		return;  
  	}
	
	
	spot.set  (po,spot.colorArr[index])  // set an eye grabbing color, not to garish now!
	if (index < spot.colorArr.length - 1)
		spot.fade (po,spot.colorArr[index+1],spot.step,spot.duration)	
	else {
		spot.fade (po,spot.colorArr[0],spot.step,spot.duration)	
		index = 0;
	}
	
	//alert("spot('" + element + "'," + (index + 1) + ")")
	setTimeout("spot('" + element + "'," + (index + 1) + ")", spot.duration)

	
	
}
//--------------------------------------------------------------
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)  // base 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
}
