﻿/**
* @copyright (C) 2006 Joomla-addons.org
* @author Websmurf
* 
* --------------------------------------------------------------------------------
* All rights reserved.  Easy FAQ Component for Joomla!
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
* --------------------------------------------------------------------------------
*
* Parts of this code has been shamelessly borrowed from the joomla 1.5. codebase
* Original code by Johan Janssens, Louis Landry, David Gal and others
**/

/**
 * JMenu javascript behavior
 *
 * @author Johan Janssens <johan.janssens@joomla.org>
 * @package	Joomla
 * @since	1.5
 * @version 1.0
 */
  
/* -------------------------------------------- */
/* -- JMenu prototype ------------------------- */
/* -------------------------------------------- */

//constructor
var JMenu = function() { this.constructor.apply(this, arguments);}
JMenu.prototype = {
	constructor: function(element) 
	{	
		var elements = element.getElementsByTagName("LI");
		var nested = null
		for (var i=0; i<elements.length; i++) 
		{
			var element = elements[i];
			
			this.registerEvent(element, 'mouseover');
			this.registerEvent(element, 'mouseout');
			
			//find nested UL
			for (j=0; j < element.childNodes.length; j++) {
				if (element.childNodes[j].nodeName == "UL")  {	
					nested = element.childNodes[j]
				}
			}
			
			if(nested == null) 
				return;
			
			//declare width
			var offsetWidth  = 0;
			
			//find longest child
			for (k=0; k < nested.childNodes.length; k++) {
				var node  = nested.childNodes[k]
				if (node.nodeName == "LI") 
					offsetWidth = (offsetWidth >= node.offsetWidth) ? offsetWidth :  node.offsetWidth;
			}
			
			//match longest child
			for (l=0; l < nested.childNodes.length; l++) {
				var node = nested.childNodes[l]
				if (node.nodeName == "LI") {
					node.style.width = offsetWidth+'px';
				}
			}
			
			nested.style.width = offsetWidth+'px';
		}
	},
	
	onmouseover: function(event, args)  {
		this.addClassName(event.element, 'hover');
	},

	onmouseout: function(event, args)  {
		this.removeClassName(event.element, 'hover');
	},
	
	registerEvent: function(target,type,args) 
	{
		//use a closure to keep scope
		var self = this;
			
		if (target.addEventListener)   { 
    		target.addEventListener(type,onEvent,true);
		} else if (target.attachEvent) { 
	  		target.attachEvent('on'+type,onEvent);
		} 
		
		function onEvent(e)	{
			e = e||window.event;
			e.element = target;
			return self["on"+type](e, args);
		}
	},
	
	addClassName: function(element, className) {
		this.removeClassName(element, className); 
		element.className+=(element.className.length>0?' ':'')+className; 
	},
  
	removeClassName: function(element, className) {
		element.className=element.className.replace(new RegExp("^"+className+"\\b\\s*|\\s*\\b"+className+"\\b",'g'),''); 
	}
}