/*
	Feel free to use your custom icons for the tree. Make sure they are all of the same size.
	If you don't use some keys you can just remove them from this config
*/
              
var TREE_TPL = {

	// general
    'keep_states':'true',	
    'target':'_self',	
                            
	// icons - root	
	'icon_48':'scripts/icons/emptysmall.gif',   // root icon normal
	'icon_52':'scripts/icons/emptysmall.gif',   // root icon selected
	'icon_56':'scripts/icons/emptysmall.gif',   // root icon opened
	'icon_60':'scripts/icons/emptysmall.gif',   // root icon selected opened
	// icons - node	
	'icon_16':'scripts/icons/empty.gif', // node icon normal
	'icon_20':'scripts/icons/empty.gif', // node icon selected
	'icon_24':'scripts/icons/empty.gif', // node icon opened
	'icon_28':'scripts/icons/empty.gif', // node icon selected opened
 	'icon_80':'scripts/icons/empty.gif', // mouseovered node icon normal
	// icons - leaf
	'icon_0':'scripts/icons/empty.gif', // leaf icon normal
	'icon_4':'scripts/icons/empty.gif', // leaf icon selected
	// icons - junctions	
	'icon_2':'scripts/icons/empty.gif', // junction for leaf
	'icon_3':'scripts/icons/empty.gif',       // junction for last leaf
	'icon_18':'scripts/icons/empty.gif', // junction for closed node
	'icon_19':'scripts/icons/empty.gif',       // junctioin for last closed node
	'icon_26':'scripts/icons/empty.gif',// junction for opened node
	'icon_27':'scripts/icons/empty.gif',      // junctioin for last opended node
 	// icons - misc
	'icon_e':'scripts/icons/empty.gif', // empty image
	'icon_l':'scripts/icons/empty.gif',  // vertical line
	
    
    
	// styles - root
	'style_48':'mout', // normal root caption style
	'style_52':'moutgroen', // selected root caption style
	'style_56':'moutwit', // opened root caption style
	'style_60':'moutgroen', // selected opened root caption style
	'style_112':'mover', // mouseovered normal root caption style
	'style_116':'mover', // mouseovered selected root caption style
	'style_120':'mover', // mouseovered opened root caption style
	'style_124':'mover', // mouseovered selected opened root caption style
	
	// styles - node
	'style_16':'mout', // normal node caption style
	'style_20':'moutgroen', // selected node caption style
	'style_24':'moutwit', // opened node caption style
	'style_28':'moutwit', // selected opened node caption style
	'style_80':'mover', // mouseovered normal node caption style
	'style_84':'mover', // mouseovered selected node caption style
	'style_88':'mover', // mouseovered opened node caption style
	'style_92':'mover', // mouseovered selected opened node caption style

	// styles - leaf
	'style_0':'mout', // normal leaf caption style
	'style_4':'moutgroen', // selected leaf caption style
	'style_64':'mover', // mouseovered normal leaf caption style
	'style_68':'mover', // mouseovered selected leaf caption style

	'onItemSelect':'onItemSelectHandler', // open met enkele klik 
	'onItemOpen':'openen' // maar 1 item open 

	// make sure there is no comma after the last key-value pair
};

         


/*
	This function modifies tree behavior so only one item
	in a level can be opened at a time.
	Function should be called AS onItemOpen handler of the tree
*/
function openen (o_item) {

	// get current block
	var a_curblock = o_item.o_parent.a_children;

	// close all nodes except current
	for (var i = 0; i < a_curblock.length; i++)
		if (a_curblock[i].n_state & 48 && a_curblock[i] != o_item)
			a_curblock[i].open(true);

	// proceed to default handler
	return true;
}

/* 
	This function modifies tree behavior so nodes without link
	assigned will open on single click
	Function should be called AS onItemSelect handler of the tree
*/

function onItemSelectHandler (o_item) {
    // if node with no link then toggle
    var o_state = o_item.state();
    if (o_state['node'] && !o_item.a_config[1]) {
        o_item.o_root.toggle(o_item.n_id);
        // cancel default action
        return false;
    }
    // proceed to default handler
    return true;
}




/*
	This function opens first tree item which has the caption specified.
	There are a lot of verifications that will help you to debug your
	code. You can disable or remove those checks when finished.
	
*/
function openItemByCaption (s_caption, o_tree) {
	// set to true when debugging the application
	var B_DEBUG = false;

	// exit if required parameter isn't specified
	if (!s_caption)
		return (B_DEBUG
			? alert("Required parameter to function openItemByCaption is missing")
			: false
		);

	// use first tree on the page if tree object isn't explicitly defined
	if (!o_tree)
		o_tree = (TREES[0]);
	if (!o_tree)
		return (B_DEBUG
			? alert("No Tigra Tree Menu PRO instances can be found on this page")
			: false
		);

    // find item with specified caption
    var a_item = o_tree.find_item(s_caption);
    for(var n=0; n < a_item.length; n++) {
	o_item=a_item[n];
	// collect info about all item's parents
	var n_id = o_item.n_id,
		n_depth = o_item.n_depth,
		a_index = o_item.o_root.a_index,
		a_parents = [o_item];

	while (n_depth) {
		if (a_index[n_id].n_depth < n_depth) {
			a_parents[a_parents.length] = a_index[n_id];
			n_depth--;
		}
		n_id--;
	}
		
	// open all parents starting from root
	for (var i = a_parents.length-1; i >= 0; i--)
	   // check if node or root
	   if (a_parents[i].n_state & 48)
	      a_parents[i].open();
	   else
	      if (B_DEBUG) 
	         alert("Item with caption '" + a_parents[i].a_config[0]+ 
	            "' is a leaf.\nHierarchy will be opened to its parent node only.")
    }
}


	 




/* collapses nodes of the tree
	n_index - zero based index of the tree on the pate
                  if omited then applied to first tree (n_index==0)
	n_depth - zero based level to which the tree with be collapsed
	          if omitted the tree will collapsed to second level (n_depth==1)
 */
function collapse_all (n_index, n_depth) {
	var o_tree = TREES[n_index ? n_index : 0];
	if (n_depth == null) n_depth = 1;
	if (!o_tree)
		alert("Tree is not initialized yet");
	var a_nodes = o_tree.a_nodes;
	for (var i = a_nodes.length - 1; i >= 0; i--)
		if (a_nodes[i].n_depth >= n_depth && a_nodes[i].open)
			a_nodes[i].open(1, 1);
	o_tree.ndom_refresh();
}

