var speed = 20;
var enableOpen = true;
var anim = new Pipeline("anim", 5);
var tab_div_unvisible = [];


/* Pipeline Class */
function Pipeline (ref, interval)
{
	this.pipeline = new Array();
	this.pipeline_ptr = 0;
	this.timeout = interval;
	this.ref = ref;

	this.play = Pipeline_play;
	this.reset = Pipeline_reset;
	this.add = Pipeline_add;
}

function Pipeline_reset ()
{
	this.pipeline = new Array();
	this.pipeline_ptr = 0;
}

function Pipeline_play ()
{
	if (this.pipeline.length == 0) return;
	
	eval (this.pipeline[this.pipeline_ptr]);
	
	this.pipeline_ptr++;
	
	if (this.pipeline_ptr < this.pipeline.length)
	{
		setTimeout (this.ref + ".play()", this.timeout);
	}
	else
	{
		this.reset();
	}
}

function Pipeline_add (callback)
{
	this.pipeline[this.pipeline.length] = callback;
}



/* Tree Initializer */

// Ouverture des sous menus

function open_children (data)
{
	for (var i = 0; i < data.length; i++)
	{
		if ( (i < data.length - 1) && (data[i + 1][0] > data[i][0]) ) 
		{ 
			var url	= (data[i].length == 4) ? data[i][2] : null;
			if (url!=null && url.indexOf(data[i][3]) > -1)
			{
				tree_close_open (i);
				anim.play ();
			}
		}
	}
}



function init_trees (x, y, data)
{
	// Auto Open
	var item_states = new Array();
	for (var i = 0; i < tree_state.length; i++)
	{
		var index = tree_state[i];
		item_states[index] = true;
		
		for (var j = index + 1; j < data.length; j++)
		{
			
			if (data[j][0] == data[index][0]) break;
			else if (data[j][0] == data[index][0] + 1) item_states[j] = true;
		}
	}

	// Build trees
	var selected_state = new Array();
	var arr = new Array();
	for (var i = 0; i < data.length; i++)
	{
		var depth	= data[i][0];
		var name	= data[i][1];
		var url	= (data[i].length == 4) ? data[i][2] : null;
		var group	= (i < data.length - 1 && data[i + 1][0] > depth) ? true : false;
		var modsel= ( url.indexOf(data[i][3]) > -1) ? true : false;
		
		selected_state[depth] = i;
		arr[i] = tree_init (i, depth, name, url, group, item_states[i], tree_state, selected_state, modsel);
		arr[i].moveTo (x, y);
	}
	
	// Initial Placement
	var lasty = y;
	for (var i = 0; i < arr.length; i++)
	{
		if (arr[i].depth == 0 || arr[i].style.visibility == "visible")
		{
			arr[i].moveTo (x, lasty);
			arr[i].show();
			lasty += arr[i].ysize;
		}
	}
	getXpos = lasty;
	return arr;
}

function build_tree_state (selected_state, depth)
{
	var str = "&location.tree:=";
	for (var i = 0; i <= depth; i++)
	{
		if (i != 0) str += ",";
		str += selected_state[i];
	}
	return str;
}

function tree_init (idx, depth, name, url, group, item_state, state, selected_state, isselected)
{
	var item;
	
	var str =  url + build_tree_state (selected_state, depth);
	item = tree_drawItem (idx, depth, name, str, group, selected_state,isselected);
	item.isGroup = group;
	if (!group) item.url = str;

	item.depth = depth;
	item.idx = idx;
	item.style.visibility = (item_state) ? "visible" : "hidden";
	item.opened = (depth < state.length) ? (state[depth] == idx) : false;
	item.style.cursor = "default";
	
	item.onclick = tree_action;
	item.moveTo	= tree_moveTo;
	item.move	= tree_move;
	item.show	= tree_show;
	item.hide	= tree_hide;
	item.isSelect = isselected;

	setImage (item);

	return item;
}

function tree_moveTo (x, y)
{
	this.xpos = x;
	this.ypos = y;
	this.style.left = x + "px";
	this.style.top = y + "px";
}

function tree_move (x, y)
{
	this.xpos += x;
	this.ypos += y;
	this.style.left = this.xpos + "px";
	this.style.top = this.ypos + "px";
}

function tree_show ()
{
	this.style.visibility = "visible";
}

function tree_hide ()
{
	this.style.visibility = "hidden";
	this.opened = false;
	
	setImage(this);
}

/* End Of Trees Initializer */


function setImage (tree)
{
	if (tree.isGroup)
	{
		var img = document.getElementById ("img" + tree.idx);
		if (tree.isSelect)
		{
			try{img.src = img_leaf;}catch(E){};
		}
		else
		{
			try{img.src = (tree.opened) ? img_opened : img_closed;}catch(E){};
		}
	}
	else 
	{
		try{img.src = (tree.opened) ? img_opened : img_closed;}catch(E){};
	}
	
}

/* tree anims */
function tree_action ()
{
	if (this.isGroup)
	{
		if (this.opened) 
		{
			tree_close (this.idx);
		}
		else 
		{
			tree_close_open (this.idx);
		}
		anim.play ();
	}
	else
	{
		var u = this.url;
		if( u!=null && u.indexOf('javascript:')<0 )
		{
			document.location.href=u;
		}
		else if( u!=null && u.indexOf('javascript:')==0 )
		{
			eval( u );	
		}
	}
}

function tree_close_open (idx)
{
	var prevOpen = -1
	for (var i = 0; i < trees.length; i++)
	{
		var curr = trees[i];
		if (curr.depth == trees[idx].depth && curr.opened == true)
		{
			prevOpen = i;
			break;
		}
	}
	
	if (prevOpen != -1) tree_close (prevOpen);

	tree_open (idx);
}

function findNextItem (idx)
{
	var depth = trees[idx].depth;
	for (var i = idx + 1; i < trees.length; i++)
	{
		if (trees[i].depth <= depth) return i;
	}
	return trees.length;
}

function tree_open (idx)
{
	if (!enableOpen)
	{
		anim.add ("tree_open ("+idx+")");
		return;
	}
	
	var tree	= trees[idx];
	var next	= findNextItem (idx);
	var offset	= 0;
	
	tree.opened = true;

	setImage (trees[idx]);
	
	for (var i = idx + 1; i < next; i++)
	{
		var curr = trees[i];
		getXpos+= curr.ysize;
		if (curr.depth == (tree.depth + 1))
		{
			offset += curr.ysize;
		}
	}

	anim.add ("_tree_open(" + idx + ", " + next + ", " + offset + ", " + 0 + ", " + idx + ")");
}

function _tree_open (from, to, offset, pos, lastshown)
{
	pos += speed;
	
	var pix = (pos > offset) ? speed - (pos - offset) : speed;
	
	for (var i = to; i <= (trees.length - 1); i++)
	{
		var curr = trees[i];
		if (curr.style.visibility == "visible")
		{
			curr.move (0, pix);
		}
	}
	
	var lasty = trees[lastshown].ypos + trees[lastshown].ysize;
	if (to == trees.length || lasty < trees[to].ypos)
	{
		for (var i = lastshown + 1 ; i < to ; i++)
		{
			if (trees[i].depth == trees[from].depth + 1)
			{
				trees[i].moveTo (trees[i].xpos, lasty); 
				trees[i].show();

				lastshown = i;
				break;
			}
		}
	}
	
	if (pos < offset)
	{
		anim.add ("_tree_open("+from+","+ to+","+ offset+", "+pos+", "+lastshown+")");
	}
}

function tree_close (idx)
{
	enableOpen = false;
	var tree	= trees[idx];
	var next	= findNextItem (idx);
	var offset = 0;
	
	tree.opened = false;
	
	setImage (trees[idx]);
	
	for (var i = idx + 1; i < next; i++)
	{
		var curr = trees[i];
		if (curr.style.visibility == "visible")
		{
			offset += curr.ysize;
		}
	}

	anim.add ("_tree_close(" + idx + ", " + next + ", " + offset + ", " + 0 + ", -1)");
}

function _tree_close (from, to, offset, pos, lasthidden)
{
	pos += speed;
	
	var pix = (pos > offset) ? speed - (pos - offset) : speed;
	
	for (var i = to; i <= (trees.length - 1); i++)
	{
		var curr = trees[i];
		if (curr.style.visibility == "visible")
		{
			curr.move (0, -pix);
		}
	}
	
	if (lasthidden == -1) lasthidden = nextToHide (from, to);
	
	if (lasthidden != -1)
	{
		var lasty = trees[lasthidden].ypos;
		if (to == trees.length || lasty >= trees[to].ypos)
		{
			for (var i = lasthidden; i > from ; i--)
			{
				if (trees[i].style.visibility == "visible")
				{
					trees[i].hide();
					lasthidden = nextToHide (from, i);
					break;
				}
			}
		}
	}

	if (pos < offset)
	{
		anim.add ("_tree_close("+from+","+ to+","+ offset+", "+pos+", "+lasthidden+")");
	}
	else
	{
		enableOpen = true;
	}

}

function nextToHide (from, to)
{
	for (var i = to - 1 ; i > from ; i--)
	{
		if (trees[i].style.visibility == "visible")
		{
			return i;
		}
	}
	return -1;
}


/* menu code */

function init_menus (x, y, data)
{
	var arr = new Array ();
	
	for (var i = 0; i < data.length; i++)
	{
		var menu = init_menu (i, data);
		arr[i] = menu;
	}
	
	var lastx = x;
	
	for (var i = 0; i < arr.length; i++)
	{
		var menu = arr[i];
		menu.style.left = lastx;
		menu.style.top = y + 1;
		menu.style.visibility = "visible";
		menu.style.height = menu.offsetHeight;
		
		if (typeof menu.content != "undefined")
		{
			menu.content.style.left = lastx - 1;
			menu.content.style.top = y + menu.offsetHeight + 2;
			menu.content.style.height = menu.content.offsetHeight;
		}
		
		lastx += menu.offsetWidth;
	}
	
	arr[0].totalWidth = lastx;

	return arr;
}

//- Pour menu haut -

function menu_color (active)
{
	if (active) return color1;//"#d2d0d1";
	else return color4;
}

function menu_color2 (active)
{
	if (active) return color3;//"#ffffff";
	else return color2;
}

function menu_setActive ()
{
	if( menu_color )
		this.bgColor = menu_color (true);
}

function menu_setInactive ()
{
	if( menu_color )
		this.bgColor = menu_color(false);
}



//- Pour sous menu --

function ssmenu_color (active)
{
	if (active) return color1;//"#d2d0d1";
	else return color2;
}

function ssmenu_color2 (active)
{
	if (active) return color3;//"#ffffff";
	else return color2;
}

function ssmenu_setActive ()
{
	if( menu_color )
		this.bgColor = ssmenu_color (true);
}

function ssmenu_setInactive ()
{
	if( menu_color )
		this.bgColor = ssmenu_color(false);
}

function init_menu (idx, data)
{
	var item;
	
	if (data[idx].length == 1)
	{
		item = drawMenuRoot (idx, data);
		item.style.visibility = "hidden";
		//NS6
		if (document.getElementById&&!document.all)
		{
			item.style.cursor = "pointer";
		}
		else
		{
			item.style.cursor = "hand";
		}
		//item.style.cursor = "default";
		item.idx = idx;
	}
	else
	{
		item = drawMenu (idx, data);
	}
	
	return item;
}

function menu_click ()
{
	var u = this.url;
	if( u!=null && u.length>0 && u.indexOf('javascript:')<0 )
	{
		document.location.href=u;
	}
	else if( u!=null && u.indexOf('javascript:')==0 )
	{
		eval( u );	
	}
}

function drawMenu (idx, data)
{
	var root = drawMenuRoot (idx, data);
	var content = drawMenuContent (idx, data);
	
	content.style.visibility = "hidden";
	//NS6
	if (document.getElementById&&!document.all)
	{
		content.style.cursor = "pointer";
	}
	else
	{
		content.style.cursor = "hand";
	}
	//content.style.cursor = "default";
	content.inside = false;
	content.onmouseover = menu_content_enter;
	if (document.all)
		content.onmouseout = menu_content_leave;

	root.content = content;
	content.root = root;

	root.style.visibility = "hidden";
	//NS6
	if (document.getElementById&&!document.all)
	{
		root.style.cursor = "pointer";
	}
	else
	{
		root.style.cursor = "hand";
	}
	//root.style.cursor = "hand";
	//root.style.cursor = "default";
	root.idx = idx;

	root.inside = false;
	root.onmouseover = menu_enter;
	root.onmouseout = menu_leave;

	return root;
}

function menu_enter ()
{
	//window.status=this.url;
	
	for( i = 0  ; i < tab_div_unvisible.length ; i ++ )
	{
			try	{document.getElementById(tab_div_unvisible[i]).style.visibility='hidden';}catch(e){}
	}
	
	for (var i = 0; i < menus.length; i++)
	{
		var menu = menus[i];
		if (typeof menu.content != "undefined")
		{
			menu.content.inside = false;
			if (this.idx == i)
			{
				menu.inside = true;
				menu.content.style.visibility = "visible";
			}
			else
			{
				menu.inside = false;
				menu.content.style.visibility = "hidden";
			}
		}
	}
}

function menu_leave ()
{
	this.inside = false;
	var menu = menus[this.idx];
	if (menu.content.inside == false && menu.inside == false)
	{
		for( i = 0  ; i < tab_div_unvisible.length ; i ++ )
		{
			try{document.getElementById(tab_div_unvisible[i]).style.visibility='visible'; }catch(e){}
		}
	}
	setTimeout ("_menu_leave("+this.idx+")", 200);
	
}


function _menu_leave (idx)
{
	var menu = menus[idx];
	if (menu.content.inside == false && menu.inside == false)
	{
		menu.content.style.visibility = "hidden";
	}
}

function menu_content_enter ()
{
	for( i = 0  ; i < tab_div_unvisible.length ; i ++ )
	{
		try	{document.getElementById(tab_div_unvisible[i]).style.visibility='hidden';}catch(e){}
	}
	this.inside = true;
	this.style.visibility = "visible";
}

function menu_content_leave ()
{
	//window.status="";
	
	this.inside = false;
	if (this.root.inside == false)
	{
		this.style.visibility = "hidden";
		for( i = 0  ; i < tab_div_unvisible.length ; i ++ )
		{
			try	{document.getElementById(tab_div_unvisible[i]).style.visibility='visible';}catch(e){}
		}
	}
}
