// Title: Tigra Tree
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
//
// geändert:	 Thomas Kern, 2005 
// 						 http://www.kernidee.de

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


// Menüeinträge in einem extra Feld ablegen, um das automatische Aufklappen zu steuern
var maxentries = 150;				// mehr als 150 werden es schon nicht werden...
var files = new Array(maxentries);		// erster Schritt zum zweidimensionalen Array
for (var i=0;i<maxentries;i++) 		// eine Zeile für jeden Eintrag
{
	files[i] = new Array(3); 			// zwei Spalten
	files[i][0] = "0";					// hier wird der Link zum Menüeintrag abgelegt
	files[i][1] = "0"; 					// hier wird ein Verweis (die entsprechende ID des files-Arrays) auf den übergeordneten Menüeintrag abgelegt	
	files[i][2] = "0"; 					// auf Eins setzen, wenn es untergeordnete Menüeinträge gibt	
}


/* ========================================================================================================= 
/* ergänzende Hilfsfunktionen, nicht original im Tigra Tree enthalten
*/
// Dateinamen einschl. des letzten Unterverzeichnisses (1. Ebene) ermitteln
function ExtractFileName(myString)
{	
	var last, len, tmp, file, dir;
	
	// zuerst den Dateinamen als letzten Eintrag vor dem Slash bestimmen
	len = myString.length;
	last = myString.lastIndexOf("/");
	file = myString.slice(last+1, len);
	
	// dann aus dem restlichen String den letzten Verzeichnisnamen bestimmen
	tmp = myString.substring(0, last);	
	len = tmp.length;
	last = tmp.lastIndexOf("/");
	dir = tmp.slice(last+1, len);
	
	tmp = "../" + dir + "/" + file;
	
	return tmp;
}

/* ========================================================================================================= 
/* Hilfsfunktionen
*/
// einzelnen Menüeintrag anlegen
function tree_item (o_parent, n_order) {

	// Position ermitteln
	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;

	// Dateinamen speichern, um das Menü später zielgerichtet aufklappen zu können
	files[this.n_id][0] = this.a_config[1];
	if (this.n_depth > 0) { // übergeordneter Menüeintrag vorhanden
		files[this.n_id][1] = o_parent.n_id;	// damit kann der übergeordnete Eintrag aufgeklappt werden
		files[o_parent.n_id][2] = "1"; 		// damit wird der Eintrag aufgeklappt, wenn es untergeordnete Einträge gibt
	}

	// ggf. weitere Einträge unterhalb anlegen
	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++)
		new tree_item(this, i);

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

// 
function 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();
	this.upstatus();
}

// 
function item_select (b_deselect) {
	
	
	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('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';

	this.upstatus();
		
	return Boolean(this.a_config[1]);	
}

//
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 () {

	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" align="absbottom">';
		o_current_item = o_current_item.o_parent;
	}
	return '<table cellpadding="0" cellspacing="0" border="0"><tr><td class=mtree nowrap>' + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? '<a href="javascript: 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 + ')"><img src="' + this.get_icon(true) + '" border="0" align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		: '<img src="' + this.get_icon(true) + '" border="0" align="absbottom">') : '') 
		+ '<a href="' + this.a_config[1] + ' " class=mtree " ' +  /* ' " target=" ' + this.o_root.a_tpl['target'] + */  ' " 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="t' + this.o_root.n_id + 'i" id="i_txt' + this.o_root.n_id + '_' + this.n_id + '"><img src="' + this.get_icon() + '" border="0" align="absbottom" name="i_img' + this.o_root.n_id + '_' + this.n_id + '" class="t' + this.o_root.n_id + 'im">' + this.a_config[0] + '</a></td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
}

//
function item_get_icon (b_junction) {
	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))];
}


/* ========================================================================================================= 
/* Eintrittsfunktion, Menüeinträge, Icons und Voreinstellungen werden übergeben (stehen im Regelfall in anderen Dateien)
*/
function tree (a_items /*var TREE_ITEMS*/, a_template /*var TREE_TPL*/, docsrc /*document location*/) {

	var i, j;

	// Werte initialisieren
	this.a_tpl      = a_template;
	this.a_config   = a_items;
	this.o_root     = this;
	this.a_index    = [];
	this.o_selected = null;
	this.n_depth    = -1;

	
	// Icons übernehmen und Images anlegen
	var o_icone = new Image(),
		o_iconl = new Image();
	o_icone.src = a_template['icon_e'];
	o_iconl.src = a_template['icon_l'];
	a_template['im_e'] = o_icone;
	a_template['im_l'] = o_iconl;
	for (i = 0; i < 64; i++)
		if (a_template['icon_' + i]) {
			var o_icon = new Image();
			a_template['im_' + i] = o_icon;
			o_icon.src = a_template['icon_' + i];
		}
	
	// Funktionen zum Umschalten des Aussehens und der Icons werden hier direkt definiert
	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.a_index[n_id].upstatus(true) };
	this.mover  = function (n_id) { this.a_index[n_id].upstatus() };
	this.lastClick = function (n_id) { this.lastselect=n_id };

	// für jeden Menüeintrag wird ein Item angelegt
	this.a_children = [];
	for (i = 0; i < a_items.length; i++)
		new tree_item(this, i);

	//
	this.n_id = trees.length;
	trees[this.n_id] = this;
	
	// Menüeinträge anzeigen
	for (i = 0; i < this.a_children.length; i++) {
		document.write(this.a_children[i].init());
		this.a_children[i].open();
	}

	// aktuelle Datei bestimmen und das Menü entsprechend öffnen
	var docsrc = ExtractFileName(docsrc);
	var found = 0;
	
	for(i = 0; i < this.a_index.length; i++) {
		if( docsrc.search(files[i][0]) > -1 ) { 	// Eintrag der aktuellen Datei lokalisieren
			if (files[i][1] > 0) {				// Menüeintrag ist untergeordnet
				//alert (files[i][0] + " liegt unterhalb von " + files[ files[i][1] ][0]);
				this.toggle( files[i][1] );
			}
			else if ( i>0 && files[i][2] > 0 ) {	// Hauptmenü nicht zuklappen !
				this.toggle(i);
			}
			this.select(i);
			found = 1;
		}		
	}

	// kein passender Menüeintrag, z.B. wegen dynamisch generierter Seite (Suchformular)
	if (found < 1) {
		this.select(0);
	}
}

