// Function.prototype.closure = function(obj)
// {
//   // Init object storage.
//   if (!window.__objs)
//   {
//     window.__objs = [];
//     window.__funs = [];
//   }

//   // For symmetry and clarity.
//   var fun = this;

//   // Make sure the object has an id and is stored in the object store.
//   var objId = obj.__objId;
//   if (!objId)
//     __objs[objId = obj.__objId = __objs.length] = obj;

//   // Make sure the function has an id and is stored in the function store.
//   var funId = fun.__funId;
//   if (!funId)
//     __funs[funId = fun.__funId = __funs.length] = fun;

//   // Init closure storage.
//   if (!obj.__closures)
//     obj.__closures = [];

//   // See if we previously created a closure for this object/function pair.
//   var closure = obj.__closures[funId];
//   if (closure)
//     return closure;

//   // Clear references to keep them out of the closure scope.
//   obj = null;
//   fun = null;

//   // Create the closure, store in cache and return result.
//   return __objs[objId].__closures[funId] = function ()
//   {
//     return __funs[funId].apply(__objs[objId], arguments);
//   };
// };

function gamma(level, max, min) {
  var before = level;
  if (!min)
    min = 0;
  level = Math.pow(level, 1 / 1.5);
  // alert("gamma level " + before + " -> " + level);
  return Math.round(min + (max - min) * level);
}

function toColor(gray) {
  gray = window.gamma(gray, 0xff);
  if (gray < 0x10)
    return "#0" + gray.toString(16) + "0" + gray.toString(16) + "0" + gray.toString(16);
  return "#" + gray.toString(16) + gray.toString(16) + gray.toString(16);
}

function toBlueColor(gray) {
  if (gray < 0x10)
    return "#00000" + gray.toString(16);
  return "#0000" + gray.toString(16);
}

function toBlueGrayColor(gray) {
  var blue = window.gamma(gray * 3, 0xff);
  gray = window.gamma(gray, 0xff);
  if (blue < 0x10)
    blue = "0" + blue.toString(16);
  else
    blue = blue.toString(16);
  if (gray < 0x10)
    gray = "0" + gray.toString(16);
  else
    gray = gray.toString(16);
  return "#" + gray + gray + blue;
}

// function Highlighter_lighter() {
//   // if (this.level == 0)
//   //   this.level = 0x0f;
//   // else
//   //   this.level += 0x10;
//   this.level += this.step;
//   this.obj.style.color = toBlueGrayColor(this.level);
//   if (this.level > this.max - this.step) {
//     this.timer = window.setTimeout(this.darker.closure(this), this.timestep);
//   } else {
//     this.timer = window.setTimeout(this.lighter.closure(this), this.timestep);
//   }
// }

// function Highlighter_darker() {
//   this.level -= this.step;
//   // if (this.level <= 0x10)
//   //   this.level = 0;
//   // else
//   //   this.level -= 0x10;
//   this.obj.style.color = toBlueGrayColor(this.level);
//   var obj = this;
//   if (this.level >= this.step) {
//     this.timer = window.setTimeout(this.darker.closure(this), this.timestep);
//   } else if (!this.last) {
//     this.timer = window.setTimeout(this.lighter.closure(this), this.timestep);
//   } else
//     this.timer = null;
// }

// function Highlighter_disable() {
//   this.last = true;
// }

// function Highlighter_enable() {
//   this.last = false;
//   if (this.timer) {
//     $(this).stopTime();
//     // window.clearTimeout(this.timer);
//     this.timer = false;
//   }
//   this.lighter();
// }

function Highlighter(o) {
  this.obj = o;
  this.level = 0;
  this.max = Math.pow(.4, 1.5);
  this.step = 0.025;
  this.timestep = 50;
  this.last = false;
  this.lighter = function () {
    this.level += this.step;
    $(this.obj).css('color', toBlueGrayColor(this.level));
    if (this.level > this.max - this.step)
      $(this).oneTime(this.timestep, 'highlight',
		      function () { this.darker(); });
    else
      $(this).oneTime(this.timestep, 'highlight',
		      function () { this.lighter(); });
  };

  this.darker = function () {
    this.level -= this.step;
    $(this.obj).css('color', toBlueGrayColor(this.level));
    if (this.level >= this.step)
      $(this).oneTime(this.timestep, 'highlight',
		      function () { this.darker(); });
    else if (!this.last)
      $(this).oneTime(this.timestep, 'highlight',
		      function () { this.lighter(); });
  };

  this.disable = function () { this.last = true; };

  this.enable = function () {
    this.last = false;
    $(this).stopTime('highlight');
    this.lighter();
  };
}

function blinkerActivate(e) {
  $(e.target).filter('a').each(function (index, value) {
    if (!value.blinker)
      value.blinker = new Highlighter(value);
    value.blinker.enable();
  });

  // if (!e) e = event;
  // var t = e.srcElement ? e.srcElement : e.target;
  // if (t.tagName.toUpperCase() != "A")
  //   return;
  // if (!t.blinker)
  //   t.blinker = new window.Highlighter(t);
  // if (t.blinker.enable)
  //   t.blinker.enable();
}

function blinkerDeactivate(e) {
  $(e.target).filter('a').each(function (index, value) {
    if (!value.blinker)
      value.blinker = new Highlighter(value);
    value.blinker.disable();
  });
  // if (!e) e = event;
  // var t = e.srcElement ? e.srcElement : e.target;
  // if (t.tagName.toUpperCase() != "A")
  //   return;
  // if (!t.blinker)
  //   t.blinker = new window.Highlighter(t);
  // if (t.blinker.disable)
  //   t.blinker.disable();
}

$(document).ready(function () {

  $('a')
    .filter(':not(#sizetoggle a)')
    .filter(':not(#addcommentsoptlink)')
    .filter(':not(.noblink)')
    .each(function (index, value) {
    $(value)
      .css('text-decoration', 'none')
      .mouseover(blinkerActivate)
      .mouseout(blinkerDeactivate);
  });

  // var ol = document.getElementsByTagName("a");

  // for (i = 0; i < ol.length; ++i) {

  //   if (ol.item(i).id == "sizetogglelink" || ol.item(i).id == "addcommentsoptlink" || ol.item(i).className == "noblink")
  //     continue;

  //   if (ol.item(i).style.setProperty)
  //     ol.item(i).style.setProperty("text-decoration", "none", "important");
  //   else
  //     ol.item(i).style.textDecoration = "none";
  //   if (ol.item(i).addEventListener) {
  //     ol.item(i).addEventListener("mouseover", window.blinkerActivate, false);
  //     ol.item(i).addEventListener("mouseout", window.blinkerDeactivate, false);
  //   } else if (attachEvent) {
  //     ol.item(i).attachEvent("onmouseover", window.blinkerActivate);
  //     ol.item(i).attachEvent("onmouseout", window.blinkerDeactivate);
  //   }

  // }

  // return true;
});

// $(document).ready(installHig
// if (window.addEventListener)
//   window.addEventListener("load", window.installHighlighters, false);
// else if (window.attachEvent)
//   window.attachEvent("onload", window.installHighlighters);
// else
//   window.onload = window.installHighlighters;

