var containerHeight;
var containerWidth;
var viewPortHeight;
var viewPortWidth;
var mousePositionTop;
var mousePositionLeft;

function findPopupPointerPosition(originalPointerPos, popupContainerId, event)
{
    var theMousePosition = new MousePosition();
    theMousePosition.getMouseOverPosition(event);

    containerHeight = jQuery(popupContainerId).height();
    containerWidth = jQuery(popupContainerId).width();
	viewPortHeight = document.documentElement.clientHeight;
	viewPortWidth = document.documentElement.clientWidth;
	mousePositionTop = event.clientY;
	mousePositionLeft = event.clientX;
	var displayString = "Container Height:" + containerHeight + "\nContainer Width:" + containerWidth;
	displayString += "\nBrowser Height:" + viewPortHeight + "\nBrowser Width:" + viewPortWidth;
	displayString += "\nMouse Top Position:" + mousePositionTop + "\nMouse Left Position:" + mousePositionLeft;
	//alert(displayString);

	if (originalPointerPos == "topLeft")
	{
		if (isCrossingTopOfWindow() && !isCrossingBottomOfWindow())
		{
			if (isCrossingLeftOfWindow() && !isCrossingRightOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-right");
				return {vAlign:"bottom", hAlign:"right"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-left");
				return {vAlign:"bottom", hAlign:"left"};
			}
		}
		else
		{
			if (isCrossingLeftOfWindow() && !isCrossingRightOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-right");
				return {vAlign:"top", hAlign:"right"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-left");
				return {vAlign:"top", hAlign:"left"};
			}
		}
	}
	else if (originalPointerPos == "topRight")
	{
		if (isCrossingTopOfWindow() && !isCrossingBottomOfWindow())
		{
			if (isCrossingRightOfWindow() && !isCrossingLeftOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-left");
				return {vAlign:"bottom", hAlign:"left"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-right");
				return {vAlign:"bottom", hAlign:"right"};
			}
		}
		else
		{
			if (isCrossingRightOfWindow() && !isCrossingLeftOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-left");
				return {vAlign:"top", hAlign:"left"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-right");
				return {vAlign:"top", hAlign:"right"};
			}
		}
	}
	else if (originalPointerPos == "bottomLeft")
	{
		if (isCrossingBottomOfWindow() && !isCrossingTopOfWindow())
		{
			if (isCrossingLeftOfWindow() && !isCrossingRightOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-right");
				return {vAlign:"top", hAlign:"right"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-left");
				return {vAlign:"top", hAlign:"left"};
			}
		}
		else
		{
			if (isCrossingLeftOfWindow() && !isCrossingRightOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-right");
				return {vAlign:"bottom", hAlign:"right"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-left");
				return {vAlign:"bottom", hAlign:"left"};
			}
		}
	}
	else if (originalPointerPos == "bottomRight")
	{
		if (isCrossingBottomOfWindow() && !isCrossingTopOfWindow())
		{
			if (isCrossingRightOfWindow() && !isCrossingLeftOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-left");
				return {vAlign:"top", hAlign:"left"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-above-right");
				return {vAlign:"top", hAlign:"right"};
			}
		}
		else
		{
			if (isCrossingRightOfWindow() && !isCrossingLeftOfWindow())
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-left");
				return {vAlign:"bottom", hAlign:"left"};
			}
			else
			{
				replacePopupClassAttribute(popupContainerId, "pop-up-below-right");
				return {vAlign:"bottom", hAlign:"right"};
			}
		}
	}
    else
    {
        return {vAlign:"top", hAlign:"left"};   
    }
}

function isCrossingTopOfWindow()
{
	return ((mousePositionTop - containerHeight) < 0)
}

function isCrossingLeftOfWindow()
{
	return ((mousePositionLeft - containerWidth) < 0)
}

function isCrossingBottomOfWindow()
{
	return ((mousePositionTop + containerHeight) > viewPortHeight)
}

function isCrossingRightOfWindow()
{
	return ((mousePositionLeft + containerWidth) > viewPortWidth)
}

function replacePopupClassAttribute(popupContainerId, newPopupClassAtrr)
{
	var currentClassAttr = jQuery(popupContainerId).attr("class");
	var newClassAttr;
	if (currentClassAttr.indexOf("pop-up-above-right") != -1)
		newClassAttr = currentClassAttr.replace("pop-up-above-right", newPopupClassAtrr);
	else if (currentClassAttr.indexOf("pop-up-above-left") != -1)
		newClassAttr = currentClassAttr.replace("pop-up-above-left", newPopupClassAtrr);
	else if (currentClassAttr.indexOf("pop-up-below-left") != -1)
		newClassAttr = currentClassAttr.replace("pop-up-below-left", newPopupClassAtrr);
	else
		newClassAttr = currentClassAttr.replace("pop-up-below-right", newPopupClassAtrr);
	jQuery(popupContainerId).attr("class", newClassAttr);
}