window.addEvent('load', function()
{
	new barnl_topmenu();
}); 

var barnl_topmenu = new Class({
		options: {
			marr_slides: null
		},
		initialize: function()
		{
				this.marr_slides = Array();

				//we find the ul element of our sub_elements
				var arr_elements = $$('td.top_menu_subelements');

				//we loop in these elements:
				for(var i = 0; i < arr_elements.length; i++)
				{
					//we find the next ul element that is a child of our current element:
					var obj_child = this.get_next_element_child(arr_elements[i], 'ul');
					if (obj_child == null)
						return;

					this.marr_slides[this.marr_slides.length] = new menu_slide(arr_elements[i], obj_child);
				}
		},
		get_next_element_child: function (_obj_element, _s_element_tag)
		{
				var arr_children = _obj_element.getChildren();
				for(var i = 0; i < arr_children.length; i++)
				{
					if (arr_children[i].getTag() == _s_element_tag)
						return arr_children[i];
				}

				return null;
		}
});

var menu_slide = new Class({
		options: {
				mobj_slide: null,
				mobj_child: null,
				ms_is_down: false
		},
		initialize: function(_obj_trigger, _obj_child)
		{
				this.mobj_child = _obj_child;

				//we set the width of our child the same as our width:
				var i_trigger_width = _obj_trigger.getStyle('width').toInt();
				this.mobj_child.setStyle('width', i_trigger_width);

				//we set a random id to our child:
				var irand = Math.floor(Math.random()* 1000000001);
				_obj_child.setProperty('id', irand);

				//we create the slide and we hide it:
				var obj_this = this;
				this.mobj_slide = new Fx.Slide(_obj_child.getProperty('id'), {wait: false});
				this.mobj_slide.hide();

				//we set the events:
				var obj = this;
				_obj_trigger.addEvent('mouseenter', function(e)
				{ e = new Event(e); obj.slidein(); e.stop(); });

				_obj_trigger.addEvent('mouseleave', function(e)
				{ e = new Event(e); obj.slideout(); e.stop(); });
			
		},
		slidein: function()
		{
				//we make sure the child is a block:
				this.mobj_child.setStyle('display', 'block');

				//we slide in:
				this.mobj_slide.slideIn();

				//we are goiing down:
				this.ms_is_down = true;
		},
		slideout: function()
		{
				//we make sure the child is a block:
				this.mobj_child.setStyle('display', 'block');

				//we slide out:
				this.mobj_slide.slideOut();

				//we are goiing up:
				this.ms_is_down = false;
		}
});
