//--> CLASS

// Arborescence

function arbo(proj_name, proj_desc, proj_id, tool_tab, info_tab, user_tab) {
	//--> Attributs
	var racine;
	var objtab = new Array();
	var t_node_source = new Array();
	var selected_elmt = null;
	
	//--> Methodes
	var init = function(this_obj) {
		var elmt = document.getElementById("childs_0");
		removeDataChildNodes(elmt);
		t_node_source["task"] = elmt.firstChild.cloneNode(true);
		elmt.removeChild(elmt.firstChild);
		var elmt = document.getElementById("work_space");
		removeDataChildNodes(elmt);
		t_node_source["root"] = elmt.cloneNode(true);
		racine = new project(proj_name, proj_desc, proj_id, this_obj, tool_tab, info_tab, user_tab);
		objtab[proj_id] = racine;
	}
	
	this.new_task = function(parent_id, task_name, task_desc, task_id, tool_t, info_t, user_t) {
		if (objtab[parent_id]) {
			if (!(objtab[task_id]))
				objtab[task_id] = objtab[parent_id].new_child_task(task_name, task_desc, task_id, t_node_source["task"].cloneNode(true), this,  tool_t, info_t, user_t);
			else
				alert("Error : ID already exist !");
		}
		else
			alert("Error : Parent ID not exist : " + parent_id);
	}
	
	this.select_it = function(id) {
		if (selected_elmt)
			objtab[selected_elmt].unselect_task();
		if (id)
			objtab[id].select_task()
		selected_elmt = id;
	}

	//--> Initialisation
	init(this);
}

function project(proj_name, proj_desc, proj_id, p_env, tool_tab, info_tab, user_tab) {
	//--> Attributs
	var id = proj_id;
	var name = proj_name;
	var desc = proj_desc;
	var html_elmts = new Array();
	var task_childs = new Array();
	var this_obj = this;
	var env = p_env;
	var tool_t = tool_tab;
	var info_t = info_tab;
	var user_t = user_tab;
	
	//--> Methodes
	var init = function() {
		html_elmt_index();
		my_setDataNode(html_elmts["title"], name.toUpperCase());
		setDescTask(html_elmts["desc"], desc, 80)
		html_elmts["base"].style.display = 'block';
		init_event();
	}
	
	var html_elmt_index = function() {
		html_elmts["childs"] = document.getElementById("childs_0");
		html_elmts["base"] = document.getElementById("base_0");
		html_elmts["branch"] = document.getElementById("branch_0");
		html_elmts["table"] = document.getElementById("child_table_0");
		html_elmts["head"] = document.getElementById("head_0");
		html_elmts["roller"] = document.getElementById("roller_0");
		html_elmts["title"] = document.getElementById("title_0");
		html_elmts["desc"] = document.getElementById("desc_0");
		html_elmts["childs"].id = 'childs_' + id;
		html_elmts["base"].id = 'base_' + id;
		html_elmts["head"].id = 'head_' + id;
		html_elmts["branch"].id = 'branch_' + id;
		html_elmts["table"].id = 'child_table_' + id;
		html_elmts["roller"].id = 'roller_' + id;
		html_elmts["title"].id = 'title_' + id;
		html_elmts["desc"].id = 'desc_' + id;
	}
	
	this.new_child_task = function(task_name, task_desc, task_id, task_node, p_env, tool_tab, info_tab, user_tab) {
		html_elmts["roller"].style.display = 'block';
		html_elmts["table"].style.display = 'block';
		html_elmts["childs"].appendChild(task_node);
		task_childs[task_childs.length] = new task(task_name, task_desc, task_id, this, p_env, tool_tab, info_tab, user_tab);
		this.resize_branch();
		return (task_childs[task_childs.length - 1]);
	}

	this.resize_branch = function() {
		document.getElementById("branch_" + id).style.height = 0;
		if (html_elmts["childs"].childNodes.length)
			document.getElementById("branch_" + id).style.height = html_elmts["childs"].offsetHeight - html_elmts["childs"].lastChild.offsetHeight + 35 + 1;
	}
	
	var roll_childs = function() {
		if (html_elmts["table"].style.display == 'none') {
			html_elmts["table"].style.display = 'block';
			my_setDataNode(html_elmts["roller"], "-");
		}
		else {
			html_elmts["table"].style.display = 'none';
			my_setDataNode(html_elmts["roller"], "+");
		}
	}

	var init_event =  function() {
		html_elmts["roller"].onmouseover = function() {document.onclick = roll_childs;};
		html_elmts["roller"].onmouseout = function() {document.onclick = null};
		html_elmts["head"].onmouseover = function() {document.onclick = function(){env.select_it(id);};};
		html_elmts["head"].onmouseout = function() {document.onclick = null};
	}
	
	this.select_task = function() {
		html_elmts["head"].className = "selected_p_head";
		html_elmts["title"].className = "selected_p_name";
		document.getElementById("tool_tab").innerHTML = loadTabContent(tool_t); // TODO : load content
		document.getElementById("info_tab").innerHTML = loadTabContent(info_t);
		document.getElementById("user_tab").innerHTML = loadTabContent(user_t);
	}
	
	this.unselect_task = function() {
		html_elmts["head"].className = "p_head";
		html_elmts["title"].className = "p_name";
	}

	//--> Initialisation
	init();
}

function task(task_name, task_desc, task_id, parent, p_env, tool_tab, info_tab, user_tab) {
	//--> Attributs
	var id = task_id;
	var name = task_name;
	var desc = task_desc;
	var parent_obj = parent;
	var html_elmts = new Array();
	var task_childs = new Array();
	var this_obj = this;
	var env = p_env;
	var tool_t = tool_tab;
	var info_t = info_tab;
	var user_t = user_tab;
	
	//--> Methodes
	var init = function() {
		html_elmt_index();
		my_setDataNode(html_elmts["title"], name.toUpperCase());
		setDescTask(html_elmts["desc"], desc, 80)
		html_elmts["contain"].style.display = 'block';
		init_event();
	}
	
	var html_elmt_index = function() {
		html_elmts["childs"] = document.getElementById("childs_x");
		html_elmts["contain"] = document.getElementById("contain_x");
		html_elmts["branch"] = document.getElementById("branch_x");
		html_elmts["table"] = document.getElementById("child_table_x");
		html_elmts["head"] = document.getElementById("head_x");
		html_elmts["roller"] = document.getElementById("roller_x");
		html_elmts["title"] = document.getElementById("title_x");
		html_elmts["desc"] = document.getElementById("desc_x");
		html_elmts["childs"].id = 'childs_' + id;
		html_elmts["contain"].id = 'contain_' + id;
		html_elmts["head"].id = 'head_' + id;
		html_elmts["branch"].id = 'branch_' + id;
		html_elmts["table"].id = 'child_table_' + id;
		html_elmts["roller"].id = 'roller_' + id;
		html_elmts["title"].id = 'title_' + id;
		html_elmts["desc"].id = 'desc_' + id;
	}
	
	this.new_child_task = function(task_name, task_desc, task_id, task_node, p_env, tool_tab, info_tab, user_tab) {
		html_elmts["roller"].style.display = 'block';
		html_elmts["table"].style.display = 'block';
		html_elmts["childs"].appendChild(task_node);
		task_childs[task_childs.length] = new task(task_name, task_desc, task_id, this, p_env, tool_tab, info_tab, user_tab);
		this.resize_branch();
		return (task_childs[task_childs.length - 1]);
	}

	this.resize_branch = function() {
		document.getElementById("branch_" + id).style.height = 0;
		if (html_elmts["childs"].childNodes.length)
			document.getElementById("branch_" + id).style.height = html_elmts["childs"].offsetHeight - html_elmts["childs"].lastChild.offsetHeight + 35 + 1;
		parent.resize_branch();
	}
	
	var roll_childs = function() {
		if (html_elmts["table"].style.display == 'none') {
			html_elmts["table"].style.display = 'block';
			my_setDataNode(html_elmts["roller"], "-");
		}
		else {
			html_elmts["table"].style.display = 'none';
			my_setDataNode(html_elmts["roller"], "+");
		}
		parent.resize_branch();
	}

	var init_event =  function() {
		html_elmts["roller"].onmouseover = function() {document.onclick = roll_childs;};
		html_elmts["roller"].onmouseout = function() {document.onclick = null};
		html_elmts["head"].onmouseover = function() {document.onclick = function(){env.select_it(id);};};
		html_elmts["head"].onmouseout = function() {document.onclick = null};
	}
	
	this.select_task = function() {
		html_elmts["head"].className = "selected_task_head";
		html_elmts["title"].className = "selected_task_name";
		document.getElementById("tool_tab").innerHTML = loadTabContent(tool_t); // TODO : load content
		document.getElementById("info_tab").innerHTML = loadTabContent(info_t);
		document.getElementById("user_tab").innerHTML = loadTabContent(user_t);
	}
	
	this.unselect_task = function() {
		html_elmts["head"].className = "task_head";
		html_elmts["title"].className = "task_name";
	}
	
	//--> Initialisation
	init();
}

/**
 * Loads tab content
 */
function	loadTabContent(id_content)
{
	var		contentPtr;
	var		HTML;

	contentPtr = document.getElementById(id_content);
	if (contentPtr != null)
		HTML = contentPtr.innerHTML;
	else
		HTML = "Erreur 404";
	return (HTML);
}

// Tool Bar

var	tool_bar = function() {
	var html_elmts = new Array();
	var tb_ong = new Array();
	var selected_ong;
	
	var	init = function() {
		html_elmts["base"] = document.getElementById("tool_base");
		html_elmts["hider"] = document.getElementById("tool_hider");
		html_elmts["content"] = document.getElementById("tool_content");
		html_elmts["nav"] = document.getElementById("tool_nav");
		html_elmts["base"].style.display = "none";
		html_elmts["hider"].onclick = hide_bar;
	}

	this.load_ongl = function(title, elmt_id) {
		var onglet_elmt = document.getElementById(elmt_id);
		if (onglet_elmt) {
			var num = tb_ong.length;
			var onglet_copie = onglet_elmt.cloneNode(true);
			onglet_elmt.parentNode.removeChild(onglet_elmt);
			onglet_copie.style.display = "block";
			
			var title_elmt = document.createElement("div");
			title_elmt.className = "tb_ong_title";
			title_elmt.appendChild(document.createTextNode(title));

			var onglet = document.createElement("div");
			onglet.appendChild(title_elmt);
			onglet.appendChild(onglet_copie);
			onglet.style.display = "none";
			html_elmts["content"].appendChild(onglet);
			
			var tab = new Array();
			tab["ong"] = onglet;
			tab["title"] = title;
			tb_ong[num] = tab;
			
			if (tb_ong.length == 1) {
				onglet.style.display = "block";
				selected_ong = 0;
				tab = new Array();
				html_elmts["nav_childs"] = tab;
				html_elmts["base"].style.display = "block";
			}
			else {
				onglet = document.createElement("div");
				onglet.appendChild(document.createTextNode(title));
				onglet.className = "tb_ong_nav";
				var decal = (html_elmts["hider"].offsetWidth + 10 * num);
				onglet.style.marginLeft = decal + "px";
				calc_base_size();
				load_event(onglet, num);
				html_elmts["nav"].appendChild(onglet);
				html_elmts["nav"].insertBefore(onglet, html_elmts["nav"].firstChild);
				html_elmts["nav_childs"][num - 1] = onglet;
			}
		}
		else alert("Error : bad tab ID.");
	}

	var	bar_pos = function() {
		var b_height = ((getWindowHeight() - html_elmts["base"].offsetHeight) / 2);
		if (b_height < 0)
			b_height = 0;
		html_elmts["base"].style.top = b_height + "px";
	}
	
	var hide_bar = function() {
		if (html_elmts["content"].style.width == "auto" || html_elmts["content"].style.width == "") {
			html_elmts["content"].style.width = "1px";
			html_elmts["nav"].style.width = "1px";
		}
		else {
			html_elmts["content"].style.width = "auto";
			calc_base_size();
		}
	}
	
	var change_ong = function(ong_id) {
		tb_ong[selected_ong].ong.style.display = "none";
		tb_ong[ong_id].ong.style.display = "block";
		calc_base_size();
		var j = ong_id;
		for (i = 0; i < html_elmts["nav_childs"].length; i++)
			{
				++j;
				if (j >= tb_ong.length) j = 0;
				if (tb_ong[ong_id].title == html_elmts["nav_childs"][i].firstChild.nodeValue) var ong_onmouse = j;
				my_setDataNode(html_elmts["nav_childs"][i], tb_ong[j].title)
				load_event(html_elmts["nav_childs"][i], j);
			}
		selected_ong = ong_id;
		document.onclick = function() {change_ong(ong_onmouse);};
	}

	var load_event = function(ong, id) {
		ong.onmouseover = function() {document.onclick = function() {change_ong(id);};}
		ong.onmouseout = function() {document.onclick = null;};
	}
	
	var calc_base_size = function() {
		html_elmts["nav"].style.width = "0px";
		html_elmts["nav"].style.width = html_elmts["base"].offsetWidth + "px";
	}

	init();
}



//--> FUNCTIONS

function	removeDataChildNodes(html_elmt) {
	for (var i = 0 ; i < html_elmt.childNodes.length ; i++) {
		if (html_elmt.childNodes[i].nodeType == 3)
			html_elmt.removeChild(html_elmt.childNodes[i]);
	}
}

function	my_setDataNode(parentNode, data_str){
	while (parentNode.hasChildNodes())
		parentNode.removeChild(parentNode.firstChild);
	parentNode.appendChild(document.createTextNode(data_str));
}

function	setDescTask(parentNode, data_str, limit_len){
	if (data_str.length > limit_len + 5) {
		var elmt = document.createElement("a");
		my_setDataNode(elmt, "(...)");
		my_setAttributeNode(elmt, "class", "desc_susp");
		my_setAttributeNode(elmt, "title", data_str);
		data_str = data_str.slice(0, limit_len);
	}
	my_setDataNode(parentNode, data_str);
	if (elmt)
		parentNode.appendChild(elmt);
}

function	my_setAttributeNode(node, attribute_name, attribute_value) {
	var attribute_node = document.createAttribute(attribute_name);
	attribute_node.nodeValue = attribute_value;
	node.setAttributeNode(attribute_node);
}



function getWindowHeight() {
	if (document.body.offsetHeight) return document.body.offsetHeight;
	else return window.innerHeight;
}

function getWindowWidth() {
	if (document.body.offsetWidth) return document.body.offsetWidth;
	else return window.innerWidth;
}


