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

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

function init_trees (x, y, data)
{
	// Auto Open
	//alert ("init_trees");
	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 == 3) ? data[i][2] : null;
		var group	= (i < data.length - 1 && data[i + 1][0] > depth) ? true : false;
		
		selected_state[depth] = i;
		
		arr[i] = tree_init (i, depth, name, url, group, item_states[i], tree_state, selected_state);
		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;
		}
	}

	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)
{
	var item;
	
	var str =  url + build_tree_state (selected_state, depth);

	item = tree_drawItem (idx, depth, name, str, group, selected_state);
	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;

	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);
		img.src = (tree.opened) ? img_opened : img_closed;
	}
}

/* tree anims */
function tree_action ()
{
	if (this.isGroup)
	{
		if (this.opened) tree_close (this.idx);
		else 			 tree_close_open (this.idx);
		anim.play ();
	}
	else
	{
		document.location.href= this.url;
	}
}

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

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

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

/*
function menu_setActive(idx)
{
	this.bgColor = menu_color(true,idx) ;
}


function menu_setInactive(idx)
{
	this.bgColor = menu_color(false,idx);
}
*/

function menu_setActive0 ()
{
	this.bgColor = menu_color0(true) ;
}

function menu_setInactive0 ()
{
	this.bgColor = menu_color0(false);
}

function menu_setActive1 ()
{
	this.bgColor = menu_color1(true) ;
}

function menu_setInactive1 ()
{
	this.bgColor = menu_color1(false);
}

function menu_setActive2 ()
{
	this.bgColor = menu_color2(true) ;
}

function menu_setInactive2 ()
{
	this.bgColor = menu_color2(false);
}

function menu_setActive3 ()
{
	this.bgColor = menu_color3(true) ;
}

function menu_setInactive3 ()
{
	this.bgColor = menu_color3(false);
}

function menu_setActive4 ()
{
	this.bgColor = menu_color4(true) ;
}

function menu_setInactive4 ()
{
	this.bgColor = menu_color4(false);
}

function menu_setActive5 ()
{
	this.bgColor = menu_color5(true) ;
}

function menu_setInactive5 ()
{
	this.bgColor = menu_color5(false);
}
function menu_setActive6 ()
{
	this.bgColor = menu_color6(true) ;
}

function menu_setInactive6 ()
{
	this.bgColor = menu_color6(false);
}


function init_menu (idx, data)
{
	var item;
	
	if (data[idx].length == 1)
	{
		item = drawMenuRoot (idx, data);
		item.style.visibility = "hidden";
		item.style.cursor = "default";
		item.idx = idx;
	}
	else
	{
		item = drawMenu (idx, data);
	}
	
	return item;
}

function menu_click ()
{
	document.location.href=this.url;
}

function drawMenu (idx, data)
{
	var root = drawMenuRoot (idx, data);
	var content = drawMenuContent (idx, data);
	
	content.style.visibility = "hidden";
	content.style.cursor = "default";
	content.inside = false;
	content.onmouseover = menu_content_enter;
	content.onmouseout = menu_content_leave;

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

	root.style.visibility = "hidden";
	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 (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";
//hsg
				menu.content.style.bgColor = color[i][0];
			}
			else
			{
				menu.inside = false;
				menu.content.style.visibility = "hidden";
			}
		}
	}
}

function menu_leave ()
{
	//window.status="";
	
	this.inside = false;
	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";
//hsg
		menu.content.style.bgcolor = color[idx][0];
	}
}

function menu_content_enter ()
{
	//window.status=this.url;
	
	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";
}

/* time code */
function init_times (x, y)
{
	var item = draw_times (x, y);

	item.style.left = x - item.offsetWidth - 1;
	item.style.top = y;
	item.style.visibility = "visible";
	
	update_times ();
}

function update_times ()
{
	var stamp = new Date();
	var hh = stamp.getHours();
	var mi = stamp.getMinutes();
	var ss = stamp.getSeconds();
	
	var dhh;
	
	if (mi < 10) mi = "0" + mi;
	if (ss < 10) ss = "0" + ss;

	for (var i = 0; i < 3; i++)
	{
		var time = document.getElementById ("time" + i);
		
		var offset = 0;
		if (i == 1) offset = -6;
		if (i == 2) offset = +8;
		
		dhh = hh + offset;
		
		if (dhh > 23) dhh -= 24;
		
		if (dhh < 10) dhh = "0" + dhh;
		
		var txt = time.firstChild;
		time.removeChild(txt);
		txt = document.createTextNode (dhh + ":" + mi + ":" + ss);
		time.appendChild (txt);
	}

	setTimeout ("update_times();", 1000);
}

/* end of time code */

/* Current path code */
function init_path (x, y)
{
	var str = "init path";
	var item;

	if (tree_state.length > 0) str = init_path_from_tree ();
	else if (menu_state.length > 0) str = init_path_from_menu ();
	
	draw_path (str);

	item = document.getElementById ("curr_path_shadow");
	item.style.left = x;
	item.style.top = y;
	item = document.getElementById ("curr_path");
	item.style.left = x-2;
	item.style.top = y-2;
}

function init_path_from_tree ()
{
	var str = "";

	for (var i = 0; i < tree_state.length; i++)
	{
		var curr = tree_data[tree_state[i]][1];
		str += curr;
		if (i < tree_state.length - 1) str += " > ";
	}
	
	return str;
}

function init_path_from_menu ()
{
	var str = menu_data[menu_state[0]][0][0];
	if (menu_state.length == 2)
	{
		str += " > ";
		str += menu_data[menu_state[0]][menu_state[1]][0];
	}
	
	return str;
}

/* end of current path code */
