var ELEMENT_NODE = 1;

Menu = Class.create();
Object.extend(Menu, {
	base: "Categories",
	baseTop: 80,
	baseLeft: 0,
	lastShowed: null,
	timeout: null,
	lastOn: null,

	prepare: function(obj, top, left) {
		obj = obj ? obj : Menu.base;
		top = top ? top : Menu.baseTop;
		left = left ? left : Menu.baseLeft;

		var cat = $(obj);
		Element.setStyle(obj, { "top": top + "px", "left": left + "px"} );

		// tengo q posicionarlo
		for (var i=0; i<cat.childNodes.length; i++) {
			var subcat = cat.childNodes[i];

			if (subcat.nodeType == ELEMENT_NODE) {
				var id = subcat.id.substr(8, subcat.id.length-8);

				var subcats = $("SubCategoriesOf" + id);
				if (subcats) {
					Menu.prepare(subcats, top + subcat.offsetHeight, left + 1 + cat.offsetWidth);
				}
			}
		}
	},

	showMain: function() {
		$(Menu.base).className = $(Menu.base).className.replaceAll("Invisible", "");
	},

	showSubCategories: function(id, obj) {
		if (Menu.lastShowed) {
			Menu.lastShowed.className += " Invisible";
			Menu.lastShowed = null;
			Menu.lastOn.className = "";
			Menu.lastOn = null;
		}

		if (id) {
			$("SubCategoriesOf" + id).className = $("SubCategoriesOf" + id).className.replaceAll("Invisible", "");
			Menu.lastShowed = $("SubCategoriesOf" + id);
			Menu.lastOn = obj;
			Menu.lastOn.className = "On";
			Menu.startCount();
		}
	},

	hideAll: function() {
		if (Menu.lastShowed) {
			Menu.lastShowed.className += " Invisible";
			Menu.lastShowed = null;
			Menu.lastOn.className = "";
			Menu.lastOn = null;
		}

		$(Menu.base).className += " Invisible";
	},

	startCount: function() {
		if (Menu.timeout) {
			clearTimeout(Menu.timeout);
		}
		Menu.timeout = setTimeout(function() { Menu.hideAll(); }, 5000);
	}
});