/**
 * Copyright (c)2005-2007 Matt Kruse (javascripttoolbox.com)
 * 
 * Dual licensed under the MIT and GPL licenses. 
 * This basically means you can use this code however you want for
 * free, but don't claim to have written it yourself!
 * Donations always accepted: http://www.JavascriptToolbox.com/donate/
 * 
 * Please do not link to the .js files on javascripttoolbox.com from
 * your site. Copy the files locally to your server instead.
 * 
 */
/*
This code is inspired by and extended from Stuart Langridge's aqlist code:
		http://www.kryogenix.org/code/browser/aqlists/
		Stuart Langridge, November 2002
		sil@kryogenix.org
		Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) 
		and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
*/

// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,false); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Full expands a tree with a given ID
function expandTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	var ret = expandCollapseList(ul,nodeOpenClass,itemId);
	if (ret) {
		var o = document.getElementById(itemId);
		if (o.scrollIntoView) {
			o.scrollIntoView(false);
		}
	}
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (itemId!=null && item.id==itemId) { return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;
						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		var uls = document.getElementsByTagName("ul");
		if (uls==null) { return; }
		var uls_length = uls.length;
		for (var uli=0;uli<uls_length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

function treeNodeOnclick() {
  this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
	//is_super_task(this.parentNode);
  return false;
}
function retFalse() {
	return false;
}
// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	var childNodesLength = ul.childNodes.length;
	for (var itemi=0;itemi<childNodesLength;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			var itemChildNodesLength = item.childNodes.length;
			for (var sitemi=0;sitemi<itemChildNodesLength;sitemi++) {
				var sitem = item.childNodes[sitemi];
//	alert(sitem.innerHTML);
//  alert(itemChildNodesLength);
				if (sitem.nodeName=="UL") {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeOpenClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = treeNodeOnclick;
			}
			else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = retFalse;
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}


// HITM added functions 


//update the name of the task
function change_name(li_id,new_name)
{
  if (!li_id.childNodes || li_id.childNodes.length==0) { return; }
  var childNodesLength = li_id.childNodes.length;
	for (var itemi=0;itemi<childNodesLength;itemi++) 
  {
  var item = li_id.childNodes[itemi];
	if (item.nodeName == "A") 
    {
      item.innerHTML =new_name;
    }
  }
//now we hide the update dialog and show success message
    div_toggle(['fade','task_box'],['notice'],false);
    $('notice').innerHTML = "Task \'"+new_name+"\' was updated!";
}

//task was updated, but the name was not. So, we need to provide notification only
function change_details(name)
{
    div_toggle(['fade','task_box'],['notice'],false);
    $('notice').innerHTML = "Task \'"+name+"\' was updated!";
}

//changes parent expand/colapse mode to bullet is no more kids are there
function update_expandability(li_id)
{
  if (is_super_task(li_id))
  // there are children so should be expandable
  {
    //alert('expandable');
    //change to expandable if currently bullet
    if (li_id.firstChild.nodeName=="SPAN" && li_id.firstChild.className== nodeBulletClass)
    {
      li_id.firstChild.className = nodeOpenClass;
      alert('Change it!');
    }
  }
  else
  // no children so should be a bullet
  {
    alert('bullet');
  }
}


// helper that returns true if task is li and has ul underneath it; otherwise false
function is_super_task(t_id)
{
  //only valid li should be avaluated
  if (!t_id.childNodes || t_id.childNodes.length==0 || t_id.nodeName != "LI") {return false;}
  //loop to see if there any sublists i.e. kids
  var subLists = false;
	var itemChildNodesLength = t_id.childNodes.length;
  for (var sitemi=0;sitemi<itemChildNodesLength;sitemi++) 
  {
	 var sitem = t_id.childNodes[sitemi];
		if (sitem.nodeName=="UL") 
    {
			subLists = true;
		}
  }
  return subLists;
}


//helper that returns openning ul element 
function task_wrapper(t_id)
{
  //only valid li should be avaluated
  if (!t_id.childNodes || t_id.childNodes.length==0 || t_id.nodeName != "LI") {return false;}
  //loop to see if there any sublists i.e. kids
 	var itemChildNodesLength = t_id.childNodes.length;
  for (var sitemi=0;sitemi<itemChildNodesLength;sitemi++) 
  {
	 var sitem = t_id.childNodes[sitemi];
		if (sitem.nodeName=="UL") 
    {
			return sitem;
		}
  }
  return false;
}





// create subtask
function create_task(parent_id,id_s,name)
{
//does parent have children already?
if (is_super_task(parent_id))
//if so just create new one on the bottom of exiting ul
  {
   new Insertion.Bottom(task_wrapper(parent_id),'<LI id=t_'+id_s+'> <a href="#" onclick=" loadTask('+id_s+');return false;">'+name+'</a></LI>') ;
  }
else
//create a brand new ul with new item on the bottom right after it
{
   new Insertion.Bottom(parent_id,'<ul><LI id=t_'+id_s+'> <a href="#" onclick=" loadTask('+id_s+');return false;">'+name+'</a></LI></ul>') ;
}
  //make it a bullet
     //insert "that" span for the style
	var s= document.createElement("SPAN");
	var t= '\u00A0'; // &nbsp;
  s.className = "bullet";
  s.onclick = retFalse;
	var item = $('t_'+id_s);
  item.className = "liBullet";
  s.appendChild(document.createTextNode(t));
	item.insertBefore(s,item.firstChild);
  //now update parent first child to be expandable
   parent_id.firstChild.onclick = treeNodeOnclick;
   parent_id.className = "liOpen";
   //parent_id.appendChild(ul);
   
   //now we return back to the project tree
   div_toggle(['fade','task_box'],['notice'],false);
    $('notice').innerHTML = "Task \'"+name+"\' was created!";

}


//delete task
function delete_task(t_id,name)
{
  var parent_node = t_id.parentNode;
  var parent_task = parent_node.parentNode;
  Element.remove(t_id);
  if (!parent_node.childNodes || parent_node.childNodes.length==0) 
  {
    Element.remove(parent_node);
    parent_task.className = "liBullet";
    parent_task.firstChild.onclick = retFalse;
  }
   //now we return back to the project tree
   div_toggle(['fade','task_box'],['notice'],false);
    $('notice').innerHTML = "Task \'"+name+"\' was deleted!";

}
