/*
	Feel free to use your custom images for the tree. Make sure they are all of the same size.
	User images collections are welcome, we'll publish them giving all regards.
*/

var item_scope1 = {'st' : 0, 's0':'t0i', 's4':'t0i', 's8':'t0i', 's12':'t0i', 's64':'t0i', 's68':'t0i', 's72':'t0i', 's76':'t0i'};
var item_scope2 = {'s0':'t0i', 's4':'t0i', 's8':'t0i', 's12':'t0i', 's64':'t0i', 's68':'t0i', 's72':'t0i', 's76':'t0i'};
var item_scope3 = {'st' : 0, 's0':'t1i', 's4':'t1i', 's8':'t1i', 's12':'t1i', 's64':'t1i', 's68':'t1i', 's72':'t1i', 's76':'t1i'};
var item_scope4 = {'s0':'t2i', 's4':'t2i', 's8':'t2i', 's12':'t2i', 's64':'t2i', 's68':'t2i', 's72':'t2i', 's76':'t2i'};

var TREE_TPL = {
	'target'  : '_self',	// name of the frame links will be opened in
							// other possible values are: _blank, _parent, _search, _self and _top

	'icon_e'  : 'templates/default/images/empty.gif', // empty image
	'icon_l'  : 'templates/default/images/empty.gif',  // vertical line

        'icon_32' : 'templates/default/images/arrow.jpg',   // root leaf icon normal
        'icon_36' : 'templates/default/images/arrow.jpg',   // root leaf icon selected
	
	'icon_48' : 'templates/default/images/arrow.jpg',   // root icon normal
	'icon_52' : 'templates/default/images/arrow.jpg',   // root icon selected
	'icon_56' : 'templates/default/images/arrow_d.jpg',   // root icon opened
	'icon_60' : 'templates/default/images/arrow_d.jpg',   // root icon selected
	
//	'icon_16' : 'templates/default/images/folder.gif', // node icon normal
//	'icon_20' : 'templates/default/images/folderopen.gif', // node icon selected
//	'icon_24' : 'templates/default/images/folderopen.gif', // node icon opened
//	'icon_28' : 'templates/default/images/folderopen.gif', // node icon selected opened

//	'icon_0'  : 'templates/default/images/page.gif', // leaf icon normal
//	'icon_4'  : 'templates/default/images/page.gif', // leaf icon selected
	
	'icon_2'  : 'templates/default/images/empty.gif', // junction for leaf
	'icon_3'  : 'templates/default/images/empty.gif',       // junction for last leaf
	'icon_18' : 'templates/default/images/empty.gif', // junction for closed node
	'icon_19' : 'templates/default/images/empty.gif',       // junctioin for last closed node
	'icon_26' : 'templates/default/images/empty.gif',// junction for opened node
	'icon_27' : 'templates/default/images/empty.gif',      // junctioin for last opended node
	'onItemSelect':'onItemSelectHandler' // on item open event handler

};

/*
	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 onItemOpenHandler (o_item, o_tree) {
	// 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
		);
	// get current block
	var a_curblock = o_tree.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
    if (o_item.a_config[3] && !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.")
    }
}

/* expands 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 expanded
	          if omitted the tree will be fully expanded
 */
function expand_all (n_index, n_depth) {
	var o_tree = TREES[n_index ? n_index : 0];
	if (!o_tree)
		alert("Tree is not initialized yet");
	var a_nodes = o_tree.a_nodes;
	for (var i = 0; i< a_nodes.length; i++)
		if (n_depth == null || a_nodes[i].n_depth <= n_depth)
			a_nodes[i].open(0, 1);
	o_tree.ndom_refresh();
}
