/**
 * Created by IntelliJ IDEA.
 * User: Viech
 * Date: 04.10.2010
 * Time: 23:23:37
 * To change this template use File | Settings | File Templates.
 */

jQuery.navigator = function(obj, name) {
	this.init(obj, name);
};

jQuery.extend(jQuery.navigator, {
	prototype: {
		init: function(obj, name) {
            $('body').prepend('<div id="debug" style="height:100%;position:fixed;right:0;top:0;z-index:9999;width:500px;overflow-y:scroll;background:#eee;border-left:5px solid #aaa;color:#000;font-family:monospace;font-size:12px;letter-spacing: 0;word-spacing:0;"></div>');
            $('#debug').append('<div id="debugInner" style="height:100%;position:relative;width:500px;"></div>');
            $('#debugInner').append('<div id="debugHeader" style="border-bottom:1px solid #666;height:50px;padding:2px 2px 5px 10px;position:fixed;background:#bbb;width:500px;z-index:99999;font-size:14px;font-weight:bold;"></div>');
            $('#debugInner').append('<div id="debugContent" style="padding-top:48px;"></div>');

            jQuery.navigator.depth = 0;
            jQuery.navigator.objects = [{'obj': obj, 'name': name}];
            jQuery.navigator.debug(jQuery.navigator.objects, jQuery.navigator.depth);

            $('.debugDeeper').live('click', function() {
                var href = $(this).attr('href');
                    href = href.substr(1);
                jQuery.navigator.depth++;
                jQuery.navigator.objects[jQuery.navigator.depth] = {'obj': jQuery.navigator.objects[jQuery.navigator.depth-1]['obj'][href], 'name': href};
                jQuery.navigator.debug(jQuery.navigator.objects, jQuery.navigator.depth);
                return false;
            });

            $('.debugBack').live('click', function() {
                var href = $(this).attr('href');
                var newDepth = href.substr(1) - 0;
                var newObjects = [];
                for (i=0; i<= newDepth; i++) {
                    newObjects[i] = jQuery.navigator.objects[i];
                }
                jQuery.navigator.depth = newDepth;
                jQuery.navigator.objects = newObjects;
                jQuery.navigator.debug(jQuery.navigator.objects, jQuery.navigator.depth);
                return false;
            });
        }
	},

    debug: function(objects, depth) {
        jQuery.navigator.setHeader(objects);
        $('#debugContent').html('');
        var obj = objects[depth]['obj'];
        jQuery.navigator.sortDebug(obj);
    },

    sortDebug: function(obj) {
        var sorted = [];
        for (var i in obj) {
            sorted[typeof(obj[i])] = [];
        }
        for (var i in obj) {
            sorted[typeof(obj[i])][i] = obj[i];
        }
        for (var i in sorted) {
            $('#debugContent').append('<h4 style="border-bottom:1px solid #aaa;margin-top:10px;padding:2px;color:#000;font-weight:bold;background:#ccc">'+i+'</h4>');
            for (var j in sorted[i]) {
                switch (i) {
                    case 'object':
                        jQuery.navigator.output('<a href="#' + j + '" class="debugDeeper"><strong>' + j + ':</strong> ' + sorted[i][j] + '</a>');
                        break;
                    case 'string':
                        jQuery.navigator.output('<strong>' + j + ':</strong> ' + jQuery.navigator.htmlspecialchars(sorted[i][j]));
                        break;
                    case 'function':
                    case 'number':
                    default:
                        jQuery.navigator.output('<strong>' + j + ':</strong> ' + sorted[i][j]);
                        break;
                }
            }
        }
    },

    setHeader:  function(objects) {
        $('#debugHeader').html('');
        for (var i in objects) {
            $('#debugHeader').append('<a href="#' + i + '" class="debugBack">' + objects[i]['name'] + '</a> ');
        }
    },

    output:  function(txt) {
        $('#debugContent').append('<p style="border-bottom:1px solid #ccc;padding:2px 2px 2px 10px;color:#000;">'+txt+'</p>');
    },

    htmlspecialchars: function(str,typ) {
        if(typeof str=="undefined") str="";
        if(typeof typ!="number") typ=2;
        typ=Math.max(0,Math.min(3,parseInt(typ)));
        var html=new Array();
        html[38]="&amp;"; html[60]="&lt;"; html[62]="&gt;";
        if(typ==1 || typ==3) html[39]="&#039;";
        //if(typ==2 || typ==3) html[34]="&quot;";
        for(var i in html) eval("str=str.replace(/"+String.fromCharCode(i)+"/g,\""+html[i]+"\");");
        return str;
    }
});


