// 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.
// add function COOKIES by Massimo d. in 11.2006


// uso ---------------------------------------------------------------
// <p>
// 	<a href="#" onclick="Wppespandi('wppOpenClose');">Espandi</a> | 
//	<a href="#" onclick="Wppespandi('wppOpenClose', true);">Contrai</a>
//	</p>

//wppOpenClose = new tree (TREE_ITEMS, TREE_TPL);
// -------------------------------------------------------------------
// // ############################################################################################################ 
// aggiunta della func cookies
// ############################################################################################################ 
var o_root = null;
var o_selected = null;
// ############################################################################################################ 
var NumSpaces
var UseCludge
var CludgeCount = 0;
var ImgPath = 'icons/'

function tree (a_items, a_template, CollapseBase, addspaces, UCludge) {

        if(!CollapseBase) { CollapseBase = false; }


        if(!addspaces) {NumSpaces = 0;}
        else
        {NumSpaces = addspaces;}

        if(!UCludge)
        {
          UseCludge = false;
        }
        else
        {
          UseCludge = UCludge;
        }


	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;
	// aggiunta da fun cookie
	this.open_status = true;
	//
	
	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 (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 = 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(true) };
     

	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(CollapseBase){this.a_children[i].open(true);}
	}
	 
	
	// modify by icemax for danix
	  //for (var i = 1; i < this.a_index.length; i++)
	 
// ----------------- inizio modifica cookie
		var current = GetCookie("currentNode");
		var to_open = [];
		var i =0;
		
																		//alert('nodo corrente ' +current);
		while(current>0) {
			to_open[i] = current;
			i = i+1;
			current = this.a_index[current].o_parent.n_id;
																		//alert(current);
		}
		for(i=to_open.length-1;i>=0;i--) {
			current = to_open[i];
			 trees[this.n_id].toggle(current); 
			 
																//alert('apertura tree ' +current);
 	    }
      
		 
		   /// this.a_children.open(); //tips
		   /// new tree (TREE_EXT2, TREE_STD, false,1,false);

		  
		
/*	// appartenente alla function ma disabilitata per test
    //
		
		var jscriptCmd = this.a_index[current].a_config[1];
		var loc1 = jscriptCmd.indexOf("javascript:setText(");
		var loc2 = jscriptCmd.indexOf(")");
		jscriptCmd = jscriptCmd.substring(loc1+19,loc2);
		var comma = jscriptCmd.indexOf(",");
		var name = jscriptCmd.substring(1,comma-1);
		jscriptCmd = jscriptCmd.substring(comma+1);
		comma = jscriptCmd.indexOf(",");
		var branch = jscriptCmd.substring(1,comma-1);
		jscriptCmd = jscriptCmd.substring(comma+1);
		var length = jscriptCmd.length;
		var level = jscriptCmd.substring(1,length-1);
		parent.setText(name, branch, level);
*/
// ------------- fine modifica cookie


}

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 ? 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 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) {
	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';
	
	// modifica per cookie
	//get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	//get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.background = b_deselect ? 'red' : 'blue';
	//get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.color = b_deselect ? 'black' : 'white';
	SetCookie("currentNode",this.n_id);
	// fine aggiunta per cookie
	
	this.upstatus();
	// cook
	o_selected = this.o_root.o_selected;
	// -- end cook
	return Boolean(this.a_config[1]);
	
	
	 
	
}

// ---------- altra func aggiunta per cookie ------------
function item_toggle(b_open)
{
	o_selected.open(o_selected.open_status);
	o_selected.open_status = !o_selected.open_status
}
//--------------------------------------------------------
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 TxtSpaces = ''
        if(NumSpaces!=0)
        {
          for(i = 0; i <= NumSpaces; i++)
          {
            TxtSpaces = TxtSpaces + '&nbsp;'
          }
        }

        var Extras = ''
        var Extras2 = ''
        var Extras3 = ''

        if(UseCludge)
        {
          if(!this.n_depth)
          {
            Extras = '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ');Flickit(' + CludgeCount + ')"><img ID=CLUDGEBUTTON'+ CludgeCount +' src=' + ImgPath + 'plus.gif ALT=plus border=0></A>';
            Extras2 = ';Flickit(' + CludgeCount + ')';
            Extras3 = '<IMG align=absbottom SRC=' + ImgPath + 'empty.gif>'
            CludgeCount = CludgeCount + 1
          }
        }
	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" WIDTH=100%><tr><td nowrap><FONT SIZE=2>' + TxtSpaces  + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? Extras3 + '<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">') : '') 
		+ Extras + '<a href=' + this.a_config[1] + ' target="' + this.o_root.a_tpl['target'] + '" ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')' + Extras2 +'"onclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + '); return trees[' + this.o_root.n_id + '].select(' + 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))];
}

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))];
}

// ############################################################################################################ 
function SetCookie(name, value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	//var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	var path = "/"; //allows the tree to remain open across pages with diff names & paths

       // name = "test"

	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
	                          // alert('registrato con successo '+name+' in valore '+value);
	return null;
}
// ############################################################################################################ 
function GetCookie(name)
{
       //name = CookieBranding(name)

	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	                                                                  //   alert(document.cookie);
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return 0;
}
// ############################################################################################################ 
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
// ############################################################################################################ 


function Flickit(CludgeCount)
{
  if(document.getElementById('CLUDGEBUTTON' + CludgeCount).alt=="minus")
  {
    document.getElementById('CLUDGEBUTTON' + CludgeCount).src= ImgPath + 'plus.gif';
    document.getElementById('CLUDGEBUTTON' + CludgeCount).alt = 'plus';
  }
  else
  {
    document.getElementById('CLUDGEBUTTON' + CludgeCount).src= ImgPath + 'minus.gif';
    document.getElementById('CLUDGEBUTTON' + CludgeCount).alt = 'minus';
  }
}



var trees = [];
get_element = document.all ?

 
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

function Wppespandi(Albero, opzione) {
for (var i = 1; i < eval(Albero + '.a_index.length'); i++) {
var o_item = eval(Albero + '.a_index[ i]');
o_item.open(opzione);
}}