function Debug( s )
{
  document.getElementById("debug").innerHTML = s;
}

function RGBCol(R,G,B)
{
  this.R = R;
  this.G = G;
  this.B = B;
}

function getRGB( s )
{
  s = s.toUpperCase();
  if(s.charAt(0) == "#")
   {
     if(s.length == 7)
      {
        var rgb = new RGBCol(parseInt(s.substr(1,2),16),parseInt(s.substr(3,2),16),parseInt(s.substr(5,2),16));
      }
     else
      {
        var rgb = new RGBCol(parseInt(s.substr(1,1),16),parseInt(s.substr(2,1),16),parseInt(s.substr(3,1),16));
      }
   }
  else if(s.substr(0,3) == "RGB")
   {
     s = s.substr(s.indexOf("(")+1,s.length);
     s = s.substr(0,s.indexOf(")"));
     var A = s.split(",");
     var rgb = new RGBCol(parseInt(A[0]),parseInt(A[1]),parseInt(A[2]));
   }
  else var rgb = RGBCol(0,0,0);

  return rgb;
}

function RGBtoCol(rgb)
{
  return "rgb("+rgb.R+","+rgb.G+","+rgb.B+")";
}

function limitCol( col, v)
{
  col = col + v;
  if(col < 0) col = 0;
   else if(col >255) col = 255;
  return col;
}

var ch = new Array();
ch[0] = 0;
ch[1] = new RGBCol();
ch[2] = new RGBCol();
ch[3] = new RGBCol();
ch[4] = new RGBCol();

function setCols(restore)
{
 if(ch[0])
  {
   if(restore)
    {
      ch[0].style.color =RGBtoCol(ch[3]) ;
      ch[0].style.backgroundColor =RGBtoCol(ch[4]) ;
      ch[0]=0;
    }
   else
    {
      ch[0].style.color =RGBtoCol(ch[1]) ;
      ch[0].style.backgroundColor =RGBtoCol(ch[2]) ;
    }
  }
}

function diffCol(col, mx, d)
{
 if(d) col = col + 20;
  else col = col - 20;

 if(d==true && col > mx) col = mx;
  else if(d==false && col < mx) col = mx;
 return limitCol(col,0);
}

function doChangeCol()
{// alert( RGBtoCol(ch[1]) +" x "+ RGBtoCol(ch[2]));
  if(ch[3].R > ch[4].R) ch[1].R = diffCol( ch[1].R , ch[4].R , false);
   else ch[1].R = diffCol( ch[1].R , ch[4].R , true);

  if(ch[3].G > ch[4].G) ch[1].G = diffCol( ch[1].G , ch[4].G , false);
   else ch[1].G = diffCol( ch[1].G , ch[4].G , true);

  if(ch[3].B > ch[4].B) ch[1].B = diffCol( ch[1].B , ch[4].B , false);
   else ch[1].B = diffCol( ch[1].B , ch[4].B , true);



  if(ch[4].R > ch[3].R) ch[2].R = diffCol( ch[2].R , ch[3].R , false);
   else ch[2].R = diffCol( ch[2].R , ch[3].R , true);

  if(ch[4].G > ch[3].G) ch[2].G = diffCol( ch[2].G , ch[3].G , false);
   else ch[2].G = diffCol( ch[2].G , ch[3].G , true);

  if(ch[4].B > ch[3].B) ch[2].B = diffCol( ch[2].B , ch[3].B , false);
   else ch[2].B = diffCol( ch[2].B , ch[3].B , true);

//alert( RGBtoCol(ch[1]) +" x "+ RGBtoCol(ch[2]));
  setCols(false);

  if(ch[1].R != ch[4].R || ch[1].G != ch[4].G || ch[1].B != ch[4].B ||
     ch[2].R != ch[3].R || ch[2].G != ch[3].G || ch[2].B != ch[3].B )
   {
     setTimeout("doChangeCol()",50);
   }
}

function changeCol(e)
{
  if(e==null) setCols(true);
  else if( e == ch[0])
   {

   }
  else
   {
    setCols(true);
    ch[0]=0;

    var vg = e.style.color;
    var bg = e.style.backgroundColor;

    var vg_rgb = getRGB(vg);
    var bg_rgb = getRGB(bg);


    ch[0] = e;
    ch[1].R = vg_rgb.R;
    ch[1].G = vg_rgb.G;
    ch[1].B = vg_rgb.B;
    ch[3].R = vg_rgb.R;
    ch[3].G = vg_rgb.G;
    ch[3].B = vg_rgb.B;

    ch[2].R = bg_rgb.R;
    ch[2].G = bg_rgb.G;
    ch[2].B = bg_rgb.B;
    ch[4].R = bg_rgb.R;
    ch[4].G = bg_rgb.G;
    ch[4].B = bg_rgb.B;

    doChangeCol();
   }


 // alert(vg +" - " + bg);

//  alert(vg +" : R=" + rgb.R+", G=" + rgb.G+", B=" + rgb.B);
}
