/// Package: CDON.ui
Incordia.Packages.create("CDON.ui");
{
	var textNode = 3;
	var notSpace = /[^\s]/;
	
	var getNodes = function(em, tag, max)
	{
		var count = 0;
		var findTags = !!tag;
		tag = (tag||"").toUpperCase();
		
		var set = [];
		for(var i=0, node; node = em.childNodes[i]; ++i)
		{
			if(node.nodeType==textNode || (findTags && node.nodeName!=tag)) continue;
			set.push(node);
			
			if(max && (++count>=max)) break;
		}
		return set;
	}
	
	
	var Cookies =
	{
		create: function(name, value, days)
		{
			var expires = "";
			if (days)
			{
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				expires = "; expires="+date.toGMTString();
			} else expires = "";
			document.cookie = name + "=" + value + expires + "; path=/";
		},
		
		get: function(name)
		{
			var cookies = document.cookie.split(";");
			for(var i=0, mi=cookies.length; i<mi; ++i)
			{
				var cookie = cookies[i].substring(cookies[i].search(/[^\s]/));
				if(cookie.indexOf(name + "=")==0) return cookie.substring(name.length + 1);
			}
			return null;
		},
		
		remove: function(name)
		{
			Cookies.create(name, "", -1);
		}
	};
	
	
	
	/// Class: MenuTree
	CDON.ui.MenuTree = Incordia.Class.extend(
	{
		em: null,
		args: null,
		nodes: null,
		activeNode: null,
		
		constructor: function(element, args)
		{
			this.nodes = [];
			this.em = element;
			this.args = Incordia.merge(CDON.ui.MenuTree.defaults, args);
		},
		
		load: function()
		{
			this.em = $(this.em);
			if(!this.em) return;
			this.em.className += " menutree";
			
			if(0!=this._gatherNodes()) return;
			this._hideAll();
			this._eatCookie();
		},
		
		_eatCookie: function()
		{
			var cookie = Cookies.get(this.em.id + "-menutree");
			if(cookie && this.nodes && this.nodes[cookie])
				this.nodes[cookie].show();
		},
		
		_hideAll: function()
		{
			for(var i=0, node; node = this.nodes[i]; ++i)
				node.hide();
		},
		
		_gatherNodes: function()
		{
			var items = getNodes(this.em, "LI")
			for(var i=0, item; item = items[i]; ++i)
			{
				var title = getNodes(item, "SPAN", 1)[0];
				if(!title) return -1;
				var subList = getNodes(item, "UL", 1)[0];
				if(!subList) return -2;
				
				var node = this._makeNode(title, subList);
				var idx = this.nodes.push(node) - 1;
				node.index = idx;
				addEvent(title, "click", (function(n) { this.nodes[n].toggle(); }).rbind(this, idx));
			}
			return 0;
		},
		
		_makeNode: function(title, subList)
		{
			var system = this;
			
			var node = {
				title: title,
				subList: subList,
				index: -1,
				
				toggle: function()
				{
					if(system.activeNode == this) this.hide();
					else this.show();
				},
				
				show: function()
				{
					if(system.activeNode != null) system.activeNode.hide();
					this.subList.style.display = "";
					system.activeNode = this;
					Cookies.create(system.em.id + "-menutree", this.index, 1);
				},
				
				hide: function()
				{
					this.subList.style.display = "none";
					if(system.activeNode == this) system.activeNode = null;
				}
			};
			return node;
		},
		
		
		global:
		{
			defaults:
			{
			}
		}
	});

}
