﻿$(function(){
	$.namespace("menu", {
		__menu:null,
		load: function (containerId) {
			var $this = $(this)[0];
			__menu = $(containerId);
			__menu.children("li").each(function(i, t){
				$(t).children("ul").each(function(j, subMenu){
					subMenu.style.display = "block";
					$this.addFlyoutMenu($(subMenu), $(t));
				});
			});
			__menu.children("li").last().addClass("last-child");
		},
		addFlyoutMenu:function(subMenu, owner){
			var $this = $(this)[0];
			subMenu.children("li").each(function(i, t){
				$(t).children("ul").each(function(j, subSubMenu){
					$this.addFlyoutMenu($(subSubMenu), $(t));
				});
			});
			owner.addClass("hasSubMenus");
			switch(subMenu[0].getAttribute("direction"))
			{
				case "down":
					subMenu.css({
						top:owner[0].offsetHeight + "px",
						left:"0px"
					});
					break;
				case "right":
					subMenu.css({
						top:"-3px",
						left:owner[0].offsetWidth + "px"
					});
					
					break;
			}
			subMenu[0].style.display = "none";
			owner.hover(function(){subMenu[0].style.display = "block"}, function(){subMenu[0].style.display = "none"});
		}
	});
});
$(function(){$.menu.load("#main-menu");});
