/* DO NOT TOUCH ANYTHING STARTING FROM HERE */
  function debug(str) {
    var debug = document.getElementById("debug");
    debug.innerHTML += str;
  }

  function ColorFade() {
  
    /* 
     * this is all stuff that can be changed (these just setup defaults)
     */
    this.timeBetweenFade = 5000; /* the time between the fade affects */
    this.fadeTime        = 2500; /* the time to use during the fade affect */
    this.fadeTick        = 100;   /* the amount of time between color ticks */
    this.colorList       = new Array('#cfcfcf', '#cf0033', '#993399');
    
    /* This is all internally used */
    this.lastColorPos       = 0;
    this.lastRGB            = new Array(0,0,0); /* 0 == R, 1 == G, 2 == B */
    this.rgbTransitionList  = new Array();
    this.rgbTransistionPos  = 0;
    document.body.colorTrans = this;
  }
  
  ColorFade.prototype.TransitColor = function() {
    this.SetFadeTicks();
    this.TransitColorTick();
  }

  ColorFade.prototype.TransitColorTick = function() {
    document.body.style.backgroundColor = this.rgbTransitionList[this.rgbTransitionPos];    
    this.rgbTransitionPos++;
    
    if(this.rgbTransitionPos >= this.rgbTransitionList.length) {
      setTimeout("document.body.colorTrans.TransitColor();", this.timeBetweenFade);
    }else{
      setTimeout("document.body.colorTrans.TransitColorTick();", this.fadeTick);
    }
  }
  
  ColorFade.prototype.SetFadeTicks = function() {
      this.rgbTransitionList = new Array();
      this.rgbTransitionPos = 0;
      
      
      var fadeCount = this.fadeTime / this.fadeTick;
      
      var begColor = this.GetRGBColors(this.colorList[this.lastColorPos]);
      
      var endColor = null;      
      if((this.lastColorPos + 1) >= this.colorList.length) {
        endColor = this.GetRGBColors(this.colorList[0]); 
      }else {
        endColor = this.GetRGBColors(this.colorList[this.lastColorPos + 1]);      
      }
      
      var transTick = new Array();
      for(var i = 0; i < 3; i++) {
        var a = new Number(begColor[i]);
        var b = new Number(endColor[i]);        
        var c = 0;
        
        c = parseInt((b - a) / fadeCount);
        
        transTick[i] = c.toFixed(0);
      }

      for(var i = 0; i < fadeCount; i++) {
        var tick = new Array();
        for(var c = 0; c < 3; c++) {
           var n = new Number(parseInt(begColor[c]) + (parseInt(transTick[c]) * i));
           tick[c] = n.toFixed(0);
        }

        this.rgbTransitionList[i] = this.GetHexColors(tick);
      }
      
      this.lastColorPos++;
      
      if(this.lastColorPos >= this.colorList.length) {
        this.lastColorPos = 0;
      }
  }
  
  /*
   * This expects #ffffff color and returns array(r, g, b)
   */
  ColorFade.prototype.GetRGBColors = function(hexColor) {
    var r = parseInt("0x" + hexColor.substr(1,2));
    var g = parseInt("0x" + hexColor.substr(3,2));
    var b = parseInt("0x" + hexColor.substr(5,2));
    
    return( new Array(r, g, b));    
  }
  
  /*
   * This Expects array(r, g, b) and returns #ffffff
   */
  ColorFade.prototype.GetHexColors = function(rgbColor) {
    var color = "#";
    
    for(var i = 0; i < 3; i++) {
      var c = new Number((rgbColor[i] > 255) ? 255 : rgbColor[i]);
      var d = parseInt(c.toFixed(0));
      var a = d.toString(16);
      if(a.length == 1){
        color = color + "0" + a;
      }else{
        color = color + a;
      } 
    }
    
    return color;
  }
/* DO NOT TOUCH ANYTHING ENDING TO HERE */
  
