function BlendIn(Element) {
  if (Element && Element.className=="CustomFreePage") {
    for (var i=0; i<DoBlendOut.length; i++) {
      if (DoBlendOut[i]==Element) {
        DoBlendOut.splice(i, 1);
      }
    }
    DoBlendIn.push(Element);
  }
}

function BlendOut(Element) {
  if (Element && Element.className=="CustomFreePage") {
    for (var i=0; i<DoBlendIn.length; i++) {
      if (DoBlendIn[i]==Element) {
        DoBlendIn.splice(i, 1);
      }
    }
    DoBlendOut.push(Element);
  }
}

var DoBlendIn=new Array();
var DoBlendOut=new Array();

function BlendElements() {
  for (var i=0; i<DoBlendIn.length; i++) {
    var GreyValue=GetGreyValue(DoBlendIn[i])-1;
    if (GreyValue>201) {
      SetColor(DoBlendIn[i], GreyValue);
    }
    else {
      DoBlendIn.splice(i, 1);
    }
  }
  for (var i=0; i<DoBlendOut.length; i++) {
    var GreyValue=GetGreyValue(DoBlendOut[i])+1;
    if (GreyValue<246) {
      SetColor(DoBlendOut[i], GreyValue);
    }
    else {
      DoBlendOut.splice(i, 1);
    }
  }
}

function GetGreyValue(Element) {
  var Color=Element.style.backgroundColor;
  if (Color.substr(0, 3)=="rgb") {
    Color=Color.substr(4);
    Colors=Color.split(",");
    return parseInt(Colors[0], 10);
  }
  if (Color.length<7) {
    Color="#F5E566";
  }
  return parseInt(Color.slice(1, 3), 16);
}

function SetColor(Element, GreyValue) {
  var HexGreyRed=GreyValue.toString(16);
  GreyValue-=16;
  var HexGreyGreen=GreyValue.toString(16);
  GreyValue-=127;
  var HexGreyBlue=GreyValue.toString(16);
  if (HexGreyRed.length<2) {
    HexGreyRed="0" + HexGreyRed;
  }
  if (HexGreyGreen.length<2) {
    HexGreyGreen="0" + HexGreyGreen;
  }
  if (HexGreyBlue.length<2) {
    HexGreyBlue="0" + HexGreyBlue;
  }
  var Color="#" +  HexGreyRed + HexGreyGreen + HexGreyBlue;
  Element.style.backgroundColor=Color;
}

window.setInterval("BlendElements()", 5);
