var timeout_id = 0;
var pop_delay = 300;
var hide_delay = 600;

function find_pos (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function popmenu (pId, pAnchorId, pParent, pPosition) {
	if (pId != undefined) {
		this.cElement = document.getElementById(pId);
		this.cElement.style.visibility = 'hidden';
		this.cElement.style.display = 'none';	
	} else {
		this.cElement = null;
	}
	if (pAnchorId != undefined) {
		this.cAnchor = document.getElementById(pAnchorId);
	} else {
		this.cAnchor = null;
	}
	if (pParent != undefined) {
		this.cParent = pParent;
		this.cParent.cChildren[this.cParent.cChildren.length] = this;	
	} else {
		this.cParent = null;
	}
	if (pPosition != undefined) {
		this.cPosition = pPosition;
	} else {
		this.cPosition = 'below';
	}
	this.cChildren = Array();
	this.cVisible = false;
	if (pId != undefined) {
		this.cName = pId;
	} else {
		this.cName = 'menu_parent';
	}
	
	this.pop = pop;
	this.unpop = unpop;
	this.show = show;
	this.hide = hide;
	this.hide_children = hide_children;
	this.over = over;
	this.out = out;
	this.timeout = timeout;
	this.timein = timein;
}

function pop () {
	this.over();
	if (pop_delay > 0) {
		if (timeout_id != 0) {
			clearTimeout(timeout_id);
		}
		timeout_id = setTimeout(this.cName + '.timein()', pop_delay);
	} else {
		this.timein();
	}
}

function unpop () {
	if (pop_delay > 0) {
		if (timeout_id != 0) {
			clearTimeout(timeout_id);
		}
		timeout_id = setTimeout(this.cName + '.hide_children()', pop_delay);
	} else {
		this.hide_children();
	}
}

function show () {
	this.cParent.hide_children();
	this.cElement.style.display = 'block';
	switch (this.cPosition) {
		case 'above':
		case 'above-left':
			h = -this.cElement.offsetHeight;
			w = 0;
			break;
		case 'above-center':
			h = -this.cElement.offsetHeight;
			w = (this.cAnchor.offsetWidth - this.cAnchor.offsetWidth) / 2;
			break;
		case 'above-right':
			h = -this.cElement.offsetHeight;
			w = this.cAnchor.offsetWidth - this.cAnchor.offsetWidth;
			break;
		case 'below':
		case 'below-left':
			h = this.cAnchor.offsetHeight;
			w = 0;
			break;
		case 'below-center':
			h = this.cAnchor.offsetHeight;
			w = (this.cAnchor.offsetWidth - this.cElement.offsetWidth) / 2;
			break;
		case 'below-right':
			h = this.cAnchor.offsetHeight;
			w = this.cAnchor.offsetWidth - cElement.offsetWidth;
			break;
		case 'left':
		case 'left-top':
			h = 0;
			w = -this.cElement.offsetWidth;
			break;
		case 'left-middle':
			h = (this.cAnchor.offsetHeight - this.cElement.offsetHeight) / 2;
			w = -this.cElement.offsetWidth;
			break;
		case 'left-bottom':
			h = this.cAnchor.offsetHeight - this.cElement.offsetHeight;
			w = -this.cElement.offsetWidth;
			break;
		case 'right':
		case 'right-top':
			h = 0;
			w = this.cAnchor.offsetWidth;
			break;
		case 'right-middle':
			h = (this.cAnchor.offsetHeight - this.cElement.offsetHeight) / 2;
			w = this.cAnchor.offsetWidth;
			break;
		case 'right-bottom':
			h = this.cAnchor.offsetHeight - this.cElement.offsetHeight;
			w = this.cAnchor.offsetWidth;
			break;                       
		default:
			h = this.cAnchor.offsetHeight;
			w = 0;
	}
	this.cElement.style.position = 'absolute';
	p = find_pos(this.cAnchor);
	this.cElement.style.left = (p[0] + w) + 'px';
	this.cElement.style.top = (p[1] + h) + 'px';
	this.cElement.style.visibility = 'visible';        
}

function hide () {
	if (this.cChildren.length > 0) {
		this.hide_children();
	}
	if (this.cElement != null) {
		this.cElement.style.visibility = 'hidden';
		this.cElement.style.display = 'none';
	}
}

function hide_children() {
	for (var i = 0; i < this.cChildren.length; i++) {
		this.cChildren[i].hide();
	}   
}

function over () {
	this.cVisible = true;
	var lCurrent = this.cParent;
	while (lCurrent != null) {
		lCurrent.cVisible = true;		
		lCurrent = lCurrent.cParent;
	}
}

function out () {
	this.cVisible = false;
	var lCurrent = this.cParent;
	var lTop = this;
	while (lCurrent != null) {
		lCurrent.cVisible = false;
		lTop = lCurrent;
		lCurrent = lCurrent.cParent;
	}	
	if (hide_delay > 0) {
		if (timeout_id != 0) {
			clearTimeout(timeout_id);
		}
		timeout_id = setTimeout(lTop.cName + '.timeout()', hide_delay);
	} else {
		lTop.timeout();
	}
}

function timein () {
	if (this.cVisible && this.cElement != null) {
		if (this.cElement.style.visibility != 'visible') {
			this.show();
		}
	}
}

function timeout () {
	if (!this.cVisible) {
		this.hide();
	}
}
