//mrj.require("helpers.Tree");

mrj.menus.Accordion = mrj.createClass(
{
	init: function (id, params)
	{
		this.id = id;
		this.activeMenu = null;
		this.menus = new Array();
		this.menuIdMap = new Object();
		this.tree = new mrj.helpers.Tree();
		this.tree.parent = this;
		this.styles = new Object();
		this.activeMenu = null;
		
		if (this.params != undefined)
		{
			if (this.params.placeHolder != undefined)
			{
				this.placeHolder = mrj.newElement(this.params.placeHolder);
				this.params.placeHolder = undefined;
			}
		}
		
		if (this.placeHolder == undefined)
		{
			this.placeHolder = new mrj.Element(id);
		}
		
		applyParams(this, params);
	},
	
	setStyles: function (prefix)
	{
		this.styles = mrj.css.prefixedClassesToObject(prefix);
	},
	
	createMenu: function ()
	{
		var n = new mrj.newClass(this.Menu, arguments);
		this.menus.push(n);
		this.menuIdMap[arguments[0]] = n;
	},

	getMenu: function (id)
	{
		return this.menuIdMap[id];
	},

	setData: function (data)
	{
		if (data.tree != undefined)
		{
			this.tree.setData(data.tree);
		}
	},
	
	render: function ()
	{
		this.placeHolder.applyStyle(this.styles.container);
		this.tree.iterate(null, this.renderNode, 1);
		
		if (this.defaultOpen)
			this.openMenu(this.defaultOpen);
	},

	renderNode: function (menu, node, level)
	{
		node.level = level;
		if (node.isParent() || node.isRoot())
		{
			node.element = menu.placeHolder.createElement("div");
			node.menuElement = menu.placeHolder.createElement("div");
			node.element.node = node;
			node.element.menu = menu;

			if (menu.beforeRenderParent != undefined)
				menu.beforeRenderParent(node);

			node.element.setContent(node.title);
			node.element.createEvent("onclick", menu.parentNodeClick);
			
			node.element.htmlElement.className = 'acMenu_parent acMenu_' + node.id;
			node.element.applyStyle(menu.styles.parent);
			node.element.applyStyle(node.style);
			
			node.menuElement.setScrollable();
			
			if (node.open == undefined) node.open = false;
			node.menuElement.scrollContainer.setHeight(0);

			if (node.open) menu.defaultOpen = node;
			
		}
		else
		{
			node.element = node.parent.menuElement.createElement("div");
			node.element.setContent(node.title);
			node.element.htmlElement.className = 'acMenu_child';
			node.element.applyStyle(menu.styles.child);
			node.element.applyStyle(node.style);
			
			node.element.htmlElement.style.position = "relative";
			node.element.node = node;
			node.element.menu = menu;
						
			node.element.createEvent("onmouseover", function (e) { e.fade({ opacity: 50 }); });
			node.element.createEvent("onmouseout", function (e) { e.fade({ opacity: 100, delay: 200 }); });
			node.element.createEvent("onclick", menu.childNodeClick);
		}
	},
	
	childNodeClick: function (e)
	{
		if (typeof(e.node.url) == "string")
			top.location.href = e.node.url;
		else if (typeof(e.node.url) == "function")
			e.node.url(e.node);
	},
	
	parentNodeClick: function (e)
	{
		e.menu.toggleMenu(e.node);
	},
	
	toggleMenu: function (node)
	{
		if (node.open)
			this.closeMenu(node);
		else
			this.openMenu(node);
	},
	
	openMenu: function (node)
	{
		if (this.activeMenu != null && this.activeMenu != node)
		{
			this.closeMenu(this.activeMenu);
		}
		
		var h = node.menuElement.getHeight();
		node.menuElement.slide({ height: h });
		node.open = true;
		this.activeMenu = node;
	},

	closeMenu: function (node)
	{
		node.menuElement.slide({ height: 0 });
		node.open = false
		this.activeMenu = null;
	},

	Menu: mrj.createClass({
		init: function (id)
		{
			this.id = id;
		}
	}) 

});


