/* debug.js - Paul Seamons - License BSD - Copyright 2007 */
/* $Id: debug.js,v 1.14 2010-02-04 03:34:30 paul Exp $ */

var debug_span_index = 0;
function debug(el){
  var win = top.window.open('','debug_output','scrollbars=yes');
  var dw = function (t) { win.document.write(t) };
  debug_text(el, dw);
  win.document.close();
  if (win.focus) win.focus();
}

function debug_text (el, dw) {
  var is_ie = navigator.appVersion.indexOf('MSIE') != -1;
  debug_span_index = 0;

  var T = '';
  if (! dw) dw = function (t) { T += t }

  dw('<title>debug ouput for '+el+'</title><font size=2><pre style="color:green">');
  dw('<scr'+'ipt>function debug_toggle (id) { var el = document.getElementById(id); el.style.display = (el.style.display == "none") ? "inline" : "none" }</scr'+'ipt>');
  _debug(dw, el, '\t', is_ie, '')
  dw('</pre></font>');
  dw('--------- Done with Debug-------------\n');

  return T;
}

function _debug(dw, el, ind, is_ie, topkey) {
  if (ind.length > 7) { dw('[ too deep ]\n'); return; }
  if (""+el == 'null') { dw('null'); return }

  var a = [];
  var is_array;
  if (Array.prototype.isPrototypeOf(el)) {
    for (var i = 0; i < el.length; i++) a.push(i);
    is_array = true;
  } else if (el.substring) {
    dw('"'+el+'"');
    return;
  } else {
    if (topkey == 'form'
        && el.nodeName
        && el.nodeName == 'FORM') { dw("[ FORM ]"); return }
    var used = {};
    for (var i in el) if (! used[i]++) a.push(i);
    a = a.sort();
  }

  dw(is_array ? '[' : '{');
  if (a.length) {
    var id = 'debug_span'+(debug_span_index++);
    dw("   /* <a href=javascript:debug_toggle('"+id+"')>Hide</a> */ ");
    dw("<span id="+id+">\n");
  }

  for (var i = 0; i < a.length; i++) {
    var key = a[i];
    var val = (key == 'filters') ? '' : el[key];
    dw(is_array ? ind : ind + key + ' : ');

    if (key == 'filters')                   dw("[ IE css filters ]");
    else if ((""+key).match(/^[A-Z_]+$/)  ) dw(""+val);
    else if (key == 'opsProfile'  && is_ie) dw("[ IE opsProfile ]");
    else if (key == 'userProfile' && is_ie) dw("[ userProfile ]");
    else if (key == 'external'    && is_ie) dw("[ external ]");
    else if (key == 'enabledPlugin'       ) dw("[ enabledPlugin ]");
    else if (key == 'applicationCache'    ) dw("[ applicationCache ]");
    else if (key == 'localStorage'        ) dw("[ localStorage ]");
    else if (typeof(val) == 'function'    ) dw("[ function ]");
    else if ((""+key).match(/^(content|controllers|globalStorage|history|location|java|window)$/) && (""+topkey).match(/^(window|content|contentWindow|)$/)) dw("[ nested "+topkey+" "+key+" ]");
    else if ((""+key).match(/^(selection|styleSheets|currentStyle|runtimeStyle|attributes)$/) && is_ie) dw(""+val);
    else if ((""+key).match(/^(Image|Option|XMLHttpRequest|XMLSerializer|DOMParser)$/) && navigator.appName.match(/konqueror/i)) dw("[ "+key+" ]");
    else if ((""+key).match(/(all|elements|frames|filter|document|parent|child|owner|sibling|self|top|view)/i)) dw(""+val);
    else if ((""+key).match(/(activeElement|forms|target|body|links|scripts|images|sun|Components|Packages|DadWindow)/i)) dw(""+val);
    else if (typeof(val) == 'number'      ) dw(""+val);
    else if (typeof(val) == 'string'      ) dw('"'+val.replace(/\n[\S\s]*$/,'...').replace(/</g,'&lt;')+'"');
    else if (typeof(val) == 'boolean'     ) dw(val ? "true" : "false");
    else if (topkey == 'attributes'       ) dw("{ /*attributes*/ value: \""+(typeof(val) == 'object' ? val.value : val)+"\" }");
    else if (typeof(val) == 'undefined'   ) dw("undefined");
    else if (typeof(val) == 'object'      ) _debug(dw, val, ind+'\t', is_ie, key);
    else                                    dw(" typeof "+typeof(val));

    dw(i == a.length - 1 ? "\n" : ",\n");
  }

  if (a.length) {
    ind = ind.substring(0, ind.length-1);
    dw(ind)
    dw("</span>");
  }
  dw(is_array ? ']' : '}');
}
