// Debug.js v01.2 by Jon.
// Opens a debugging window and facilitates writing to it.
// Especially useful for alerting variables inside loops.
/*" _______________ HOW TO USE THIS SCRIPT _________________ "*/
/*" CHANGE EXAMPLES TO REFLECT UPGRADE! "*/
/*
1. Add to html head:
    <script type="text/javascript" language="JavaScript" src="tech/debug.js"></script>
    <script language="JavaScript">
     openDebugWindow( { openerWindow: this, debugIsOn: true , purgeOldReports: false }) ;
    </script>
2. Put the function call in a loop or wherever. Examples:
    HEADING: debug("'makeMovingDivs()'", {importance: 10}) ;
    debug( myImportantVariable, {importance: 4} ) ;
    debug( "isFarEnoughHorizontally", { styleString: "color: turquoise; font-size: 20px ; " } ) ;
3. To report name and value of a local variable, create a temporary global:
    _this = this ; debug("_this")
4. You'll get the name and value of a variable unless you double-quote ( "  ' a nice headline ' " ) the argument. Example:
    debug("'makeMovingDivs()'", {importance: 10}) ;
*/
// Prepare the window.
debugWindowOpenedBefore = false ;
function openDebugWindow(argsObj) { //openerWindow [usually "this"], debugIsOn, purgeOldReports
    debugWindowOpenedBefore = true ;
    debugIsOn = argsObj.debugIsOn ;
    purgeOldReports = argsObj.purgeOldReports ;
    if (debugIsOn) {
        openerWindow = argsObj.openerWindow ;
        openerWindowTitle = (openerWindow.document.title == "")? openerWindowTitle = "untitled window" : openerWindow.document.title ;
        debugWinVar = window.open("","debugWinName","width=400,height=800,resizable,scrollbars,left=-50") ;
        debugWinVar.document.write("<html><head><title>Debugging: " + openerWindowTitle + "</title></head><body>") ;
        if (purgeOldReports) {
            // The following line does not allow multiple reports per window.
            debugWinVar.document.close() ;
        }
        var currentTimeObj =  new Date() ;
        debugWinVar.document.write( "<hr />NEW REPORT AT " + currentTimeObj.getHours() + ":" +  currentTimeObj.getMinutes() + ":" +  currentTimeObj.getSeconds() + "<br />" );
        debugWinVar.focus() ;
    }
}
// Report an event to that window.
function debug(lineToWriteArg, optionsObj) {
    if (!debugIsOn) return ;
    var styleString = "font-family: arial ; font-size: 12px ; font-weight: bold ; " ;
    var lineToWrite = lineToWriteArg ;
    // CONDITIONALS WOULD HAVE TO GO ABOVE.
    if (lineToWrite != "'" + eval(lineToWrite) + "'") {
        lineToWrite += ": <span style='color: purple ;'>" + eval(lineToWrite) + "</span>" ;
    }
    else {
        styleString += " color: wheat ; background-color: dimgray ; " ;
    }
    if (optionsObj) {
        if (optionsObj.importance) {
            switch (optionsObj.importance) { // 10 is highest.
                case 0:
                    break ;
                case 1:
                    styleString += "color: blueviolet" ;
                    break ;
                case 2:
                    styleString +="margin-left: 15px ; color: dodgerblue ; " ;
                    break ;
                case 3:
                    styleString +="margin-left: 15px ; color: darkcyan ; " ;
                    break ;
                case 4:
                    styleString +="margin-left: 10px ; color: olive ; font-size: 13px ; " ;
                    break ;
                 case 5:
                    styleString +="margin-left: 10px ; color: teal ; font-size: 13px ; " ;
                    break ;
                case 6:
                    styleString += "margin-left: 5px ; color: orange; font-size: 14px ; " ;
                    break ;
                case 7:
                    styleString +="margin-left: 5px ; color: orangered ; font-size: 14px ; " ;
                    break ;
                case 8:
                    styleString +="margin-left: 0px ; color: red ; font-size: 16px ;" ;
                    break ;
                case 9:
                    styleString +="margin-left: 0px ; color: maroon ; font-size: 20px ;" ;
                    break ;
                case 10:
                    styleString +="margin-left: 0px ; color: white ; background-color: black; font-size: 24px ;" ;
                    break ;
           }
        }
        if (optionsObj.styleStringCustom) {
            styleString += optionsObj.styleStringCustom ;
        }
    }
    // if (!optionsObj.noBreak) {
    lineToWrite += "<br />"
    // }
    lineToWrite = "<span style=' margin-left: 20px; " + styleString + " '>" + lineToWrite + "</span>" ;
    if (!debugWindowOpenedBefore) {
        alert("debug.js hasn't been properly initialized. Check the instructions in debug.js or remove all debug.js references from your html code.") ;
        document.write("Exited document.") ;
        return ;
    }
    if (typeof debugWinVar == "undefined" || typeof debugWinVar.document == "undefined" ) { // It was opened but is now closed.
        openDebugWindow({ openerWindow: openerWindow, debugIsOn: debugIsOn , purgeOldReports: purgeOldReports }) ;
    }
    debugWinVar.document.writeln(lineToWrite) ;
}
/* TO IMPLEMENT
Flags:
-b follow with a line break and a border ________
-1, -2 importance
-n no line break

function listProps(obj,targetDiv){
	htmlVar = ""
	for(i in obj){
		htmlVar += i+"<br />" 
	}
	document.getElementById(targetDiv).innerHTML = htmlVar
}

*/