var Tree = function (){return this.init.apply(this, arguments)};
Tree.prototype = {
    init: function (id){
        this.stack = [];
        this.tree = new YAHOO.widget.TreeView(id);
        this.parent = this.tree.getRoot();
        this.tail = null;
    },
    append: function(text){
        this.tail = new YAHOO.widget.TextNode(text, this.parent, false);
    },
    push: function(){
        this.stack.push(this.parent);
        this.parent = this.tail;
    },
    pop: function (){
        this.tail = this.parent;
        this.parent = this.stack.pop();
    },
    declare: function (lambda){
        try {
            lambda.apply(this);
        } catch(e) {
            throw Error(e.message);
        }
    },
    view: function (){
        this.tree.draw();
    }
};
YAHOO.util.Event.addListener(window, 'load', function (){
    var Dom = YAHOO.util.Dom;
    var tree = new Tree('treeView');
    try {
        tree.declare(treeDef);
        tree.view();

        var appended = false;
        var $ = function(id){return document.getElementById(id)};
        YAHOO.util.Event.addListener('showXml', 'click', function (){
            if(!appended){
                var xmlTree = $('xmlTree');
                var xmlView = $('xmlView');
                xmlView.appendChild(xmlTree);
                Dom.setStyle('xmlTree', 'display', 'block');
                appended = true;
            }
            var display = Dom.getStyle('xmlView', 'display');
            if(display == 'none'){
                Dom.setStyle('xmlView', 'display', 'block');
            } else {
                Dom.setStyle('xmlView', 'display', 'none');
            }
        });
    } catch(e){
        Dom.setStyle('phptree', 'display', 'none');
    }
});

