function Menu(el, increment) {
	this.el = el;
	if(!this.el) return;
	this.firstLink = this.el.getElementsByTagName('a')[0];
	this.div = this.el.getElementsByTagName('div')[0];
	if(!this.firstLink || !this.div) return;
	this.increment = increment;
	this.div.style.top = this.firstLink.offsetHeight - this.div.offsetHeight + 'px';
	this.end = 0;
	this.bind();
}

Menu.prototype.bind = function() {
	var o = this;
	this.el.onmouseover = function() {
		o.end = o.firstLink.offsetHeight;
		o.slide();
	}
	this.el.onmouseout = function(e) {
		var mPos = mouse_position(e);
		var elPos = element_position(o.div);
		var linkTop = element_position(o.firstLink)[1];
		if ((mPos[0] > elPos[0] && mPos[0] < (elPos[0] + this.offsetWidth)) && (mPos[1] > linkTop && mPos[1] < (elPos[1] + o.div.offsetHeight))) return;
		o.end = o.firstLink.offsetHeight - o.div.offsetHeight;
		o.slide();
	}
}

Menu.prototype.slide = function() {
	if (this.state == 'hide') return;
	var interval = window.setInterval(tween, 30);
	var o = this;
	function tween() {
		window.clearInterval(interval);
		var current = o.div.offsetTop;
		if(current == o.end) return;
		if (current < o.end) {
			var next = current + o.increment;
			if(next > o.end) next = o.end;
		} else {
			var next = current - o.increment;
			if(next < o.end) next = o.end;
		}
		o.div.style.top = next + 'px';
		o.slide();
	}
}

function buildMenus(target) {
	var el = document.getElementById(target);
	if (!el) return;
	var menus = el.getElementsByTagName('DT');
	var increment = 20;
	for (var i=0; i<menus.length; i++) {
		var menu = new Menu(menus[i], increment);
	}
}

function mouse_position(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return [posx, posy];
}

function element_position(o) {
	var x = y = 0;
	if (o.offsetParent) {
		x = o.offsetLeft;
		y = o.offsetTop;
		while (o = o.offsetParent) {
			x += o.offsetLeft;
			y += o.offsetTop;
		}
	}
	return [x,y];
}

window.onload = function(){buildMenus('dd_menu');};