﻿
function SoTree(treediv, treeData, options) 
{
	var nodes = [];
	
	//instantiate the tree:
    this.tree = new YAHOO.widget.TreeView(treediv);
    
    // Add some nodes...
    // In treedata, n = name, i = id for brevity on the wire
    var sectionNode, categoryNode, section, category;
    
    for (var key in treeData)
    {
       section = treeData[key];
       var o = new Object();
       o.label = section.n;
       o.style= section.sn;
       sectionNode = new YAHOO.widget.TaskNode(o, this.tree.getRoot(), false /* expanded */ ); 
       
       //GLog.write(sectionNode.labelElId);
       
       sectionNode.id = section.i;
       //sectionNode.check();
       
       for (var k=0; k<section.cats.length; k++)
       {
            category = section.cats[k];
            categoryNode = new YAHOO.widget.TaskNode(category.n, sectionNode, false);
            categoryNode.id = category.i;
            
       }
    }

    // Trees with TaskNodes will fire an event for when a check box is clicked
    this.tree.subscribe("checkClick", options.checkClick);
    this.tree.subscribe("labelClick", function(node) { node.checkClick(); });
	
	//The tree is not created in the DOM until this method is called:
    this.tree.draw();
    
    // Now check top level all nodes (and therby all their children - see TaskNode.js.
    //var topNodes = this.tree.getRoot().children;
    //for(var i = 0; i < topNodes.length; ++i) {topNodes[i].check();}
    
    // this is the jquery to use to set the right classes on the treeview
    for (var key in treeData)
    {
        section = treeData[key];;
        $("div.ygtvchildren div.ygtvitem:has(a." + section.sn + ")").addClass(section.sn);
    }
    
};


// Render the selcted notes in the folllowing format
// parent:child,child|parent|parent:child, child child etc
// when no children are writton out this means *all* children are checked
// Note this assumes we are only ever 2 children deep
SoTree.prototype.getSelections = function()
{
    var nodes = this.tree.getRoot().children;
    var output = "";

    for(var i=0, l=nodes.length; i<l; i=i+1) {
    
        var n = nodes[i];
   
        if (n.checkState == 1) 
        {
        
            // Write section id to string
            output += n.id;
            
            // Node has some but not all children checked so recurse children
            if (n.hasChildren()) 
            {
                var childNodes = [];
                
                output += ":";
                for(var j=0, k=n.children.length; j<k; j=j+1) 
                {
                    var childNode = n.children[j];
                    if (childNode.checkState == 2) 
                    {
                        childNodes.push (childNode.id);
                    }
                }
                // Write children to the string
                output += childNodes.join(',');
                
            }
            output += "|";
        }
        if (n.checkState === 2) {
            
            // All node selected - so no need to write out the children
            output += n.id + "|";
        }
    }
    
    // trim off the trailing |
    if (output.length > 1) output = output.substring(0,output.length-1);

    return output;
};