/**
 * @class	DropdownMenu
 * @author	Paul Kruijt
 * @author	Marco Troost
 */

var DropdownMenu = new Class({

	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	integer	effect_type
	 * @return	void
	 */

	initialize: function(menu_wrapper_node, timer_interval)
	{
		// settings
		this.timer_interval	= !timer_interval ? 0 : timer_interval;

		// get id's
		this.menu_wrapper	= $(menu_wrapper_node);

		this.menu			= this.menu_wrapper.getElement('ul');


		// set strings
		this.hover_class	= 'cs_hover';
		this.active_class	= 'active';

		this.stored_class	= '';
	},

	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		var _this = this;

		if (this.menu)
		{
			var menu_childs			= this.menu.getChildren('li');
			var menu_childs_total	= menu_childs.length;

			if (menu_childs_total > 0)
			{
				for (i=0; i<menu_childs_total; i++)
				{
					var item_node = menu_childs[i];

					this.toggleSubMenu(item_node);
				}
			}
		}
	},

	/**
	 * toggleSubMenu
	 * @return	void
	 */
	toggleSubMenu: function(submenu_item_node)
	{
		var _this = this;

//		var submenu = submenu_item_node.getElementsByTagName('ul')[0];
		var submenu = submenu_item_node.getElement('ul');

		if(submenu)
		{

			submenu_item_node.addEvent('mouseover', function()
			{				
				// set width of main menu item
				_this.setSubmenuWidth(this);
				
				if (this.className.length > 0)
				{
					_this.stored_class = this.className;
				}

				// show submenu
				this.className	= _this.hover_class;

				// set active anchor
				var anchor_node	= this.getElements('a');
				if (anchor_node) anchor_node.className	= _this.active_class;
			});

			submenu_item_node.onmouseout = function()
			{
				// hide submenu
				this.className = _this.stored_class.length > 0 ? _this.stored_class : '';

				// set in-active anchor
				var anchor_node	= this.getElements('a');
				if (anchor_node) anchor_node.className	= '';

				_this.stored_class = '';
			}

			// check for sub child items
			var submenu_items 		= submenu.getElementsByTagName('li');
			var total_submenu_items	= submenu_items.length;

			if (total_submenu_items)
			{
				for (b=0; b<total_submenu_items; b++)
				{
					var submenu_item = submenu_items[b];

					var subsubmenu = submenu_item.getElements('ul');

					if(subsubmenu)
					{
						submenu_item.onmouseover = function()
						{
							// show submenu
							this.className	= _this.hover_class;

							// set active anchor
							var anchor_node	= this.getElements('a');
							if (anchor_node) anchor_node.className	= _this.active_class;
						}

						submenu_item.onmouseout = function()
						{
							// hide submenu
							this.className = '';

							// set in-active anchor
							var anchor_node	= this.getElements('a');
							if (anchor_node) anchor_node.className	= '';
						}
					}
				}
			}
		}
	},

	/**
	 * setSubmenuWidth
	 * @return	void
	 */
	setSubmenuWidth: function(menu_item_node)
	{
		// set vars
		var _this	= this;

		// get width of main menu item
		var menu_item_coords	= menu_item_node.getCoordinates();

		var menu_item_width		= menu_item_coords.width.toInt();
		var submenu				= menu_item_node.getElement('ul');

		if (submenu && menu_item_width > 0)
		{
			menu_item_width = menu_item_width - 1;
			submenu.setStyle('min-width', menu_item_width+'px');
		}
	}
});
