safeStep = 17;
colorStep = safeStep * 2

colorMode = 0 // should we affect color onmouseover?
coordCache = [];
colorCache = [];

function toggleColorMode() {
    colorMode = !colorMode
    if (!colorMode) {
        // TODO: build an AA keyed on color
        // cookie the user with color settings
        // might hit max cookie size
        var tmp = '';
        for (var key in colorCache) {
            if (key) {
                tmp += key+': '
                for (var ikey in colorCache[key]) {
                    tmp += ikey+', '
                    // dump this to a cookie as RRR,GGG,BBB=XXX,YYY+XXX,YYY|RRR,GGG,BBB=XXX,YYY...
                    // possible to chunk cookies to get around size limit, or is it per domain?
                    if (!coordCache[colorCache[key][ikey]]) coordCache[colorCache[key][ikey]] = []
                    coordCache[colorCache[key][ikey]][key+','+ikey] += 1
                }
                tmp += "\n"
            }
        }
        //alert(tmp)
    }
}

function incColor(cell) {
  if (colorMode && cell.style) {
    if (cell.style.backgroundColor == '') {
        //cell.style.backgroundColor = '#ffffee'; // sissy pastel start
        cell.style.backgroundColor = '#000011'; // more manly colors
    }
    var curr = cell.style.backgroundColor;
    // mozilla converts whatever color you set to rgb
    if (/^rgb\((\d+), (\d+), (\d+)\)/.test(curr)) {
        cr = parseInt(RegExp.$1, 10);
        cg = parseInt(RegExp.$2, 10);
        cb = parseInt(RegExp.$3, 10);
    } else if (/^#?([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.test(curr)) {
        cr = parseInt(RegExp.$1, 16);
        cg = parseInt(RegExp.$2, 16);
        cb = parseInt(RegExp.$3, 16);
    }
    // sissy pastel colors:
    /*
    if      (cb <= safeStep) { cb = 255; cg = 255-safeStep }
    else if (cb < 255)    { cb -= colorStep }
    else if (cg <= safeStep) { cg = 255; cr = 255-safeStep }
    else if (cg < 255)    { cg -= colorStep }
    else if (cr <= safeStep) { cr = 255; cb = 255-safeStep }
    else if (cr < 255)    { cr -= colorStep }
    */
    // more manly colors
    if      (cb >= 255) { cb = 0; cg = safeStep }
    else if (cb > 0)    { cb += colorStep }
    else if (cg >= 255) { cg = 0; cr = safeStep }
    else if (cg > 0)    { cg += colorStep }
    else if (cr >= 255) { cr = 0; cb = safeStep }
    else if (cr > 0)    { cr += colorStep }
    cell.style.backgroundColor = 'rgb('+ cr +', '+ cg +', '+ cb +')';

    if (cell.attributes['x'] && cell.attributes['y']) {
//        alert(cell.attributes['x'].value +', '+ cell.attributes['y'].value)
//    if (cell.innerHTML && /^(\d+),(\d+)$/.test(cell.innerHTML)) {
        // force string mode with ''+ so we create an associative array
        // <http://persistent.info/archives/2004/08/27/js-associative-arrays>
        // instead of wasting all that array space
        var i1 = ''+cell.attributes['x'].value
        var i2 = ''+cell.attributes['y'].value
        if (!colorCache[i1]) { colorCache[i1] = [] }
        colorCache[i1][i2] = cr +','+ cg +','+ cb;
    }
  }
}


