/*
 * Custom ext-js treepanel used to 
 * create seperate columns.
 */

Ext.tree.ColumnTree = Ext.extend(Ext.tree.TreePanel, {
    lines:false,
    borderWidth: Ext.isBorderBox ? 0 : 2, // the combined left/right border for each cell
    cls:'x-column-tree',
    
    onRender : function(){
		//debugger;
        Ext.tree.ColumnTree.superclass.onRender.apply(this, arguments);
        this.headers = this.body.createChild(
            {cls:'x-tree-headers'},this.innerCt.dom);

        var cols = this.columns, c;
        var totalWidth = 0;

        for(var i = 0, len = cols.length; i < len; i++){
             c = cols[i];
             totalWidth += c.width;
			 var header_style = 'width:' + (c.dataIndex == 'main_text' ? '95%' : c.width-this.borderWidth + 'px') + ';height:1px';
             this.headers.createChild({
                 cls:'x-tree-hd ' + (c.cls?c.cls+'-hd':''),
                 cn: {
                     cls:'x-tree-hd-text',
                     html: c.header
                 },
//                 style:'width:'+(c.width-this.borderWidth)+'px;height:1px'
				 style:header_style
             });
        }
		
//		totalWidth = my_bookmarks.getInnerWidth()-5;
        this.headers.createChild({cls:'x-clear'});
        // prevent floats from wrapping when clipped
//        this.headers.setWidth(totalWidth);
//        this.innerCt.setWidth(totalWidth);
    }
});

Ext.tree.ColumnNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
    focus: Ext.emptyFn, // prevent odd scrolling behavior
	
    renderElements : function(n, a, targetNode, bulkRender){
		this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';

        var t = n.getOwnerTree();
        var cols = t.columns;
        var bw = t.borderWidth;
        var c = cols[0];
//		this.emptyIcon = '/ext2.0/' + this.emptyIcon.substring(6);
		this.emptyIcon = Ext.BLANK_IMAGE_URL;

		if(a.iconCls.substring(0,6)=='FOLDER'){
			var plusminus = '';
		}else{
			var plusminus = ' style="width:0px; height=0px;"';	
		}

        var buf = [
             '<li class="x-tree-node">'
			 ,'<div id="bm_div_'+ a.id +'" ext:tree-node-id="' + n.id + '" class="x-tree-node-el x-tree-node-leaf ' + a.cls + '" style="height:22px" onmouseover="Javascript:handleMouseOver(' + a.id + ');" onmouseout="Javascript:handleMouseOut(' + a.id + ');" onClick="Javascript:handleClick(' + a.id + ');">'
			 ,'<table width="99%" align="center"><tr><td width="100%" style="overflow:hidden;" valign="top">'
			 	,'<div class="x-tree-col" style="width:97%;height:18px;overflow:hidden;" id="node_' + a.id + '">'					
			 		,'<span class="x-tree-node-indent">' + this.indentMarkup + '</span>'
			 		,'<img src="' + this.emptyIcon + '" class="x-tree-ec-icon x-tree-elbow"' +plusminus+ '>'
			 		,'<img src="'
			 			, a.icon || this.emptyIcon									
			 			, '" class="x-tree-node-icon' + (a.icon ? " x-tree-node-inline-icon" : "") + (a.iconCls ? " "+a.iconCls : "") + '" unselectable="on">'
			 		,'<a hidefocus="on" class="x-tree-node-anchor" href="'	+ (a.href ? a.href : "#") + '" tabIndex="1" '
			 			,(a.hrefTarget ? ' target="'+a.hrefTarget+'"' : "") + (a.onclick ? ' onClick=\''+a.onclick+'\'' : "") + ' style="overflow:hidden;">'
			 			,'<span unselectable="on" style="overflow:hidden;width:100%;overflow-x:hidden;overflow-y:hidden;">'
			 				, n.text || (c.renderer ? c.renderer(a[c.dataIndex], n, a) : a[c.dataIndex])
			 		,"</span></a>"
			 	,"</div></td>"];

		 if(n.attributes.node_type == 'FOLDER'){
			 var isFolder = true;
		 }else{
			 var isFolder = false;
		 }

         for(var i = 1, len = cols.length; i < len; i++){
             c = cols[i];

			if(c.dataIndex == 'canEdit' && a[c.dataIndex] == 'Y'){
				 buf.push('<td><div class="x-tree-col '
						  	,(c.cls?c.cls:'')
							,'" style="width:'
							,c.width-bw
							,'px;">'
							,'<div class="x-tree-col-text" align="left"><img src="/images/book_edit.png" '
							,'onclick="Javascript:handleEdit('+n.attributes.id+',\''+n.attributes.node_type+'\');" id="edit_bm_'+n.attributes.id+'"></div>'
						  	,"</div></td>");				
			}else if(c.dataIndex == 'canDelete' && a[c.dataIndex] == 'Y'){
				 buf.push('<div class="x-tree-col ',(c.cls?c.cls:''),'" style="width:',c.width-bw,'px;">',
							'<div class="x-tree-col-text" align="left"><img src="/images/book_delete.png" onclick="Javascript:handleDelete('+n.attributes.id+','+isFolder+');"></div>',
						  "</div>");								
			}
         }
         buf.push(
            '<div class="x-clear"></div></tr></table></div>',
            '<ul class="x-tree-node-ct" style="display:none;"></ul>',
            "</li>");

        if(bulkRender !== true && n.nextSibling && n.nextSibling.ui.getEl()){
            this.wrap = Ext.DomHelper.insertHtml("beforeBegin",
                                n.nextSibling.ui.getEl(), buf.join(""));
        }else{
            this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf.join(""));
        }

        this.elNode = this.wrap.childNodes[0];
        this.ctNode = this.wrap.childNodes[1];
		var cs = document.getElementById('node_' + a.id).childNodes;
//        var cs = this.elNode.firstChild.childNodes;
        this.indentNode = cs[0];
        this.ecNode = cs[1];
        this.iconNode = cs[2];
        this.anchor = cs[3];
        this.textNode = cs[3].firstChild;
    }
});