﻿
Entegris.Navigation.LeftNavigation = function(CurrentPageID, expandToPath){
    this.CurrentPageID = CurrentPageID;
    this.seed = 0;
    this.cssSeed = 0;
    this.expandToPath = expandToPath;

         
};
Ext.extend(Entegris.Navigation.LeftNavigation, Ext.util.Observable, {
    Initialize : function(){   
    
        var tree = new Ext.tree.TreePanel('LeftNavigationTree', {
            animate:true,
            containerScroll: true,
            lines:false,
            rootVisible:false,
            loader: new Ext.tree.TreeLoader()
        });

        //tree.el.addKeyListener(Ext.EventObject.DELETE, removeNode);

        var root = new Ext.tree.TreeNode({  id: 'root',
                                            text: 'Left Navigation',
                                            cls: 'root'});
        tree.setRootNode(root);
        Ext.select('#left_navigation > ul > li').each(this.BuildTier.createDelegate(this, [root, null], 1));
        
        tree.on('beforeexpand', this.onBeforeExpand, this);
        
        tree.render();
        root.expand();
        
        if (this.expandToPath) {
            tree.expandPath(this.expandToPath);
        }
        tree.un('beforeexpand', this.onBeforeExpand, this);
        
    },
    
    onBeforeExpand: function( node, deep, anim) {
         node.ui.addClass("AncestorOfCurrentPage");        
    },
    
    BuildTier: function(li, parent, cssClass) {
        var isTopTier = false;
        if (!cssClass) {
            cssClass = 'LeftNavigation' + (this.cssSeed % 2 == 0 ? 'Even' : 'Odd');
            this.cssSeed++;
            isTopTier = true
        }
    
        var thisCssClass = cssClass + (isTopTier ? ' TopTier' : '')
        var child = new Ext.tree.TreeNode({ id: li.dom.attributes.pageid.value,
                                            text: li.dom.attributes.label.value,
                                            cls: thisCssClass,
                                            href: li.dom.attributes.href.value});
        parent.appendChild(child);
        /*
        try {
            if (li.dom.attributes.PageID.value == this.CurrentPageID) {
                this.expandToPath = child.getPath();
            }
        } catch (ex) {}
        */
        this.seed++;
        
        li.select('> ul > li').each(this.BuildTier.createDelegate(this, [child, cssClass], 1));
    }
    
    
    
    
    
    
    
});