// 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)
// Contact: feedback@softcomplex.com (specify product title in the subject)
// Notes: This script is free. Visit official site for further details.


/* Note: This script has been changed by Thomas Praxl to be able to
		- specify an icon for each entry
		- add as much icons with links as you want (they´ll appear to the right of the entry)
*/
/****
		fasttree.js

		How to use: simply write
				new tree(myItems, mySkin, myClasses)

			    where myClasses may be null (if you do not wish to specify special Icons for the entries)

		For a description of mySkin and myClasses see treeSkins.js

		parameter:
			myItems
				This is an array with the following entries:

				string displayedName, string href, string specialClass,
					[[string class, string href, string target],...],
					[ child (the same entries) ]

				an Example:

				[['root', null, null, null,
					['childLevel1', 'about:blank', null,null,
						['childLevel2', 'about:blank', 'manager', [['editable', 'editMe.html', '_blank']]]
						['anotherChildLevel2', .........]
					]
				]]

                 you sure will want to change the style of the tree.
                 This is done by adding two styles to the document:
                 	t0i (item Text) and t0im (item Image)

                 have fun:)
                 Thomas Praxl

***/
var baseImagePath = "";
function tree (a_items, a_template, a_entryclasses, _baseImagePath, isOpen) {

  baseImagePath = _baseImagePath?_baseImagePath:"";
  this.a_entclass = a_entryclasses;
  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;
  var o_icone = new Image(),
    o_iconl = new Image();
  o_icone.src = baseImagePath+a_template['icon_e'];
  o_iconl.src = baseImagePath+a_template['icon_l'];
  a_template['im_e'] = o_icone;
  a_template['im_l'] = o_iconl;
  for (var i = 0; i < 64; i++)
    if (a_template['icon_' + i]) {
      var o_icon = new Image();
      a_template['im_' + i] = o_icon;
      o_icon.src = baseImagePath+a_template['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.a_index[n_id].upstatus(true) };
  this.mover  = function (n_id) { this.a_index[n_id].upstatus() };

  this.a_children = [];
  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++) {
      document.write(this.a_children[i].init());
      this.a_children[i].open();
      if(isOpen) open_child(this.a_children[i]);
  }
}
function open_child (c_of_this) { 
for (var x = 0; x < c_of_this.a_children.length; x++) { 
c_of_this.a_children[x].open(); 
open_child (c_of_this.a_children[x]); 
} 
} 




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 ? 4 : 0)];
  if (!this.a_config) return;
  this.o_root    = o_parent.o_root;
  if(this.a_config[2]){
    this.b_specialEntry = true;
	if(this.o_root.a_entclass['im_'+this.a_config[2]]==null){
		var o_obj = new Image();
		o_obj.src = baseImagePath + this.o_root.a_entclass[this.a_config[2]];
		this.o_root.a_entclass['im_'+this.a_config[2]] = o_obj;
	}
 }
 if(this.a_config[3]){
		for(var i = 0; i < this.a_config[3].length;++i){
			if( this.o_root.a_entclass['im_'+this.a_config[3][i]] == null){
				var o_obj = new Image();
				o_obj.src = baseImagePath+this.o_root.a_entclass[this.a_config[3][i][0]];
				this.o_root.a_entclass['im_'+this.a_config[3][i][0]] = o_obj;
			}
		}
  }
  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 - 4; i++)
    new tree_item(this, i);

  this.get_icon = item_get_icon;
  this.render_extra_icons = tree_item_render_extra_icons;
  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) {
  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');
  for(var i = 0; i < this.a_children.length; i++)
	{
		var o_iicon = document.images['i_img'+ this.o_root.n_id+'_'+this.a_children[i].n_id];
		o_iicon.src = this.a_children[i].get_icon();
	}
  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="Welcome to the message forum"', 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="'+ baseImagePath +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;
  }
  var retStr = '<table cellpadding="0" cellspacing="0" border="0" width=745><tr><td 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] + '" 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>'+this.render_extra_icons()+'</td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
	return retStr;
}

function item_get_icon (b_junction) {
	return  (this.b_specialEntry&&(b_junction==null))?baseImagePath+this.o_root.a_entclass[this.a_config[2]]:baseImagePath+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))];
}
function tree_item_render_extra_icons(){
  var extraIcons = this.a_config[3];
  var returnValue = '';
  if(extraIcons){
   for(var i = 0; i < extraIcons.length;++i){
      var eIcon = extraIcons[i];
       if(eIcon[1]){
        returnValue+='<a href="'+eIcon[1]+'" target="';
        if(eIcon[2]){
          returnValue+=eIcon[2];
        }
        else{
          returnValue+=this.o_root.a_tpl['target'];
        }
        returnValue+='">';
       }
       if(eIcon[0]){
        returnValue+='<img src="'+baseImagePath+this.o_root.a_entclass[eIcon[0]]+'" border="0" align="absbottom" name="extraicon" class="t'+this.o_root.n_id+'eIcon" >';
       }
       if(eIcon[1]){
        returnValue+='</a>';
       }
    }
  }
  return returnValue;
}

var trees = [];
get_element = document.all ?
  function (s_id) { return document.all[s_id] } :
  function (s_id) { return document.getElementById(s_id) };

