
Ext.namespace('Ext.ux');

Ext.ux.CustomXmlTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
	processResponse : function(response, node, callback){
		var doc = response.responseXML.documentElement;
		var nodes = this.parseXml(doc); 
		node.beginUpdate();
		node.appendChild(nodes);
		node.endUpdate();
        if(typeof callback == "function"){
            callback(this, node);
        }		
	},
	parseXml : function(XmlEl)      {
	    // Node Text is nodeValue to text node, otherwise it's the tag name
	    // 3 is Text_Node
	    var t = ( ( XmlEl.nodeType == 3 ) ? XmlEl.nodeValue : XmlEl.tagName );
	
	    // If no text, no node.
	    if ( t.replace( /\s/g,'' ).length == 0 )    // Empty Text_Node
	    {
	        //output_log( "Return null: XmlEl: " + XmlEl + " t: " + t );
	        return null;
	    }
	
	    // Create a new empty object which gets replaced by node with text below.
	    var result = new Object();
	
	    // For Elements, process attributes and children
	    if ( XmlEl.nodeType == 1 )  // element
	    {
	        if ( !( XmlEl.tagName == "Folder" || XmlEl.tagName == "Article" ) )
	            alert( "1) Found invalid node: " + XmlEl.tagName );
	
	        // Find the <Title> node and use its text for the new child node.
	        Ext.each( XmlEl.childNodes, function(elt) {
	            if ( (elt.nodeType == 1) && (elt.tagName == "Title") )
	            {
	                var vurl;
	                if ( XmlEl.getAttribute( "url" ) )
	                {
	                    vurl = XmlEl.getAttribute( "url" );
	                    //output_log( "url: " + XmlEl.getAttribute( "url" ) );
	                }
	
	                // output_log( elt.firstChild.nodeValue );
	                var a = { text : elt.firstChild.nodeValue, hrefsrc : vurl, hrefTarget : "myiframe"};

	                if ( XmlEl.getAttribute( "iconCls" ) )
	                {
	                    a.iconCls = XmlEl.getAttribute( "iconCls" );
	                    //output_log( "viconCls: " + XmlEl.getAttribute( "iconCls" ) );
	                }

	                if ( XmlEl.getAttribute( "iconClsExp" ) )
	                    a.iconClsExp = XmlEl.getAttribute( "iconClsExp" );

                        // Can't use this as we don't get the right image for non-default expanded images because updateExpandIcon() isn't called during load.
	                if ( XmlEl.getAttribute( "expanded" ) )
	                    a.expanded = true;
                            
	                result = new Ext.tree.TreeNode(a);
	                return false;   // exit each()
	            }
	        }, this);
	
	
	        // Iterate child nodes
	        Ext.each( XmlEl.childNodes, function( el )
	        {
	            // Only process Elements
	            if ( el.nodeType == 1 )  // element
	            {   // only descend for nodes of interest.
	                if ( el.tagName == "Folder" || el.tagName == "Article" )
	                {
	                    var c = this.parseXml(el);  // recurse
	                    if (c)
	                        result.appendChild(c);
	                }
	                else
	                if ( el.tagName != "Title" )
	                {
	                    //output_log( "2) Found invalid node: " + el.tagName );
	                }
	            }
	        }, this);
	    }
	    return result;				
	}
});


// See: http://extjs.com/forum/showthread.php?t=17679
Ext.tree.TreeNodeUI.prototype.updateExpandIcon =
Ext.tree.TreeNodeUI.prototype.updateExpandIcon.createSequence(function()
{
    if ( this.iconNode )  // undefined when tree is populated
    {
        // output_log( "1) updateExpandIcon - expanded: " + this.node.expanded + ", iconNode: " + this.iconNode + ", iconClsExp: " + this.node.attributes.iconClsExp + ", hrefsrc: " + this.node.attributes.hrefsrc );
        // output_log( "2) updateExpandIcon - expanded: " + this.node.expanded + ", node: " + this.node + ", iconCls: " + this.node.attributes.iconCls + ", getIconEl: " + this.getIconEl() + ", hrefsrc: " + this.node.attributes.hrefsrc );

        if( this.node.expanded )
        {   // Change expanded image as spec'd. Called when node expanded.
            if ( this.node.attributes.iconClsExp )
            {
             // Ext.fly( this.iconNode ).replaceClass( this.collapsedCls, this.expandedCls );  //  param1 does zip
                Ext.fly( this.iconNode ).replaceClass( this.collapsedCls, this.node.attributes.iconClsExp );
                // output_log( "5) updateExpandIcon - expanded: " + this.node.expanded + ", node: " + this.node + ", iconCls: " + this.node.attributes.iconCls + ", iconClsExp: " + this.node.attributes.iconClsExp );
            }    
        }
        // else
        //    Ext.fly( this.iconNode ).replaceClass( this.expandedCls, this.collapsedCls );
    }
    else
    {
        // output_log( "3) updateExpandIcon - expanded: " + this.node.expanded + ", node: " + this.node + ", iconCls: " + this.node.iconCls + ", getIconEl: " + this.getIconEl() + ", hrefsrc: " + this.node.attributes.hrefsrc );
        // console.info( "here..." );
    }
});


// Display msg in 'south' panel.
function output_log( msg )
{
    Ext.get( 'status' ).insertHtml( 'beforeEnd', msg + "<br>" );
}



