var pop_delay = 0;
var hide_delay = 300;

var timeout_id = 0;
var grey_out_count = 0;

var direct_descendant = false;
var positioned_parent = false;
var offset_x = 0;
var offset_y = 0;

// Position-finding functions.

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 get_viewport_width () {
// Should really be using self.innerHeight/self.innerWidth as a first choice, but Mozilla/Firefox has a 
// bug where innerHeight/innerWidth returns viewport dimensions *including* scrollbar, when the intended
// behaviour is viewport dimensions *excluding* scrollbar.
/*	if (self.innerWidth) {
		w = self.innerWidth;
	} else */ if (document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
	} else if (document.body) {
		w = document.body.clientWidth;
	} else {
		w = -1;
	}
	return w;
}

function get_viewport_height () {
// Should really be using self.innerHeight/self.innerWidth as a first choice, but Mozilla/Firefox has a 
// bug where innerHeight/innerWidth returns viewport dimensions *including* scrollbar, when the intended
// behaviour is viewport dimensions *excluding* scrollbar.
/*	if (self.innerHeight) {
		h = self.innerHeight;
	} else */ if (document.documentElement && document.documentElement.clientHeight) {
		h = document.documentElement.clientHeight;
	} else if (document.body) {
		h = document.body.clientHeight;
	} else {
		h = -1;
	}
	return h;
}

function get_document_width () {
	if (document.documentElement && document.documentElement.scrollWidth) {
		w = document.documentElement.scrollWidth;
	} else if (document.body && document.body.scrollWidth) {
		w = document.body.scrollWidth;
	} else if (document.body && document.body.offsetWidth) {
		w = document.body.offsetWidth;
	} else {
		w = -1;
	}
	return w;
}

function get_document_height () {
	if (document.documentElement && document.documentElement.scrollHeight) {
		h = document.documentElement.scrollHeight;
	} else if (document.body && document.body.scrollHeight) {
		h = document.body.scrollHeight;
	} else if (document.body && document.body.offsetHeight) {
		h = document.body.offsetHeight;
	} else {
		h = -1;
	}
	return h;
}

function get_scroll_top () {
	if (document.documentElement && document.documentElement.scrollTop) {
		t = document.documentElement.scrollTop;
	} else if (document.body) {
		t = document.body.scrollTop;
	} else {
		t = 0;
	}
	return t;
}

function get_scroll_left () {
	if (document.documentElement && document.documentElement.scrollLeft) {
		l = document.documentElement.scrollLeft;
	} else if (document.body) {
		l = document.body.scrollLeft;
	} else {
		l = 0;
	}
	return l;
}

function fix_position(obj) {
	var x, y;
	x = (get_viewport_width() - obj.cElement.offsetWidth) / 2;
	y = (get_viewport_height() - obj.cElement.offsetHeight) / 2;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		x += get_scroll_left();
		y += get_scroll_top();
	}
	obj.cElement.style.left = x + 'px';
	obj.cElement.style.top = y + 'px';
	obj.cFixId = setTimeout('fix_position(' + obj.cName + ')', 100);
}

function popmenu (pId, pAnchorId, pParent, pPosition, pGreyOut) {
	if (typeof(pId) != 'undefined') {
		this.cElement = document.getElementById(pId);
		this.cElement.style.visibility = 'hidden';
		this.cElement.style.display = 'none';	
	} else {
		this.cElement = null;
	}
	if (typeof(pAnchorId) != 'undefined') {
		this.cAnchor = document.getElementById(pAnchorId);
	} else {
		this.cAnchor = null;
	}
	if (typeof(pParent) != 'undefined') {
		this.cParent = pParent;
		this.cParent.cChildren[this.cParent.cChildren.length] = this;	
	} else {
		this.cParent = null;
	}
	if (typeof(pPosition) != 'undefined') {
		this.cPosition = pPosition;
	} else {
		this.cPosition = 'below';
	}
	if (typeof(pGreyOut) != 'undefined') {
		this.cGreyOut = pGreyOut;
	} else {
		this.cGreyOut = false;
	}

	this.cChildren = Array();
	this.cVisible = false;
	if (typeof(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;

	this.cFixId = 0;
}

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 - this.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;
		case 'screen-center':
			h = (get_viewport_height() - this.cElement.offsetHeight) / 2;
			w = (get_viewport_width() - this.cElement.offsetWidth) / 2;
			if (navigator.appName == 'Microsoft Internet Explorer') {
				h += get_scroll_top();
				w += get_scroll_left();
			}
			break;
		default:
			h = this.cAnchor.offsetHeight;
			w = 0;
	}

	if (this.cPosition == 'screen-center') {
		if (navigator.appName == 'Microsoft Internet Explorer') {
			this.cElement.style.position = 'absolute';
		} else {
			this.cElement.style.position = 'fixed';
		}
		p = [0,0];
	} else {
		this.cElement.style.position = 'absolute';
		if (direct_descendant) {
			p = [this.cAnchor.offsetLeft,this.cAnchor.offsetTop];
		} else {
			p = find_pos(this.cAnchor);
			if (positioned_parent) {
				n = find_pos(this.cElement.offsetParent);
				p[0] -= n[0];
				p[1] -= n[1];
			}
		}
	}

	this.cElement.style.left = (p[0] + w + offset_x) + 'px';
	this.cElement.style.top = (p[1] + h + offset_y) + 'px';
	this.cElement.style.visibility = 'visible';
	if (this.cPosition == 'screen-center') {
		fix_position(this);
	}
	if (this.cGreyOut) {
		this.cElement.style.zIndex = 100;
		grey_out(true);
		grey_out_count++;
	}
}

function hide () {
	if (this.cChildren.length > 0) {
		this.hide_children();
	}
	if (this.cElement != null) {
		this.cElement.style.visibility = 'hidden';
		this.cElement.style.display = 'none';
	}
	if (this.cFixId != 0) {
		clearTimeout(this.cFixId);
	}
	if (this.cGreyOut) {
		if (grey_out_count > 0) {
			grey_out_count--;
		}
		if (grey_out_count == 0) {
			grey_out(false);
		}
	}
}

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();
	}
}

function grey_out(pShow) {
	var lDark, lBody, lNode, lHeight, lWidth;

	lDark = document.getElementById('darkenScreenObject');
	if (!lDark) {
		lBody = document.getElementsByTagName("body")[0];
		lNode = document.createElement('div');
		lNode.style.position = 'absolute';
		lNode.style.top = '0px';
		lNode.style.left = '0px';
		lNode.style.overflow = 'hidden';
		lNode.style.display = 'none';
		lNode.style.opacity = .70;
		lNode.style.mozOpacity = .70;
		lNode.style.filter = 'alpha(opacity=70)';
		lNode.style.zIndex = 50;
		lNode.style.backgroundColor = '#000000';
		lNode.id = 'darkenScreenObject';
		lBody.appendChild(lNode);
		lDark = document.getElementById('darkenScreenObject');
	}

	if (pShow) {
		lDark.style.display = 'none';
		lHeight = get_document_height();
		if (lHeight == -1) {
			lDark.style.height = '100%';
		} else {
			lDark.style.height = lHeight + 'px';
		}
		lWidth = get_document_width();
		if (lWidth == -1) {
			lDark.style.width = '100%';
		} else {
			lDark.style.width = lWidth + 'px';
		}
		lDark.style.display = 'block';
	} else {
		lDark.style.display = 'none';
	}
}
