// GREAT JOB!
// GREAT JOB!
// GREAT JOB!

function getViewport()
{
    viewport = {
        'width': window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
        'height': window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
    };
    return viewport;
}

function zeroFill( number, width )
{
    width -= number.toString().length;
    if ( width > 0 )
    {
        return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
    }
    return number;
}

function pos2Color(mousePos)
{
    var viewport = sukurimu_viewport;

    var x = Math.round((mousePos.x*255)/viewport.width);
    var y = Math.round((mousePos.y*255)/viewport.height);

    var r = zeroFill(x.toString(16), 2);
    var g = zeroFill(y.toString(16), 2);
    var b = zeroFill((255-y).toString(16), 2);
    return '#' + r + g + b;
}

function paintmePretty(ev)
{
    ev = ev || window.event;
    var color = pos2Color(mouseCoords(ev));

    document.body.style.backgroundColor = color;
    changeStyle(color);
}

function mouseCoords(ev)
{
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };
}

function changeStyle(color)
{
    var theRules = new Array();
    if (document.styleSheets[0].cssRules) {
        theRules = document.styleSheets[0].cssRules;
    } 
    else if (document.styleSheets[0].rules) {
        theRules = document.styleSheets[0].rules;
    }
    for (n in theRules)
    {
        if (theRules[n].selectorText == 'th a, th a:hover, th a:visited' || theRules[n].selectorText == '.colored' || theRules[n].selectorText == 'a:hover')   {
            theRules[n].style.color = color;
        }
    }
}

var sukurimu_viewport = getViewport();
document.onmousemove = paintmePretty;

