/* Minimalist treeview library */
function categories(main_container, unique, categories, nodeOnClick, inputCat, inputTag)
{
/* Initialize the properties */
this.unique = unique;
this.main_container = main_container;
this.categories = categories;
this.selectedValue = null;
this.inputCategory = inputCat;
/* Don't know what for did I wanted this */
this.inputTag = inputTag;

/* Adds a node into the tree */
this.add = function(id, parent_id, name)
{		
	if (id == undefined || parent_id == undefined || name == undefined)
		return false;
	elem = document.createElement('div');
	elem.id = this.unique + "_" + id.toString();

	elem.innerHTML = name;
	elem.className = "tags_submenu";
	elem.catId = id;
	elem.catName = name;
	elem.hasChildren = false;
	elem.catParent = parent_id;
	elem.cats = this;
	elem.attachedNodes = new Array();
	elem.catOpen = false;

	if (parent_id == 0)
	{
		this.main_container.appendChild(elem);
		//elem.style.display = 'block';
	}
	else if (document.getElementById(this.unique + "_" + parent_id))
	{
		document.getElementById(this.unique + "_" + parent_id).className = "tags_menu";
		document.getElementById(this.unique + "_" + parent_id).appendChild(elem);
		document.getElementById(this.unique + "_" + parent_id).attachedNodes.push(elem);
		elem.style.display = 'none';
	}
	if (window.event)
	{
		elem.onclick = getCatClick;
	}
	else 
	{
		elem.addEventListener('click', getCatClick, false)	
	}
}

this.select = function(object) 
{
	/* If it's a number then most surely we consider the arg to be id */
	if (typeof(object) == 'numeric')
	{
		object = this.getElem(object);
		if (typeof(object)!="object")
		{
			return false;
		}
	}
	/* Select new one */
	if (object && !object.hasChildren)
	{
		/* Unselect the old value first */
		if (this.selectedValue) 
		{
			//this.closeALL();
			elem = this.getElem(this.selectedValue[0]);
			if (!elem.attachedNodes.length)
			{
				elem.className = "tags_submenu";
			}
		}
		if (!object.attachedNodes.length)
		{			
			object.className = "tags_submenu selected";
		}
		this.selectedValue = [object.catId, object.catParent, object.catName];
		this.inputCategory.value = object.catId;
		this.open(object);
		if (this.extraOnSelect)
		{
			this.extraOnSelect(object);
		}
	}
}

this.deselect = function() 
{
	this.inputCategory.value = '';
	this.closeALL();	
	if (this.selectedValue) 
	{
		this.getElem(this.selectedValue[0]).className = 'tags_submenu';
		this.selectedValue = false;
	}
	if (this.extraOnClear) 
	{
		this.extraOnClear();
	}
}

/* Iterates through parents to open the tree. */
this.open = function(object)
{
	if (!object)
	{
		return;
	}
	this.open(this.getElem(object.catParent));
	if (!object.attachedNodes.length)
	{
		if (this.selectedValue) 
		{
			obj = document.getElementById(this.unique + '_' + this.selectedValue[0]);
			if (obj.hasChildren) 
			{
				obj.className = 'tags_submenu';
			}
		}		
		object.className = "tags_submenu selected";
		return;
	}
	for (i=0;i<object.attachedNodes.length;i++)
	{
		object.attachedNodes[i].style.display = 'block';
	}
	object.catOpen = true;
}

this.getSelectedId = function() 
{
	if (!this.selectedValue)
		return false;
	return this.selectedValue[0];
}

this.getSelectedName = function()
{
	if (!this.selectedValue)
		return false;
	return this.selectedValue[2];
}

this.getSelectedParentId = function()
{
	if (!this.selectedValue)
		return false;
	return this.selectedValue[1];
}

this.getSelectedParentName = function()
{
	if (!this.selectedValue)
		return false;
	for (i in this.categories) 
	{
		if (this.categories[i][0] == this.selectedValue[1])
			return this.categories[i][2];
	}
}

this.closeALL = function() 
{
	for (i in categories)
	{
		catElem = this.getElem(categories[i][0]);
		if (catElem && catElem.catParent == 0 && catElem.attachedNodes && catElem.attachedNodes.length && catElem.catOpen)
		{
			this.close(catElem);
		}
	}
}

this.close = function(object)
{
	if (!object || !object.attachedNodes.length || !object.catOpen) 
	{
		alert(object.catOpen);
		return;
	}
	for (i=0;i<object.attachedNodes.length;i++)
	{
		object.attachedNodes[i].style.display = 'none';
	}	
	object.catOpen = false;
}

this.getElem = function(id)
{
	return document.getElementById(this.unique + '_' + id);
}

this.collapse = function(id)
{
	for (i in this.categories)
	{
		if (categories[i][1] == id && document.getElementById(unique + "_" + categories[i][0]) )
		{
			document.getElementById(unique + "_" + categories[i][0]).style.display = 'block';
		}
	}	
}

/* Initialize the output */
for(i=0;i<this.categories.length;i++) 
{
	if (i==0)
		continue;
	if (this.categories[i][0] == undefined && this.categories[i][1] == undefined && this.categories[i][2] == undefined)
		continue;

	this.add(this.categories[i][0], this.categories[i][1], this.categories[i][2]);
}


if (this.inputCategory && this.inputCategory.value) 
{
	this.select(this.getElem(parseInt(this.inputCategory.value)));
}

}

getCatClick = function(event)
{
	/* We don't need no bubbling */
	if (window.event)
	{
		window.event.cancelBubble = true;
	}
	else 
	{
		event.stopPropagation();
	}
	
	if (!this.attachedNodes.length)
	{
		this.cats.select(this);
		return true;
	}
	
	if (this.catOpen) 
	{
		this.cats.close(this);
	}
	else 
	{
		this.cats.open(this);
	}
	
	return true;
}
