// color shifter copyright 2006 bart thielges

var arrSize = 40;
var topLimit = arrSize;
var colorArray = new Array(arrSize);
var delayArray = new Array(arrSize);
var tmpIndx = 0;
colorArray[tmpIndx] = 'FFFFFF';   delayArray[tmpIndx++] = 10000;
colorArray[tmpIndx] = 'FBFBFB';   delayArray[tmpIndx++] = 7000;
colorArray[tmpIndx] = 'F8F8F8';   delayArray[tmpIndx++] = 5000;
colorArray[tmpIndx] = 'F4F4F4';   delayArray[tmpIndx++] = 5000;
colorArray[tmpIndx] = 'EFEFEF';   delayArray[tmpIndx++] = 3000;
colorArray[tmpIndx] = 'EBEBEB';   delayArray[tmpIndx++] = 3000;
colorArray[tmpIndx] = 'E8E8E8';   delayArray[tmpIndx++] = 3000;
colorArray[tmpIndx] = 'E4E4E4';   delayArray[tmpIndx++] = 3000;
colorArray[tmpIndx] = 'E0E0E0';   delayArray[tmpIndx++] = 2000;
colorArray[tmpIndx] = 'DBDBDB';   delayArray[tmpIndx++] = 1000;
colorArray[tmpIndx] = 'D8D8D8';   delayArray[tmpIndx++] = 300;
colorArray[tmpIndx] = 'D4D4D4';   delayArray[tmpIndx++] = 300;
colorArray[tmpIndx] = 'D0D0D0';   delayArray[tmpIndx++] = 100;
colorArray[tmpIndx] = 'CBCBCB';   delayArray[tmpIndx++] = 100;
colorArray[tmpIndx] = 'C8C8C8';   delayArray[tmpIndx++] = 100;
colorArray[tmpIndx] = 'C4C4C4';   delayArray[tmpIndx++] = 100;
colorArray[tmpIndx] = 'C0C0C0';   delayArray[tmpIndx++] = 100;
colorArray[tmpIndx] = 'BBBBBB';   delayArray[tmpIndx++] = 80;
colorArray[tmpIndx] = 'B8B8B8';   delayArray[tmpIndx++] = 80;
colorArray[tmpIndx] = 'B4B4B4';   delayArray[tmpIndx++] = 80;
colorArray[tmpIndx] = 'B0B0B0';   delayArray[tmpIndx++] = 80;
colorArray[tmpIndx] = 'ABABAB';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = 'A8A8A8';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = 'A4A4A4';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = 'A0A0A0';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = '989898';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = '909090';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = '888888';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = '808080';   delayArray[tmpIndx++] = 50;
colorArray[tmpIndx] = '787878';   delayArray[tmpIndx++] = 30;
colorArray[tmpIndx] = '707070';   delayArray[tmpIndx++] = 30;
colorArray[tmpIndx] = '686868';   delayArray[tmpIndx++] = 30;
colorArray[tmpIndx] = '606060';   delayArray[tmpIndx++] = 30;
colorArray[tmpIndx] = '585858';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '505050';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '484848';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '404040';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '303030';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '202020';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '101010';   delayArray[tmpIndx++] = 20;
colorArray[tmpIndx] = '000000';   delayArray[tmpIndx++] = 770;
var topLimit = tmpIndx - 1;

// var colIndx = 0;
var directionUp = true;
var outerLoopIters = 10;
var m3bi = outerLoopIters;
var lastValue = colorArray[0];

var hD="0123456789ABCDEF";
function d2h(d) {
    var h = hD.substr(d&15,1);
    while(d>15) {
        d>>=4;h=hD.substr(d&15,1)+h;
    }
return h;
}

function h2d(h) {return parseInt(h,16);}

function bump(colIndx) {

// alert('im bump')
    if (directionUp) {
        colIndx++;
    } else {
        colIndx--;
    }

    if (directionUp && (colIndx > topLimit)) {
        directionUp = false;
        colIndx = topLimit - 1;
    }
    if (!directionUp && (colIndx < 0)) {
        directionUp = true;
        colIndx = 1;
        outerLoopIters--;
        if (0 >= outerLoopIters) {
            return -1;  // final outer escape
        }
    }

    return colIndx;
}

// iLevel is in terms of a single color component : 0-255
function rotateColors2(colIndx, iLevel, iEnd, iStep, iDelay) {
    newColor = '#' + colorArray[colIndx];
    delay = delayArray[colIndx];

    // if iLevel active, then use that instead of incrementing colIndx
    if (null != iLevel) {
       // document.forms.my.view.value = newColor + ' ' + colIndx + ' d' + delayArray[colIndx] + 
           ' id' + iDelay + ' iL' + iLevel + ' iE' + iEnd + ' iS' + iStep;
       if ((!directionUp && (iLevel >= iEnd)) || (directionUp && (iLevel <= iEnd))) {
          // flip back to normal mode
          lastValue = colorArray[colIndx];
          comStr = "rotateColors2(" + colIndx + ')';
          setTimeout(comStr, iDelay);
       } else {
          newColorDecimal = iLevel + iStep;
          if ((!directionUp && (newColorDecimal > iEnd))  || 
              (directionUp && (newColorDecimal < iEnd))) {
              newColorDecimal = iEnd;
          }
          newColorComp = d2h(newColorDecimal);
          newColor = '#' + newColorComp + newColorComp + newColorComp;
          comStr = "rotateColors2(" + colIndx + ', ' + 
              newColorDecimal + ', ' +
              iEnd + ', ' +
              iStep + ', ' +
              iDelay + ')';

          // document.forms.my.view2.value = '1 ' + newColor;
          document.bgColor = newColor;
          setTimeout(comStr, iDelay);
       }
       return
    }


    // document.forms.my.view.value = colIndx + ' d' + delayArray[colIndx];

    newColIndx = bump(colIndx)

    if (-1 == newColIndx) {
       return;
    }

    // is the slope close to 1/2 and long delay ?  
    // If so flip to interpolated mode
    
    lastColComp = lastValue.substring(0,2);
    nextColComp = colorArray[newColIndx].substring(0,2);

    colDiff = h2d(nextColComp) - h2d(lastColComp);
    if (Math.abs(colDiff) >= 3) {

          // divide into 10 steps
          divisor = 10;
          if (colDiff > divisor) {
             iStep = colDiff / divisor;
          } else {
             divisor = Math.abs(colDiff);
             if (directionUp) {
                iStep = -1;
             } else {
                iStep = 1;
             }
          }

          iDelay = (delayArray[newColIndx] / divisor);
          comStr = "rotateColors2(" + newColIndx + ', ' + 
              h2d(lastColComp) + ', ' +
              h2d(nextColComp) + ', ' +
              iStep + ', ' +
              iDelay + ')';
          // document.forms.my.view2.value = '2 ' + newColor;
          document.bgColor = newColor;
          // when the following statement is commented out, the script
          // gets hung with lots of setTimeout calls
          setTimeout(comStr, iDelay);
          return;
    }


    // document.forms.my.view2.value = '3 ' + newColor;
    document.bgColor = newColor;
    lastValue = colorArray[newColIndx];
    setTimeout("rotateColors2(" + newColIndx + ")", delay);
}

//rotateColors2(14, 4, 255, 100, 1000);
rotateColors2(0);
