﻿/*
Class: TreeMenu
	Generic tree menu builder class. Uses a UL element with set elements to create a expand / collapse list tree menu
	
Example:
	> var menu = new TreeMenu( $('MyUL'), null );
	> var menu = new TreeMenu( $('.containerElement');
	
Authors:
	Ryan Marsh <ryan@fsite.com>
*/

var TreeMenu = new Class({
	options: {
		id: null,
		container: null,
		items: null,
		expandRootNode: null
	},

	cookie: null,
	openIndex: null,

	initialize: function(el, options) {
		this.setOptions(options);
		this.options.container = $(el);

		if (!$defined(this.options.expandRootNode))
			this.options.expandRootNode = this.options.container.getChildren("li").length == 1;

		// Set ID for this list
		if (null == this.options.id && null != this.options.container.get("id"))
			this.options.id = this.options.container.get("id");
		else if (null == this.options.id)
			this.options.id = "NoID";

		this.loadCookie();
		this.bindMenu();
	},

	/*
	Function: loadCookie
		Read cookie which holds the state information for each node
	*/
	loadCookie: function() {
		this.cookie = new Cookie("Foresite.Framework.Script.Mootools.TreeMenu:" + this.options.id);
		this.openIndex = new Array()
		var cookieValue = this.cookie.read();
		if (null != cookieValue && cookieValue.length > 0) {
			var indexs = cookieValue.split(',');
			for (var i = 0; i < indexs.length; i++)
				this.openIndex.push(indexs[i]);
		}
	},

	/*
	Function: bindMenu
		Load menu items
	*/
	bindMenu: function() {
		this.options.container.getElements('span[rel=hook]').each(function(item, index) {
			new TreeMenuItem(this, item, index);
		}, this);
	},

	openIndexContains: function(index) {
		for (var i = 0; i < this.openIndex.length; i++) {
			if (this.openIndex[i] == index)
				return true;
		}

		return false;
	},

	Remove: function(index) {
		for (var i = 0; i < this.openIndex.length; i++) {
			if (this.openIndex[i] == index)
				this.openIndex.splice(i, 1);
		}
		this.cookie.write(this.openIndex);
	},

	Add: function(index) {
		for (var i = 0; i < this.openIndex.length; i++) {
			if (this.openIndex[i] == index)
				return;
		}
		this.openIndex.push(index);
		this.cookie.write(this.openIndex);
	}
});
TreeMenu.implement(new Options, new Events);


/*
Class: TreeMenuItem
	Generic tree menu builder item class. Handles the menu item. Instantiated from <TreeMenu> class.
		
Authors:
	R.Marsh <ryan@idmediaworks.com>
*/

var TreeMenuItem = new Class({
	index: null,
	menu: null,
	hook: null,
	li: null,

	initialize: function(menu, hook, index) {
		this.index = index;
		this.menu = menu;
		this.hook = hook;
		this.li = hook.getParent('li');

		if (this.menu.openIndexContains(this.index) || this.index == 0 && menu.options.expandRootNode)
			this.li.addClass("open");

		if (!this.hasChildren())
			this.li.addClass('inactive');
		this.hook.addEvent('click', this.click.bind(this));
	},

	hasChildren: function() {
		return this.li.getElements('ul').length > 0;
	},

	click: function(e) {
		e = new Event(e);

		if (this.hasChildren()) {
			this.li.toggleClass('open');

			if (this.li.hasClass('open'))
				this.menu.Add(this.index);
			else
				this.menu.Remove(this.index);
		}
	}
});

window.addEvent('domready', function() {
	$$('ul.tree').each(function(elem) {
		new TreeMenu(elem);
	});

	$$('.treeContainer > ul').each(function(elem) {
		elem.addClass('tree');
		new TreeMenu(elem, { id: elem.getParent().get('id') });
	});
});