/* -------------------------------------------------------------------------------------------------------

    Function to write data to a debug window overlayed on the map for V3

    Originally based on code found on maps.forum.nu by Marcelo -

    The position and size of the debug window can be changed within the code.

    Usage
        in Document: <script type="text/javascript" src="{your location/debug.js">
        After map is loaded (after statement something like "map = new google.maps.map(..."
        once only call function debug - this will initialise the debug window
            debug('<h3>Debug Window</h3>',false);
            
        Then, wherever you wish to send debug data to the control insert a statement:
            dbg({test or variables);
            
        If dbg() is called but the initialisation(debug) () function has not yet been called then no errors
        will occur - but no debug data will occur either
        
        An example can be seen at http://otus1.oceantracker.net/tracker/...

------------------------------------------------------------------------------------------------------------- */ 
function debug(str,append,draggable) {

    var brk, msg;
    brk = '<br //>';
    
	append = typeof(append) == 'undefined' ? 1 : append;
    dragDiv = typeof(draggable) == 'undefined' ? 0 : draggable;
     
    if (! (typeof(window.dbgDiv) == 'undefined')) {
        if (append) 
            { msg = $(window.dbgDiv).html() + brk + '<span style="color:red">' + str + '</span>'; }
         else 
            { msg =  '<span style="color:red">' + str + '</span>'; }    
        $(window.dbgDiv).html(msg);
    }
    else {
        window.dbgDiv = document.createElement('div');
        $(window.dbgDiv).attr('id','dbgDiv');
        $(window.dbgDiv).css({
                color:"#0000ff",
                backgroundColor:"#ffffff",
                width:"800px",
                height:"120px",
                padding:"2px 3px 0px 3px",
                border:"1px solid red",
                overflow:"auto",
                cursor:"text"
        });

        if (dragDiv) {
            $(window.dbgDiv).css({cursor: "text"});      
            $(window.dbgDiv).draggable();
        }
        
        $(window.dbgDiv).html(str); 
        
        map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(window.dbgDiv);     
    }    
}

function dbg(str,append,draggable) {
//	append = typeof(append) == 'undefined' ? 1 : append;    
//   ts = typeof(ts) == 'undefined' ? 1 : ts;
//    draggable = typeof(draggable) == 'undefined' ? 1 : draggable;
    if (! (typeof(window.dbgDiv) == 'undefined')) {
        append = typeof(append) == 'undefined' ? 1 : append;    
        ts = typeof(ts) == 'undefined' ? 1 : ts;
        draggable = typeof(draggable) == 'undefined' ? 1 : draggable;    
        debug(str,append,ts,draggable);
    }
}

function dbgExists() {
    if (typeof(commandLineArgs['debug']) == 'string' &&
        commandLineArgs['debug'] == 'true') {
            console.log('Debug is on');
            retVal = true;
    }
    else {
        console.log('debug is off');
        retVal = false;
    }    
    return retVal;
}

function dbgTS(str,append,draggable) {
    dbgDate = new Date();
    hrs = dbgDate.getHours();
    mins = (dbgDate.getMinutes() < 10 ? "0" : "") + dbgDate.getMinutes();
    secs = (dbgDate.getSeconds() < 10 ? "0" : "") + dbgDate.getSeconds();
    dbgTime = hrs+":" + mins + ":" + secs;
    msg = dbgTime + ' ' + str;
    dbg(msg,append,draggable);
}
