CobjPanel = function() {
    CobjPanel.superclass.constructor.call(this, {
        id:'api-tree',
        region:'west',
		layout:'fit',
        split:true,
        width: 280,
        minSize: 175,
        maxSize: 500,
        collapsible: true,
        margins:'0 0 5 5',
        cmargins:'0 0 0 0',
        rootVisible:false,
        lines:!vt,
		useArrows:vt,
        autoScroll:true,
        animCollapse:true,
        animate: true,
		hlColor:'#EEEEEE',
        collapseMode:'mini',
        loader: new Ext.tree.TreeLoader({
			dataUrl:nodesURL,
			baseParams: {isroot:false,so:statusOn},
			id:'ScoreTL'
		}),
        root: new Ext.tree.AsyncTreeNode({
            text: 'Dashboard',
        	draggable:false,
			uiProvider:Ext.repaired.RootTreeNodeUI,
			expanded: false,
        	id:rootId,
			isroot:true
         }),
        collapseFirst:false,
		tbar: [
			{
				iconCls: 'icon-expand-all',
				tooltip: 'Expand All',
				handler: function(){ cobjPnl.expandAll(); }
			},
			{
				iconCls: 'icon-collapse-all',
				tooltip: 'Collapse All',
				handler: function(){ cobjPnl.collapseAll(); }
			},
				'-',
			{
				iconCls: 'panic_R',
				tooltip: 'Switches this view between normal mode and status mode',
				text: 'Status',
				enableToggle: true,
				hidden: !showStatus,
				pressed: statusOn,
				toggleHandler : function(b, pressed){
					statusOn = pressed;
					cobjPnl.root.getLoader().baseParams.so = statusOn;
                    cobjPnl.root.reload();
                }
			}
		],
		bbar: [
			   {
				   text:'<a href="http://www.covalentsoftware.com/" target="_blank">Powered by Covalent</a>',
				   xtype: 'tbtext'
			   }
			   ]
    });
	
	var maskEl = 'container';  //pick your poison ( body, container, etc..)
	
	this.root.getLoader().on({
					 'load' : function(loader, node){
						   // convert attributes.children to childNodes without rendering (optional)
							   node.eachChild(loader.doPreload, loader); 
					  }
					 ,'beforeload': function(loader, node,callback ){
							  //enrich your loader request params...
					  }
					 ,scope:this
					 ,single:true   //only loading this tree once, so..
				 });
	
	this.root.on({
					 'beforeload': function( node){
						 node.getOwnerTree()[maskEl].mask('Loading Tree<br />Please Wait...', 'x-mask-loading');
					  }
					 ,'expand': function( node){
						  node.getOwnerTree()[maskEl].unmask();
					   }
					,single:false
				 });
	
	this.on('render', function() {
		if (autoExpand) {
			this.expandAll();
		} else {
			this.root.expand();
		}
		if (showLogo) {
			this.getBottomToolbar().destroy();
		}
	});
};

Ext.extend(CobjPanel, Ext.tree.TreePanel, {
	selectNode : function(node){
		var path = node.getPath();
		var uuid = '&uid='+getTimeStamp();
		//console.log('iconCls',node.attributes.iconCls);
		if (node.isLeaf()) {
			
			switch (node.attributes.objType) {
				case 'KPI':
					mainPanel.loadClass(piDashURL+'?id='+node.id+uuid, node.id, node.attributes.code, path, node.attributes.iconCls);
					break;
				case 'ACT':
					mainPanel.loadClass(actDashURL+'?id='+node.id+uuid, node.id, node.attributes.code, path, node.attributes.iconCls);
					break;
				default:
					break;
			}
			
		} else {
			mainPanel.loadClass(dashSumURL+'?id='+node.id+'&p='+path.replace(/\//g, ".")+uuid, node.id, node.attributes.code, path, node.attributes.iconCls);
			node.expand();
		}
	},
	
	selectNodeById : function(id){
		var node = this.getNodeById(id);
		if (node) {
			this.selectNode(node);
		}
	},
	
	selectNodeByPath : function(path) {
		this.selectPath(path, null, function(bSuccess, oSelNode) {
			cobjPnl.selectNode(oSelNode);
		});
	}
});

DocPanel = Ext.extend(Ext.Panel, {
    closable: true,
    autoScroll:true,

    initComponent : function(){
        /*var ps = this.cclass.split('.');
		var title = ps[ps.length-1];
		title = title.replace(/</g, "&lt;");
		title = title.replace(/>/g, "&gt;");
		title = title.replace(/’/g, "&#8217;");
        this.title = title;*/
        DocPanel.superclass.initComponent.call(this);
    }
});


MainPanel = function(){
	
    MainPanel.superclass.constructor.call(this, {
        id:'doc-body',
        region:'center',
        margins:'0 5 5 0',
        minTabWidth: 135,
        plugins: new Ext.ux.TabCloseMenu(),
        enableTabScroll: true,
        activeTab: 0
    });
};

Ext.extend(MainPanel, Ext.TabPanel, {

    initEvents : function(){
        MainPanel.superclass.initEvents.call(this);
    },

    loadClass : function(href, cls, title, path, iconCls){
		/*title = title.replace(/&lt;/g, "<");
		title = title.replace(/&gt;/g, ">");
		title = title.replace(/&#8217;/g, "’");*/
        var id = 'docs-' + cls;
        var tab = this.getComponent(id);
        if(tab){
            this.setActiveTab(tab);
        }else{
            var autoLoad = {url: href, scripts: true};
            var p = this.add(new DocPanel({
                id: id,
                cclass : cls,
				title : title,
                autoLoad: autoLoad,
				path: path,
                iconCls: iconCls
            }));
            this.setActiveTab(p);
		}
    }
});

var mainPanel;
var cobjPnl;

Ext.onReady(function(){
					 
	$('img[src$=.png]').ifixpng();
	
	try {
		cobjPnl = new CobjPanel();
		mainPanel = new MainPanel();
	
		cobjPnl.on('click', function(node, e){
			 e.stopEvent();
			 this.selectNode(node);
		});
		
		cobjPnl.getLoader().on("beforeload", function(treeLoader, node) {
			treeLoader.baseParams.isroot = node.attributes.isroot;
		}, this);
	
		mainPanel.on('tabchange', function(tp, tab){
			try {
				cobjPnl.selectPath(tab.path); 
			} catch (e) {}
		});
		
		var hd = new Ext.Panel({
			border: false,
			layout:'anchor',
			region:'north',
			cls: 'docs-header',
			height:35,
			items: [{
				xtype:'box',
				el:'header',
				border:false,
				anchor: 'none'
			}]
		})
	
		var viewport = new Ext.Viewport({
			id:'dashViewport',
			layout:'border',
			items:[ hd, cobjPnl, mainPanel ]
		});
		
		viewport.doLayout();
		
		var homePanel = new Ext.Panel({
									  id:'welcome-panel',
										title:'Home',
										autoLoad:{url:dashHomeURL, scripts: true},
										iconCls:'icon-home'
									  });
		mainPanel.add(homePanel);
		mainPanel.setActiveTab(homePanel);
	} catch(e) {}
	
	setTimeout(function(){
        Ext.get('loading').remove();
        Ext.get('loading-mask').fadeOut({remove:true});
    }, 250);
	
	Ext.QuickTips.init();
});
