var MenuHorizontal = (function(){
	var self = {};

	var show = function() {
		var root = this;

		if( root.isRunShow ) {
			return false;
		}

		clearTimeout( this.timerHideChild );
		this.timerHideChild = null;

		root.isRunShow = true;
		root.isRunHide = false;

		if( root.isShow ) {
			return false;
		}

		this.childMenu.style.display = "block";

		var match = root.className.match(/\bcurrent\b/i);
		if( !match || match.length < 1 ) {
			root.className += " current";
		}

		jTweener.removeTween(this.childMenu);
		jTweener.addTween(this.childMenu, {
			width: this.childMenu.scrollWidth,
			height: this.childMenu.scrollHeight,
			time: 0.4,
			onComplete: function(){
				root.isRunShow = false;
				root.isShow = true;
			}
		});
	}

	var hide = function() {
		var root = this,
			child = this.childMenu;

		root.isShow = false;

		jTweener.removeTween(child);
		jTweener.addTween(child, {
			width: this.scrollWidth,
			height: 1,
			time: 0.2,
			onComplete: function(){
				root.className = root.className.replace(" current", "");
				child.style.display = "none";
				root.isRunHide = false;
			}
		});
	}

	window.onload = function(){
		var root = _("#menu .root"),
			i = 0,
			len = root.length;

		for(; i < len ; i++ ) {
			var child = _("div.child", root[i]);
			if( child.length > 0 ) {
				var childElement = _("*", root[i]),
					j = 0, _root = root[i],
					lenChilds = childElement.length;

				for( j=0; j < lenChilds ; j++ ) {
					childElement[j].onmouseover = function(){
						if( _root.isRunShow ) {
							return false;
						}

						show.call(_root);
					};

					childElement[j].onmouseout = function(){
						if( _root.timerHideChild ) {
							return false;
						}

						if( _root.isRunHide ) {
							return false;
						}

						_root.isRunShow = false;
						_root.isRunHide = true;

						_root.timerHideChild = setTimeout( function(){
							hide.call(_root);
						}, 30);
					};
				}

				root[i].childMenu = child[0];
				root[i].childMenu.style.width = root[i].clientWidth + "px";
				root[i].childMenu.style.height = "1px";
			}
		}
	};

	return self;
})();
