// Bluora Behaviour Extension
// @name: ListMenu
// @author: Rocco Howard
// @version: 20070630

Bluora.DIVBluoraListMenuCreate = function(el) {
	
	el.menuRoot = $(el.id+'_ul');
	
	if (el.menuRoot.settings != '' && el.menuRoot.title == '') {
		el.menuRoot.title = el.menuRoot.settings;
	}
	
	menu_settings = el.menuRoot.title.split('|');
	
	type_of_action = menu_settings[0];
	menu_timeout = menu_settings[1];
	
	if (menu_settings.length == 1) {
		type_of_action = 'mouseover';
		menu_timeout = 50;
	}
	el.menuRoot.settings = el.menuRoot.title;
	el.menuRoot.title = '';
	
	var count = 1;
	var lists = el.menuRoot.getElementsByTagName('ul');
	for (var i = 0; i < lists.length; i++) {
	
		// Find a parent LI node, if any, by recursing upwards from the UL. Quit if not found.
		li = ul = lists[i];
		while (li) {
			if (li.nodeName.toLowerCase() == 'li') break;
			li = li.parentNode;
		}
		if (!li) continue;
		// Next, find the parent UL to that LI node.
		parUL = li;
		while (parUL) {
			if (parUL.nodeName.toLowerCase() == 'ul') break;
			parUL = parUL.parentNode;
		}
		
		// Now, find the anchor tag that triggers this menu; it should be a child of the LI.
		a = null;
		for (var j = 0; j < li.childNodes.length; j++) {
			
			if (li.childNodes[j].nodeName.toLowerCase() == 'a') {
				a = li.childNodes[j];
			}
			if (!a) continue;
			if (typeof a.menu_created == 'undefined') {
				// We've found a menu node by now, so give it an ID and event handlers.
				var menuID = el.menuRoot.id + '_id_' + (count++);
				// Only assign a new ID if it doesn't have one already.
				if (ul.id) {
					menuID = ul.id;
				} else {
					ul.setAttribute('id', menuID);
				}
				a.menu_created = true;
				//Bluora.log(ul.id);
				a.activates_menu = menuID;
				
				$(menuID).defzIndex = 1000;
				$(menuID).style.zIndex = 0;
				$(menuID).hide();
				$(menuID).timeout = 0;
				
				action_open = function() {
					//Bluora.log('MenuOpen');
					if (typeof this.activates_menu != ''
						&& typeof Bluora == 'object') {
						
						var activates_menu_item = document.getElementById(this.activates_menu);
						clearTimeout(activates_menu_item.timeout);
						activates_menu_item.style.zIndex = document.getElementById(this.activates_menu).defzIndex;
						activates_menu_item.timeout = setTimeout('document.getElementById("'+this.activates_menu+'").show()', 50);
					}
				};
				
				if (type_of_action == 'mouseover') {
					a.onmouseover = action_open;
				} else if (type_of_action == 'mouseclick') {
					a.onclick = action_open;
				}
				a.onfocus = action_open;
				
				ul.onmouseover = function() {
					//Bluora.log('UL MouseOver');
					clearTimeout(this.timeout);
					this.style.zIndex = this.defzIndex;
					this.timeout = setTimeout('document.getElementById("'+this.id+'").show()', 150);
				};
				
				ul.onfocus = ul.onmouseover;
				
				a.onmouseout = function() {
					//Bluora.log('A MouseOut');
					if (typeof this.activates_menu != ''
						&& typeof Bluora == 'object') {
						
						//var activates_menu_item = document.getElementById(this.activates_menu);
						//clearTimeout(activates_menu_item.timeout);
						//activates_menu_item.style.zIndex = 0;
						document.getElementById(this.activates_menu).timeout = setTimeout('document.getElementById("'+this.activates_menu+'").hide();clearTimeout(document.getElementById("'+this.activates_menu+'").timeout);document.getElementById("'+this.activates_menu+'").zIndex=0;', 150);
					}
				};
				
				a.onblur = a.onmouseout;
				
				ul.onmouseout = function() {
					//Bluora.log('UL MouseOut');
					if (typeof this.timeout != 'undefined') {
						clearTimeout(this.timeout);
					}
					this.style.zIndex = 0;
					this.timeout = setTimeout('document.getElementById("'+this.id+'").hide()', menu_timeout);
				};
				
				ul.onblur = ul.onmouseout;
			}
		}
	}
}

Bluora.Behaviour.ListMenu = {
	'.BluoraListMenu': function(el) {
		Bluora.DIVBluoraListMenuCreate(el);
	}
}
Bluora.Behaviour.register(Bluora.Behaviour.ListMenu);
Bluora.Behaviour.apply();