// 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.

var last_page = 0;

function MenuLeftClick(ADiv, APageNum, APartNum)
{
	var Obj = $(ADiv);
	if(!Obj) return false;
	
	Obj.obj.showPageNum(APageNum, APartNum);
}

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

function setStats(book_id, page_id, part_id, first_page, page_width, page_height)
{
	if(last_page == page_id) var first_open = 'no'; else var first_open = 'yes'; 
	
	if(first_page) first_page = 'yes'; else first_page = 'no';	
		
	advAJAX.get({
		url: '/xml/setStat.php',
		book_id: book_id,
		last_id: last_page,
		page_id: page_id,
		part_id: part_id,
		first_page: first_page,
		first_open: first_open,
		page_width: page_width*1,
		page_height: page_height*1
	});	
	
	last_page = page_id;
}

function selectItem(book_id, page_id, first_page, page_width, page_height, n_link)
{
	if(n_link) var aItem = this.getItemByLink(n_link);
	else var aItem = null;
	
	if(!aItem)
	{
		if(this.o_root.o_selected)
			this.o_root.o_selected.select(true);
			
		setStats(book_id, page_id, 0, first_page, page_width, page_height);	
			
		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();
	setStats(book_id, page_id, aItem.a_config[2], first_page, page_width, page_height);
	window.location = '#'+aItem.n_id;
}

function tree (a_items, aHtml, aClickFunc) {

	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/folder.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;
	this.click_func = aClickFunc;
	
	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.click_func) };
	this.select = function (n_id) { return this.a_index[n_id].select(); };
	this.mout   = function (n_id) { this.a_index[n_id].upstatus(true) };
	this.mover  = function (n_id) { this.a_index[n_id].upstatus() };
	this.getItemByLink = getItemByLink;
	this.selectItem = selectItem;
	
	this.a_children = [];
	if(a_items) for (var i = 0; i < a_items.length; i++)
		new tree_item(this, i);

	this.n_id = trees.length;
	trees[this.n_id] = this;
		
	for (var i = 0; i < this.a_children.length; i++) {
		aHtml.innerHTML += this.a_children[i].init(this.click_func);
		this.a_children[i].open(true, this.click_func);
	}
}
function 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 ? 3 : 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 - 3; i++)
		new tree_item(this, i);

	this.get_icon = item_get_icon;
	this.open     = item_open;
	this.select   = item_select;
	this.init     = item_init;
	this.upstatus = item_upstatus;
	this.is_last  = function () { return this.n_order == this.o_parent.a_children.length - 1 };
}

function item_open (b_close, click_func) {
	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(click_func);
		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();
	this.upstatus();
}

function item_select (b_deselect) {
	if(!this.a_config[1]) return;

	if(this.a_children.length != 0 && b_deselect == null && this.a_config[1])
		this.open(false);

	if (!b_deselect) {
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
	}
	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';
	
	this.upstatus();
	return true;
}

function item_upstatus (b_clear) {
	//window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);
}

function item_init (click_func) {
	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;
	}
	click_func = 'BookPanel';
	return '<a name="' + this.n_id + '"></a><div style="line-height: 15px;" id="tree' + this.n_depth+'_'+this.n_order + '"><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();" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><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="' + (this.a_children.length == 0 ? 'JavaScript:MenuLeftClick(\''+click_func+'\', \''+this.a_config[1]+'\', \''+this.a_config[2]+'\');' : 'JavaScript:void(null)') + '" onFocus="blur();" onclick="return trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" id="i_txt' + this.o_root.n_id + '_' + 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 + ')"' : '')+'><a id="ia_txt' + this.o_root.n_id + '_' + this.n_id + '" href="' + (this.a_config[1] ? 'JavaScript:MenuLeftClick(\''+click_func+'\', \''+this.a_config[1]+'\', \''+this.a_config[2]+'\');' : '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 + ')"' : '' )+' onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" class="Item">'+ this.a_config[0] +'</a></td></tr></table></div>\n\n' + (this.a_children.length ? '<div style="margin:0; padding: 0;" id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
}

function get_class_name(ob) {
	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/arrow.gif';	
	} else {
		return this.o_root.file_path + 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 && !this.a_children.length ? 4 : 0) + (b_junction ? 3 : 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) };

