var colors = new Array("000000","000033","000066","000099","0000cc","0000ff","006600","006633","006666","006699","0066cc","0066ff","00cc00","00cc33","00cc66","00cc99","00cccc","00ccff","330000","330033","330066","330099","3300cc","3300ff","336600","336633","336666","336699","3366cc","3366ff","33cc00","33cc33","33cc66","33cc99","33cccc","33ccff","660000","660033","660066","660099","6600cc","6600ff","666600","666633","666666","666699","6666cc","6666ff","66cc00","66cc33","66cc66","66cc99","66cccc","66ccff","990000","990033","990066","990099","9900cc","9900ff","996600","996633","996666","996699","9966cc","9966ff","99cc00","99cc33","99cc66","99cc99","99cccc","99ccff","cc0000","cc0033","cc0066","cc0099","cc00cc","cc00ff","cc6600","cc6633","cc6666","cc6699","cc66cc","cc66ff","cccc00","cccc33","cccc66","cccc99","cccccc","ccccff","ff0000","ff0033","ff0066","ff0099","ff00cc","ff00ff","ff6600","ff6633","ff6666","ff6699","ff66cc","ff66ff","ffcc00","ffcc33","ffcc66","ffcc99","ffcccc","ffccff","003300","003333","003366","003399","0033cc","0033ff","009900","009933","009966","009999","0099cc","0099ff","00ff00","00ff33","00ff66","00ff99","00ffcc","00ffff","333300","333333","333366","333399","3333cc","3333ff","339900","339933","339966","339999","3399cc","3399ff","33ff00","33ff33","33ff66","33ff99","33ffcc","33ffff","663300","663333","663366","663399","6633cc","6633ff","669900","669933","669966","669999","6699cc","6699ff","66ff00","66ff33","66ff66","66ff99","66ffcc","66ffff","993300","993333","993366","993399","9933cc","9933ff","999900","999933","999966","999999","9999cc","9999ff","99ff00","99ff33","99ff66","99ff99","99ffcc","99ffff","cc3300","cc3333","cc3366","cc3399","cc33cc","cc33ff","cc9900","cc9933","cc9966","cc9999","cc99cc","cc99ff","ccff00","ccff33","ccff66","ccff99","ccffcc","ccffff","ff3300","ff3333","ff3366","ff3399","ff33cc","ff33ff","ff9900","ff9933","ff9966","ff9999","ff99cc","ff99ff","ffff00","ffff33","ffff66","ffff99","ffffcc","ffffff","000000","333333","666666","999999","cccccc","ffffff","transparent");
var selected = 0;
var skips = new Array(".header");
var names = new Array("links", "viewed_links");
var ids = new Array("document","document");
var types = new Array("document", "document");
var atts = new Array("linkColor", "vlinkColor");

var browser;// = new Browser();
var dragObj;// = new Object();
window.onload = loadRequest;

function findStyleRule(styleName)
{
  data = "";
  var obj = null;
  for (i = 0; i < document.styleSheets.length; i++)
  {
    var crossRules = null;
    if(document.styleSheets[i].cssRules) crossRules = document.styleSheets[i].cssRules
    else if(document.styleSheets[i].rules) crossRules = document.styleSheets[i].rules

    for (j = 0; j < crossRules.length; j++)
    {
      data += j + ":" + i + ":" + crossRules[j].selectorText + " ";
      if (crossRules[j].selectorText == styleName)
      {
        if(obj != null) alert(crossRules[j].selectorText + " found more than once");
        obj = crossRules[j];
      }
    }
  }
  return(obj);
}

function inArray(arr, value)
{
  for(x in arr) if(arr[x] == value) return(x);
  return(-1);
}

function xx()
{
  for (i = 0; i < document.styleSheets.length; i++)
  {
    var crossRules = null;
    if(document.styleSheets[i].cssRules) crossRules = document.styleSheets[i].cssRules
    else if(document.styleSheets[i].rules) crossRules = document.styleSheets[i].rules
    
    for (j = 0; j < crossRules.length; j++)
    {
      var rule = crossRules[j];
      var text = rule.selectorText;
      if(text.charAt(0) == '.' && inArray(skips, text) == -1)
      {
        text = text.substring(1);
        names[names.length] = text + "";
        ids[ids.length] = rule.selectorText;
        types[types.length] = "class";
        atts[atts.length] = "backgroundColor";
        
        names[names.length] = text + "_text";
        ids[ids.length] = rule.selectorText;
        types[types.length] = "class";
        atts[atts.length] = "color";
      }
    }
  }
  
  var objs = document.getElementsByTagName("*");
  for (i = 0; i < objs.length; i++)
  {
    if(objs[i].id != "")
    {
      names[names.length] = objs[i].id + "_bg";
      ids[ids.length] = objs[i].id;
      types[types.length] = "id";
      atts[atts.length] = "style.backgroundColor";
      
      names[names.length] = objs[i].id + "_fg";
      ids[ids.length] = objs[i].id;
      types[types.length] = "id";
      atts[atts.length] = "style.color";
    }
  }
}

function getElementsByClass(searchClass,node,tag)
{
  var classElements = new Array();
  if(node == null) node = document;
  if(tag == null) tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++)
  {
    if (pattern.test(els[i].className)) classElements[j++] = els[i];
  }
  return classElements;
}

function toggleDisplay(id)
{
  var obj = getObj(id);
  if(obj.style.display == "none") obj.style.display = "";
  else if(obj.style.display != "none") obj.style.display = "none";
}

function loadRequest()
{
  browser = new Browser();
  dragObj = new Object();
  dragObj.zIndex = 0;
  if(getVar("colors") == "") return;
  
  xx();
  
  html = '<div id="boxB" style="position: absolute; left: 600px; top: 100px; text-align: center; border: 2px outset; background-color: white;">';
  html += '<div style="padding: 5px; height: 20px; width: 260px; background-color: 333399; color: white; cursor: hand; font-weight: bold;" onmousedown="dragStart(event, \'boxB\')">';
  html += '<span style="float: left;">Set colors and font</span> ';
  html += '<span style="float: right;">';
  html += '<img onclick="toggleDisplay(\'yoyo\');" src="min.gif"/>';
  html += '<img onclick="toggleDisplay(\'boxB\');" src="close.gif"/>';
  html += '</span></div>';
  html += '<div style="background-color: white; padding: 10px;" id="yoyo"></div>';
  document.body.innerHTML += html
  
  for(x in names)
  {
    var color = getVar(names[x]);
    if(color.substring(0,3) == "rgb") color = color.replace(/\./g,",");
    if(color != "")
    {
      selected = x;
      cbg(color);
    }
  }
  document.body.style.fontFamily = getVar("fontFamily");
  selected = 0;
  redraw();
}

function getUrl()
{
  var url = document.location.href;
  if(url.indexOf("?") != -1) url = url.slice(0, url.indexOf("?"));
  url += "?";
  if(getVar("command") != "") url += "command=" + getVar("command") + "&";
  url += "colors=1&";
  for(x in names)
  {
    var color = getColor(x);
    color = color.replace(/ /g, "");
    if(color != "") url += names[x] + "=" + color + "&";
  }
  if(document.body.style.fontFamily != "") url += "fontFamily=" + document.body.style.fontFamily + "&";
  return(url.slice(0, -1));
}

function getEmptyUrl()
{
  var url = document.location.href;
  if(url.indexOf("?") != -1) url = url.slice(0, url.indexOf("?"));
  url += "?";
  if(getVar("command") != "") url += "command=" + getVar("command") + "&";
  url += "colors=1&";
  return(url.slice(0, -1));
}

function redraw()
{
  html = "<table cellpadding=\"5\"><tr>\n";
  for(x in names)
  {
    var d = names[x];
    if(x == selected) d = "<font style='color: red; background-color: white;'>" + d + "</font>";
    else d = "<font style='color: blue; background-color: white;'>" + d + "</font>";
    var n = "x" + x;
    var v = "";
    v = getColor(x);
    var c = v;
    if(c == "") c = "white";
    var o = "setSelected('" + x + "');";
    if(x % 2 == 0) html += "</tr><tr>";
    html += "<td nowrap style=\"background-color: " + c + ";\" onclick=\"" + o + "\">" + d + "</td>\n";
  }
  html += "</tr></table><br style='line-height: 5px;'/>\n";
  url = getUrl();
  html += "<span style='white-space: nowrap; padding: 0px 10px 0px 10px; background-color: white'>&nbsp;<a style='text-decoration: underline; color: blue;' href='" + url + "'>right click to copy color code link</a> </span><br/>";
  html += "<span style='white-space: nowrap; padding: 0px 10px 0px 10px; background-color: white'>&nbsp;<a style='text-decoration: underline; color: blue;' href='" + getEmptyUrl() + "'>click here to reset colors</a> </span><br/>";
  html += "<br style='line-height: 5px;'/>\n";
  html += "<style>table { border-collapse: collapse; } th, td { border: 1px solid #999999; font-size: .8em; } .colorTable td { width: 10px; height: 10px; }</style><table class='colorTable' border='0' cellspacing='0'><tr>\n";
  
  for(x in colors)
  {
    html += "<td bgcolor='" + colors[x]+ "' onclick='cbg(\"" + colors[x] + "\"); redraw();'></td>\n";
    y = x;
    if(y != 0 && ++y % 18 == 0) html += "</tr><tr>\n";
  }
  var fonts = new Array("times", "courier", "arial", "serif", "sans-serif", "cursive", "fantasy", "monospace");
  html += "</tr></table>\n";
  html += "Font Family: <select name='fontFamily' onchange='document.body.style.fontFamily = this.options[this.selectedIndex].value; redraw(); '>\n";
  for(x in fonts)
  {
    var sel = "";
    if(document.body.style.fontFamily == fonts[x]) sel = "selected";
    html += "<option " + sel + " value='" + fonts[x] + "'>" + fonts[x] + "</option>\n";
  }
  html += "</select>";
  getObj("yoyo").innerHTML = html;
}

function cbg(color)
{
  if(types[selected] == "document") eval("document." + atts[selected] + " = '#" + color + "';");
  document.bgColor = '#ffffff';
  if(types[selected] == "body") eval("document.body." + atts[selected] + " = '" + color + "';");
  if(types[selected] == "class")
  {
    var obj = findStyleRule(ids[selected]);
    if(obj != null) obj.style[atts[selected]] = color;
  }
  if(types[selected] == "id") if(getObj(ids[selected]) != null) eval("getObj('" + ids[selected] + "')." + atts[selected]+ " = '" + color + "';");
}

function getColor(x)
{
  var color = "";
  if(types[x] == "document") color = eval("document." + atts[x]);
  if(types[x] == "body") color = eval("document.body." + atts[x]);
  if(types[x] == "class")
  {
    var obj = findStyleRule(ids[x]);
    if(obj == null) alert("error with: " + ids[x]);
    if(obj != null) color = obj.style[atts[x]];
  }
  if(types[x] == "id") if(getObj(ids[x]) != null) color = eval("getObj('" + ids[x] + "')." + atts[x]);
  return(color.replace('#', ''));
}

function setSelected(i)
{
  selected = i;
  redraw();
}

function getObj(name)
{
  var obj = null;
  if(document.getElementById) obj = document.getElementById(name);
  else if(document.all) obj = document.all[name];
  else if(document.layers) obj = document.layers[name];
  return(obj);
}

function getVar(name)
{
  get_string = document.location.search;
  return_value = '';
  
  do
  { //This loop is made to catch all instances of any get variable.
    name_index = get_string.indexOf(name + '=');
    if(name_index != -1 && get_string.charAt(name_index-1) != "&" && get_string.charAt(name_index-1) != "?") name_index = -1; // eliminates problems with myvar=x&var=x both being detected as var=x
    if(name_index != -1)
    {
      get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
      
      end_of_value = get_string.indexOf('&');
      if(end_of_value != -1)         
        value = get_string.substr(0, end_of_value);         
      else         
        value = get_string;         
     
      if(return_value == '' || value == '')
        return_value += value;
      else
        return_value += ', ' + value;
    }
  } while(name_index != -1)
  
  //Restores all the blank spaces.
  space = return_value.indexOf('+');
  while(space != -1)
  {
    return_value = return_value.substr(0, space) + ' ' + 
    return_value.substr(space + 1, return_value.length);
    space = return_value.indexOf('+');
  }
  return(return_value);        
}

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}




