﻿var DEBUG = false;
function debug(node, depth){
    indent = 0;
    if (depth == undefined)
        depth = -1;
    debug_rec(node, depth);
}
function debug_rec(node, depth){
    var s = "";
    s += "ID: " + node.id  + "\n";
    s += "CLASS: " + node.className + "\n";
    s += "LEFT: " + node.getStyle("left") + "\n";
    s += "TOP: " + node.getStyle("top") + "\n";
    s += "POSITION: "+ node.getStyle("position") + "\n";
    s += "GETWIDTH: "+ node.getWidth() + "\n";
    s += "GETHEIGHT: "+ node.getHeight() + "\n";
    s += "CLIENTWIDTH: "+ node.clientWidth + "\n";
    s += "CLIENTHEIGHT: "+ node.clientHeight + "\n";
    s += "VISIBLE: "+ node.visible() + "\n";
    s += "INSPECT: "+ node.inspect() + "\n";
    debugPrint(s, indent);
    indent++;
    depth--;
    if (depth ==0)
        return;
    node.childElements().each(function(elem, index){
        debug_rec(elem);
    });
    indent--;
}
function debugPrint(text, indent){
    if (!DEBUG)
        return;
    var w = window.open("", "debug");
    if (indent ==undefined)
        indent = 0;
    var borderColor = darken(genColor());
    var backColor = lighten(borderColor);
    text = text.escapeHTML();
    text = text.replace(/\n/g, "<br/>");
    w.document.writeln("<p style='border:1px solid " + borderColor + ";padding-left:" + (indent * 10) + "px'>" + text + "</p>");
}
function genColor(){
    var ret = "#" + Math.round(Math.random()* 255).toString(16) + Math.round(Math.random()* 255).toString(16) + Math.round(Math.random()* 255).toString(16);
    return ret;
}
function darken(color){
    var red = parseInt(color.substr(1, 2), 16);
    var green = parseInt(color.substr(3, 2), 16);
    var blue = parseInt(color.substr(5, 2), 16);
    var diff = Math.min(255-red, Math.min(255-green, Math.min(255-blue, 30)));
    red += diff;
    green+=diff;
    blue += diff;
    return "#"+red.toString(16) + green.toString(16) + blue.toString(16);
}
function lighten(color){
    var red = parseInt(color.substr(1, 2), 16);
    var green = parseInt(color.substr(3, 2), 16);
    var blue = parseInt(color.substr(5, 2), 16);
    var diff = Math.min(red, Math.min(green, Math.min(blue, 60)));
    red  -= diff;
    green-=diff;
    blue -= diff;
    return "#"+red.toString(16) + green.toString(16) + blue.toString(16);
}
function TimerStart(id){
    if (!DEBUG)
        return;
    if (typeof timerData == "undefined"){
        timerData = new Hash();
    }
    var t = new Date().getTime()
    timerData.set(id, t);
}
function TimerStop(id){
    if (!DEBUG)
        return;
    var out = "";
    var stop = new Date().getTime();
    if (typeof timerData == "undefined"){
        out = "Timer not started for " + id;
        return;
    }  
    if (timerData.keys().indexOf(id) <0){
        out = "Timer not started for " + id;
        return;
    }
    var start = timerData.get(id);
    var elapsed = stop-start;
    var sum = timerData.get(id+"_sum");
    if (typeof sum == "undefined")
        sum = 0;
    var count = timerData.get(id+"_count");
    if (typeof count == "undefined")
        count = 0;
    timerData.set(id+"_sum", sum + elapsed);
    timerData.set(id+"_count",count  + 1);    
    timerData.unset(id);
    debugPrint("Timer " + id + " elapsed: " + elapsed + " (avg: "+ (sum+elapsed)/(count+1)+")");
}