var JMenu = new Class({
	
	getOptions: function(){
		return {offsetWidth: 0, newStyle: 'hover'};
	},
	initialize: function(el,options){
		var thisObj = this;
		this.setOptions( this.getOptions(), options);
		this.elements = $ES('li', el);
		this.nested = null;
		var offsetWidth = this.options.offsetWidth;
		this.newStyle = this.options.newStyle;
		for (var i=0; i<this.elements.length; i++){	
			var element = this.elements[i];
			element.addEvent('mouseover', function(e,elm){ if(this.className!='seleccionado'){
														 	this.addClass(thisObj.newStyle);
														 }
													});
			element.addEvent('mouseout', function(){ this.removeClass(thisObj.newStyle); });
			element.addEvent('click', function(){
				var children = this.getChildren();	
				if(children.length==2){
					ch=children[0];
				}else{
					ch = children;
				}
				a = ch.getChildren();	
				if(a!=''){
					window.location.href = a;
				}else{
					
				   return false;
				}
			});

			//find nested UL
			nested = $E('ul', element);
			if(!nested) {continue;}
			nested.setStyle('left',offsetWidth+'px');
			nested.setStyle('top','0px');
			//find longest child
			if(offsetWidth==0){
				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).setStyle('width', $(node).parentNode.offsetWidth+'px');
					$(node).setStyle('left','0px');
					$(node).setStyle('top','0px');
				}
			}

			$(nested).setStyle('width', offsetWidth+'px');
		}
	
	}
});
JMenu.implement(new Options);
