// Title: Tigra Tree
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
// Version: 1.1
// Date: 11-12-2002 (mm-dd-yyyy)
// Notes: This script is free. Visit official site for further details.

function treeMenu (a_items, aHtml, aSelItem) {

	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'  : '/img/empty.gif', // empty image
	'icon_l'  : '/img/empty.gif',  // vertical line

    'icon_32' : '/img/folder.gif',   // root leaf icon normal
    'icon_36' : '/img/folder.gif',   // root leaf icon selected
	
	'icon_48' : '/img/folder.gif',   // root icon normal
	'icon_52' : '/img/folderopen.gif',   // root icon selected
	'icon_56' : '/img/folderopen.gif',   // root icon opened
	'icon_60' : '/img/folderopen.gif',   // root icon selected
	
	'icon_16' : '/img/folder.gif', // node icon normal
	'icon_20' : '/img/folderopen.gif', // node icon selected
	'icon_24' : '/img/folderopen.gif', // node icon opened
	'icon_28' : '/img/folderopen.gif', // node icon selected opened

	'icon_0'  : '/img/folder.gif', // leaf icon normal
	'icon_4'  : '/img/folderopen.gif', // leaf icon selected
	
	'icon_2'  : '/img/empty.gif', // junction for leaf
	'icon_3'  : '/img/empty.gif',       // junction for last leaf
	'icon_18' : '/img/plus.gif', // junction for closed node
	'icon_19' : '/img/plus.gif',       // junctioin for last closed node
	'icon_26' : '/img/minus.gif',// junction for opened node
	'icon_27' : '/img/minus.gif'       // junctioin for last opended node
};
	
	this.a_tpl      = TREE_TPL;
	this.a_config   = a_items;
	this.o_root     = this;
	this.a_index    = [];
	this.o_selected = null;
	this.n_depth    = -1;
	
	var o_icone = new Image(),
		o_iconl = new Image();
	o_icone.src = TREE_TPL['icon_e'];
	o_iconl.src = TREE_TPL['icon_l'];
	TREE_TPL['im_e'] = o_icone;
	TREE_TPL['im_l'] = o_iconl;
	for (var i = 0; i < 64; i++)
		if (TREE_TPL['icon_' + i]) {
			var o_icon = new Image();
			TREE_TPL['im_' + i] = o_icon;
			o_icon.src = TREE_TPL['icon_' + i];
		}
	
	this.toggle = function (n_id) {	var o_item = this.a_index[n_id]; o_item.open(o_item.b_opened) };
	this.select = function (n_id) { return this.a_index[n_id].select(); };
	this.mout   = function (n_id) {  };
	this.mover  = function (n_id) {  };
	this.getItemByLink = menuGetItemByLink;
	this.selectItem = menuSelectItem;

	this.a_children = [];
	for (var i = 0; i < a_items.length; i++)
		new menu_tree_item(this, i);

	this.n_id = trees.length;
	trees[this.n_id] = this;
	
	aHtml.innerHTML = '';
		
	for (var i = 0; i < this.a_children.length; i++) {
		aHtml.innerHTML += this.a_children[i].init();
		this.a_children[i].open();
	}
	
	if(arguments.length < 3) aSelItem = getCookie('md_menu');
	if(aSelItem) this.selectItem(aSelItem);
}

function menuGetItemByLink(n_link)
{
	for(var i = 0; i < this.a_index.length; i++)
	{
		if (this.a_index[i].a_config[1]==n_link)
		{
			return this.a_index[i];
		}
	}
	return null;
}

function menuSelectItem(n_link)
{
	var aItem = this.getItemByLink(n_link);
	if(!aItem) return;
			
	var aItems = Array();
	
	var parent = aItem.o_parent;
	while(parent && parent.n_depth >= 0)
	{
		aItems.push(parent);
		parent = parent.o_parent;
	}
	
	for(var i = aItems.length-1; i>=0; i--)
	{
		var aSubItem = aItems[i];
		
		if(!aSubItem.b_opened)
			aSubItem.open(false);
	}
	
	aItem.select();
	
	if(aItem.a_children.length > 0)
		aItem.open(false);
}

function menu_tree_item (o_parent, n_order) {

	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 2 : 0)];
	if (!this.a_config) return;

	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	this.b_opened  = !this.n_depth;

	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++)
		new menu_tree_item(this, i);

	this.get_icon = item_get_icon;
	this.open     = menu_item_open;
	this.select   = menu_item_select;
	this.init     = menu_item_init;
	this.is_last  = function () { return this.n_order == this.o_parent.a_children.length - 1 };
}

function menu_item_open (b_close) {
	var o_idiv = get_element('i_div' + this.o_root.n_id + '_' + this.n_id);
	if (!o_idiv) return;

	if (!o_idiv.innerHTML) {
		var a_children = [];
		for (var i = 0; i < this.a_children.length; i++)
			a_children[i]= this.a_children[i].init();
		o_idiv.innerHTML = a_children.join('');
	}
	o_idiv.style.display = (b_close ? 'none' : 'block');
	
	this.b_opened = !b_close;
	var o_jicon = document.images['j_img' + this.o_root.n_id + '_' + this.n_id],
		o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_jicon) o_jicon.src = this.get_icon(true);
	if (o_iicon) o_iicon.src = this.get_icon();
}

function menu_item_select (b_deselect) {
	if(!this.a_config[1]) return;
	
	if (!b_deselect) {
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
		
		setCookie('md_menu', this.a_config[1], null, '/');
	}
	var o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_iicon) o_iicon.src = this.get_icon();

	get_element('ia_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	
	return true;
}

function go(cat)
{
	setTimeout(function() {
		window.location = '/md/home/cat/'+cat+'.xhtml';
	}, 500);
}

function menu_item_init () {
	var a_offset = [],
		o_current_item = this.o_parent;
	for (var i = this.n_depth; i > 1; i--) {
		a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.is_last() ? 'icon_e' : 'icon_l'] + '" border="0">';
		o_current_item = o_current_item.o_parent;
	}

	return '<table cellpadding="0" cellspacing="2" border="0"><tr><td valign="top"><nobr>' + (this.n_depth ? a_offset.join('') + (this.a_children.length ? '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" onFocus="blur();"><img src="' + this.get_icon(true) + '" border="0" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>' : '<img src="' + this.get_icon(true) + '" border="0">') : '') + '<a href="JavaScript:void(null);" onFocus="blur();" onclick="return trees[' + this.o_root.n_id + '].select(' + this.n_id + '); go('+this.a_config[1]+');" ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')"><img src="' + this.get_icon() + '" border="0" name="i_img' + this.o_root.n_id + '_' + this.n_id + '"></a></nobr></td><td valign="top" class="'+ get_class_name(this) +'"'+(this.a_children.length != 0 ? ' onClick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + '); go('+this.a_config[1]+');"' : ' onClick="go('+this.a_config[1]+');"')+'><a href="JavaScript:void(null);" onFocus="blur();" '+(this.a_children.length == 0 ? 'onclick="return trees[' + this.o_root.n_id + '].select(' + this.n_id + ')" ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')"' : '' )+'" id="ia_txt' + this.o_root.n_id + '_' + this.n_id + '" class="Item">'+ this.a_config[0] +'</a></td></tr></table>' + (this.a_children.length ? '<span style="margin:0; padding: 0;" id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></span>' : '');
}

function get_class_name(ob) {
	if(ob.a_config[1] == '#search') {
		return 'Search'
	} else if(ob.a_children.length == 0) {
		return 'Item'
	} else {
		return 'Cat'
	}
}

function item_get_icon (b_junction) {
	if(!b_junction && this.a_children.length == 0) {
		return '/img/folder.gif';	
	} else {
		return this.o_root.a_tpl['icon_' + ((this.n_depth ? 0 : 32) + (this.a_children.length ? 16 : 0) + (this.a_children.length && this.b_opened ? 8 : 0) + (!b_junction && this.o_root.o_selected == this ? 4 : 0) + (b_junction ? 2 : 0) + (b_junction && this.is_last() ? 1 : 0))];
	}
}

var trees = [];
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

