var fadeQueue = new Array(), RGB = new Array(256), k = 0, hex = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) RGB[k++] = hex[i] + hex[j];

function queueFade (tagid, direction, startRGB, endRGB, duration) {
  if (fadeQueue.length == 0) fadeExtra(tagid, direction, 1);
  for (var x = fadeQueue.length - 1; x >= 0; x--) {
    if (fadeQueue[x][0] == tagid) {
      if (fadeQueue[x][1] != direction) {
	fadeQueue[fadeQueue.length] = [tagid, direction, startRGB, endRGB, duration];
	return false;
      } else return false;
    }
  }
  //alert (tagid+direction+startRGB+endRGB+duration);
  fadeQueue[fadeQueue.length] = [tagid, direction, startRGB, endRGB, duration];
  setTimeout("fade('" + tagid + "','" + startRGB + "'," + parseInt(startRGB.substr(1).substr(0, 2), 16) + "," + parseInt(startRGB.substr(1).substr(2, 2), 16) + "," + parseInt(startRGB.substr(1).substr(4, 2), 16) + ",'" + endRGB + "'," + (duration / 20) + ");", 100);
}

function queueCheck (tagid) {
  var firstFade = true, firstKey = -1, secondFade = true, secondKey = 0, compileQueue = new Array();
  for (var x = 0; x < fadeQueue.length; x++) {
    if (fadeQueue[x][0] == tagid) {
      if (firstFade) {
	firstKey = x; firstFade = false;
      } else if (secondFade) {
	secondKey = x; secondFade = false;
      } else if (!secondFade) secondFade = true;
    } else compileQueue[compileQueue.length] = fadeQueue[x];
  }
  fadeExtra(tagid, fadeQueue[firstKey][1], 0);
  if (!secondFade) {
    compileQueue[compileQueue.length] = fadeQueue[secondKey];
    fadeExtra(tagid, fadeQueue[secondKey][1], 1);
    setTimeout("fade('" + tagid + "','" + fadeQueue[secondKey][2] + "'," + parseInt(fadeQueue[secondKey][2].substr(1).substr(0, 2), 16) + "," + parseInt(fadeQueue[secondKey][2].substr(1).substr(2, 2), 16) + "," + parseInt(fadeQueue[secondKey][2].substr(1).substr(4, 2), 16) + ",'" + fadeQueue[secondKey][3] + "'," + (fadeQueue[secondKey][4] / 20) + ");", 0);
  }
  fadeQueue = compileQueue;
}

function fade(tagid, startRGB, r, g, b, endRGB, slices) {
  incRGB = [r, g, b];
  for (var x = 0; x < 3; x++) {
    var colRGB = parseInt(endRGB.substr(1).substr(x * 2, 2), 16);
    var incr = (colRGB - parseInt(startRGB.substr(1).substr(x * 2, 2), 16)) / slices;
    incRGB[x] = (incr < 0) ? Math.max(incRGB[x] + incr, colRGB) : Math.min(incRGB[x] + incr, colRGB);
  }
  if (incRGB[0] == parseInt(endRGB.substr(1).substr(0, 2), 16) &&
      incRGB[1] == parseInt(endRGB.substr(1).substr(2, 2), 16) &&
      incRGB[2] == parseInt(endRGB.substr(1).substr(4, 2), 16)) {
    document.getElementById(tagid).style.color = endRGB;
    setTimeout("queueCheck('" + tagid + "');", 0);
    return false;
  }
  document.getElementById(tagid).style.color = "#" + RGB[parseInt(incRGB[0])] + RGB[parseInt(incRGB[1])] + RGB[parseInt(incRGB[2])];
  setTimeout("fade('" + tagid + "','" + startRGB + "'," + incRGB[0] + "," + incRGB[1] + "," + incRGB[2] + ",'" + endRGB + "'," + slices + ")", 20);
}
function fadeExtra(tagid, direction, af0_be1) {
  if (direction == "in" && af0_be1 == 1) document.getElementById(tagid).innerHTML = rDiv;
  if (direction == "out" && af0_be1 == 0) document.getElementById(tagid).innerHTML = "&nbsp;";
}
var rDiv = "";