/*****************************************************************************
    debug helpers

    (c) 2008 arkadiusz juszczyk <arkadiusz@waehrendwahr.de>
*****************************************************************************/

/**
    alert given message in pages

    @param  message long message with linebreaks
    @param  maxLines optional lines per screen, default 50
 */
function alertPager(message, maxLines) {
    if(maxLines == null) maxLines = 50;

    var mLines = message.split("\n");
    var mPages = [];
    while(mLines.length > 0) {
        mPages.push( mLines.splice(0,maxLines).join("\n") );
    }
    for(var i=0; i<mPages.length-1; i++) {
        if(!confirm(mPages[i])) return;
    }
    alert(mPages[mPages.length-1]);
}


/**
    return string representation of obj

    @param  obj     object to dump
    @param  name    name of obj, default "obj"
    @param  recursion   recursion depth, default 0, -1 = infinite
    @param  functions   include functions, default false
    @return printable string
 */
function dumpobj(obj, name, recursion, functions) {
    if( name == null ) name = "obj";
    if( recursion == null ) recursion = 0;
    if( functions == null ) functions = false;
    
    var ret = "";
    if( typeof(obj) == 'object' ) {
        for( var i in obj ) {
            var nameTag = name;
            if( parseInt(i) == i ) nameTag += "["+i+"]";
            else nameTag += "."+i;
            var val;
            try {
                val = obj[i];
            } catch( error ) {
                val = "[error] " + error;
            }
            if( typeof(val) == 'function' ) {
                if( !functions ) continue;
                ret += nameTag + " = " + "[function]\n";
            } else if( typeof(val) == 'object' ) {
                if( recursion != 0 ) {
                    ret += dumpobj(val, nameTag, recursion-1);
                } else {
                    ret += nameTag + " = " + val + "\n";
                }
            } else {
                ret += nameTag + " = '" + val + "' [" + typeof(val) + "]\n";
            }
        }
    } else {
        ret = name + " = '" + obj + "' [" + typeof(obj) + "]\n";
    }
    return ret;
}


