/*!
	Manages mutually exclusive popups
	Draws a straight line from the element to the popup

	\param lineName	ID of the line drawn
	*/
function PopupManager(lineID) {
	this.lastPopup = 0;
	this.lineDiv = document.getElementById(lineID);
}

/*!
	Opens a popup aligned to the given element
	\param el				The element to align to
	\param divName	The name of the popup element
	\param dirRight	Whether the popup faces right/left of the element
	\param dirDown	Whether the popup faces up/down of the element
	*/
PopupManager.prototype.open = function(el, divName, dirRight, dirDown) {
	if(this.lastPopup != 0) {
		this.lastPopup.style.visibility = 'hidden';
	}
	var div = document.getElementById(divName);
	var pos = this.getAbsolutePos(el);
	div.style.visibility = this.lineDiv.style.visibility = 'visible';

	if(dirRight) {
		pos.x += el.offsetWidth+1;
		this.lineDiv.style.left = pos.x+'px';
		pos.x += this.lineDiv.offsetWidth;
		div.style.left = pos.x +'px';
	}
	else {
		pos.x -= this.lineDiv.offsetWidth;
		this.lineDiv.style.left = pos.x+'px';
		div.style.left = pos.x - div.offsetWidth+'px';
	}

	pos.y += el.offsetHeight/2;
	this.lineDiv.style.top = pos.y+'px';
	div.style.top = (dirDown)? pos.y+'px' : pos.y-div.offsetHeight+1+'px';

	
	this.lastPopup = div;
}

PopupManager.prototype.close = function() {
	if(this.lastPopup != 0) {
		this.lastPopup.style.visibility = this.lineDiv.style.visibility = 'hidden';
		this.lastPopup = 0;
	}
}

PopupManager.prototype.getAbsolutePos = function(el) {
	var SL = 0, ST = 0;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}