var currentPopup = null;
window.document.onmousedown = newsMineOnMouseDown;

function clickedOnPopup(evnt) {
	if (currentPopup != null) {
		var xPos = evnt.clientX + document.body.scrollLeft;
		var yPos = evnt.clientY + document.body.scrollTop;
		
		var rect = new MyRectangle(currentPopup);
		//var rect = currentPopup.getBoundingClientRect();
	
		if ((rect.left <= xPos) && (rect.right >= xPos) && (rect.top <= yPos) && (rect.bottom >= yPos)) {
			return true;
		}
	}

	return false;
}

function hidePopup(popup) {
	if (popup == null) {
		popup = currentPopup;
	}
	
	if (popup != null) {
		popup.className = "newsmine-hidden";
		currentPopup = null;
	}
}

function newsMineOnMouseDown() {
     if (!clickedOnPopup(event)) {
          hidePopup();
     }
}

function onPopupTagMouseOver() {
	var srcElement = event.srcElement;
	if (event.srcElement.className != "ig") {
		srcElement = srcElement.parentNode; //a hiliter-yellow <span> could be in the way!
	}
	if (srcElement != currentPopup) {
		if (currentPopup != null) {
			hidePopup(currentPopup);
		}

		var popup = selectPopup(srcElement);
		displayPopup(srcElement, popup);
	}
}

function onPopupMouseOut() {
	if (currentPopup != null) {
		var rect = new MyRectangle(currentPopup);

		var xPos = event.clientX + document.body.scrollLeft;
		var yPos = event.clientY + document.body.scrollTop;
		
		// dont know why but i have to adjust by 5 or so on the left
		if ((rect.left >= (xPos-5)) || (rect.right <= xPos) || (rect.top >= yPos) || (rect.bottom <= yPos)) {
			hidePopup(currentPopup);
		}
	}
}

function popupTagMouseClick() {
     var popupTagElement = event.srcElement;
//     document.location.href = "http://www.lycos.com/srch/?lpv=1&loc=searchhp&query=" + escape(popupTagElement.innerText);
     document.location.href = "/ot_click.asp?loc=newsminepopup-link&id=1&url=/news/search.asp?q=" + escape(popupTagElement.innerText) + "&amp;from=newsmine-word";
}

function selectPopup(srcElement) {
     var integrationIndex = srcElement.getAttribute("ii");
     return document.all("ip" + integrationIndex);
}

function displayPopup(nearElement, popup) {
	// set the img url so we count popups correctly
	var num = popup.num;
	var ord = popup.ord;
	var img = document.all("gc" + num);
	if (img.src.indexOf("ot_gif_counter") == -1) {
		img.src = "/news/forms/ot_gif_counter.asp?gif=newsmine-popup&amp;ord=" + ord;
	}
	
	var clientX = event.clientX + document.body.scrollLeft;
	var clientY = event.clientY + document.body.scrollTop;

	var elementXPos = getRealLeft(nearElement);
	var elementYPos = getRealTop(nearElement);
	var elementHeight = parseInt(nearElement.offsetHeight);

	popup.className = "newsmine-visible";

	//////////////////////////
	// X COORDINATES
	/////////////////////////

	// get the x position for the new div
	var popupXPos = elementXPos;

	// make sure the display isnt to far from the mouse
	var mouseDif = Math.abs(popupXPos - clientX);
	if (mouseDif > 150) {
		popupXPos = clientX;
	}
	
	// calc the bottom position of the box
	var popupRightPos = popupXPos + popup.clientWidth;

	// get the overall window height
	var documentWidth = document.body.clientWidth;

	// if the box goes to far to the right then adjust it
	if (popupRightPos > documentWidth) {
		popupXPos = popupXPos - (popupRightPos - documentWidth) - 15;
	}

	//////////////////////////
	// Y COORDINATES
	/////////////////////////

	var popupYPos = elementYPos + elementHeight + 2;
	
	// calc the bottom position of the box
	var popupBottomPos = popupYPos + popup.clientHeight;

	// get the overall window height
	var documentHeight = document.body.clientHeight + document.body.scrollTop;

	// if the box goes below the window then adjust it
	if (popupBottomPos > documentHeight) {
		popupYPos = elementYPos - popup.clientHeight;
	}
	
	/*
	if (clientY >= popupYPos) {
		popupYPos += elementHeight;
	}
	*/
	
	// make sure the display isnt to far from the mouse
	var mouseDif = Math.abs(popupYPos - clientY);
	if (mouseDif >= elementHeight) {
		//popupYPos -= elementHeight;
	}
		
	popup.style.top = popupYPos;
	popup.style.left = popupXPos;
	currentPopup = popup;
}

// look up offset tree and add up the offsetLefts
function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

// look up offset tree and add up the offsetTops
function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function MyRectangle(el) {
	this.left = getRealLeft(el);
	this.top = getRealTop(el);
	this.right = this.left + el.clientWidth;
	this.bottom = this.top + el.clientHeight;	
}

function turnOnInlineLinking() {
	var tagList = document.body.all.tags('span');
	for (var i=0;i<tagList.length;i++) {
		var tag = tagList[i];
		if (tag.className == 'ig-notvis') {
			tag.className = 'ig';
			tag.onmouseover = onPopupTagMouseOver;
			tag.onclick= popupTagMouseClick;
		}
	}
	
	tag = document.all("inline-linking-msg");
	if (tag != null) {
		//alert(tag.innerText + ',' + tag.href);
		tag.innerText = "off";
		//todo: shouldn't be hard-coded here
		tag.href = "/news/forms/setprefs.asp?newsmine=inline-off";
		//alert(tag.innerText + ',' + tag.href);
	}
}
