function addEvent(obj, type, fn) {
    if (obj.addEventListener) obj.addEventListener(type, fn, false);
    else obj.attachEvent('on' + type, fn);
}

function initSpaceTree(project, json){
    function get(id) {
      return document.getElementById(id);  
    };
    var infovis = document.getElementById('infovis');
    var w = infovis.offsetWidth, h = infovis.offsetHeight;
    //init canvas
    //Create a new canvas instance.
    canvas = new Canvas('mycanvas', {
        'injectInto': 'infovis',
        'width': w,
        'height': h,
        'backgroundColor': '#fff'
    });
    //end
    
    //init st
    //Create a new ST instance
    st = new ST(canvas, {
        //set duration for the animation
        duration: 400,
        //set animation transition type
        transition: Trans.Quart.easeInOut,
        //set distance between node and its children
        levelDistance: 50,
        //set node and edge styles
        //set overridable=true for styling individual
        //nodes or edges
        Node: {
            height: 20,
            width: 120,
            type: 'rectangle',
            color: '#fff',
            align: 'center',
            overridable: true
        },
        
        Edge: {
            type: 'bezier',
            overridable: true
        },
        
        //This method is called on DOM label creation.
        //Use this method to add event handlers and styles to
        //your node.
        onCreateLabel: function(label, node){
            label.id = node.id;            
            label.innerHTML = node.name;
            label.onclick = function(){
                st.onClick(node.id);
            };
            //set label styles
            var style = label.style;
            style.width = 120 + 'px';
            style.height = 17 + 'px';            
            style.cursor = 'pointer';
            style.color = '#222';
            style.fontSize = '0.8em';
            style.textAlign = 'center';
            style.paddingTop = '3px';
        },
        
        //This method is called right before plotting
        //a node. It's useful for changing an individual node
        //style properties before plotting it.
        //The data properties prefixed with a dollar
        //sign will override the global node style properties.
        onBeforePlotNode: function(node){
            //add some color to the nodes in the path between the
            //root node and the selected node.
            if (node.selected) {
                node.data.$color = "#ccc";
            }
            else {
                delete node.data.$color;
                var GUtil = Graph.Util;
                //if the node belongs to the last plotted level
                if(!GUtil.anySubnode(node, "exist")) {
                    //count children number
                    var count = 0;
                    GUtil.eachSubnode(node, function(n) { count++; });
                    //assign a node color based on
                    //how many children it has
                    if (count > 0) {
                      node.data.$color = '#ee8';
                    }
                    else {
                      node.data.$color = '#e5e5e5'
                    }
                }
            }
        },
        
        //This method is called right before plotting
        //an edge. It's useful for changing an individual edge
        //style properties before plotting it.
        //Edge data proprties prefixed with a dollar sign will
        //override the Edge global style properties.
        onBeforePlotLine: function(adj){
            if (adj.nodeFrom.selected && adj.nodeTo.selected) {
                adj.data.$color = "#38a";
                adj.data.$lineWidth = 3;
            }
            else {
                delete adj.data.$color;
                delete adj.data.$lineWidth;
            }
        },
        
        onBeforeCompute: function(node) {
          function pathify(str) { return str.replace('::', '/'); }
          function capitalize(str) { 
            return str.replace(/^(.)/, function(a) { 
              return a.charAt(0).toUpperCase(); 
            }); 
          }
          
          var url = "http://yardoc.org/docs/" + project + "/" + escape(pathify(node.id));
          var html = "<h4>" + node.data.type.small() + "<br/>" + node.id + "</h4>";
          html += ("View " + node.data.type + " docs").link(url).small().italics();
          for (var type in {'class':0, 'instance':1}) {
            if (node.data[type + '_methods'].length > 0) {
              html += "<h5>" + capitalize(type) + " Methods</h5><ul>"
              for (var idx in node.data[type + '_methods']) {
                var meth = node.data[type + '_methods'][idx];
                var path = pathify(node.id) + (type == 'instance' ? ':' : '.') + escape(meth);
                var url = "http://yardoc.org/docs/" + project + "/" + path;
                html += "<li>" + meth.link(url) + "</li>";
              }
              html += "</ul>"
            }
          }
          get('data').innerHTML = html;
        }
    });
    //load json data
    st.loadJSON(json);
    //compute node positions and layout
    st.compute();
    //optional: make a translation of the tree
    st.geom.translate(new Complex(-400, 0), "startPos");
    //emulate a click on the root node.
    st.onClick(st.root);
    //end
}
