var bblTempX = 0;
var bblTempY = 0;

var isIE = document.all?true:false;
if (!isIE) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.CLICK)

document.onmousemove = getMouseXY;
document.onmousedown = bblClose;

var bblID = 'msgBubble';
var isBubbleOpen = false;

function bblClose() {
	xPosBBL = parseInt(bblTempX,10);
	yPosBBL = parseInt(bblTempY,10);
	
	if (isBubbleOpen) {
		var bblLeft = parseInt(document.getElementById(bblID).offsetLeft,10);
		var bblWidth = parseInt(document.getElementById(bblID).offsetWidth,10);
		
		var bblTop = parseInt(document.getElementById(bblID).offsetTop,10);
		var bblHeight = parseInt(document.getElementById(bblID).offsetHeight,10);
		
		if (xPosBBL >= bblLeft && xPosBBL <= (bblLeft + bblWidth) && yPosBBL >= bblTop && yPosBBL <= (bblTop + bblHeight)) {
			//leave pop-up open
		}
		else {
			bblHide();
		}
	}
}

function getMouseXY(e) {
	if (isIE) {
		if (document.body && event) {
			// grab the x-y pos.s if browser is IE
			bblTempX = event.clientX + document.body.scrollLeft;
			bblTempY = event.clientY + document.body.scrollTop;
		}
	}
	else {
		// grab the x-y pos.s if browser is NS
		bblTempX = e.pageX;
		bblTempY = e.pageY;
	}

	if (bblTempX < 0) { bblTempX = 0; }
	if (bblTempY < 0) { bblTempY = 0; }
	
	return true;
}

function bblHide() {
	if (isBubbleOpen) {
		document.getElementById(bblID).className = 'hideMe';
		isBubbleOpen = false;
	}
}

function bblShow(msgHTML, isPop, wPop, obj) {
	if (obj) {
		xPosBBL = getX(obj.id) + (obj.offsetWidth/2);
		yPosBBL = getY(obj.id)+15;
	}
	else {
		xPosBBL = parseInt(bblTempX,10);
		yPosBBL = parseInt(bblTempY,10);
	}

	if (isPop == 1) {
		bblID = 'msgBubblePop';
	}	

	if (document.getElementById(bblID)) {
		if (wPop == null || isNaN(wPop)) {
			//keep original width
		}
		else {
			document.getElementById(bblID).style.width = wPop +'px';
		}
		
		document.getElementById(bblID +'Inner').innerHTML = msgHTML;
		document.getElementById(bblID).className = 'showMe';
		
		isBubbleOpen = true;

		var bblW = parseInt(document.getElementById(bblID).scrollWidth,10);
		var bblH = parseInt(document.getElementById(bblID).scrollHeight,10);
		
		document.getElementById(bblID).style.top = (yPosBBL - (bblH + 10)) +'px';
		document.getElementById(bblID).style.left = (xPosBBL - (bblW / 2)) +'px';

		document.getElementById(bblID +'Arrow').style.left = ((bblW/2) - 45) +'px';
	}
	
	return false;
}
