// JavaScript Document
function getElementPos(element)
{
	var coords = {x: 0, y: 0};
	do {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
	}
	while ((element = element.offsetParent));
	return coords;
}

function setElementPos(element, coords)
{
	element.style.left = coords.x+"px";
	element.style.top = coords.y+"px";
}

function keepHoverOnPage(coords, el)
{
	// keep it on the page
	winWidth = getWinWidth();
	if (coords.x + el.offsetWidth > winWidth)
		coords.x = winWidth-el.offsetWidth-2;

	if (coords.x < 0)
		coords.x = 0;
	
	winBottom = getWinBottom();
	if(coords.y+el.offsetHeight > winBottom)
		coords.y = winBottom - el.offsetHeight;
		
	if (coords.y < 0)
		coords.y = 0;
		
	return coords;
}

function getWinWidth() {
    if (window.innerWidth) {
        w = window.innerWidth;
        if (document.body.scrollHeight &&
            document.body.scrollHeight >= getWinHeight()) {
            w -= 16;
        }
        return w;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        return document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    } else if (document.body &&
        document.body.parentNode && document.body.parentNode.clientWidth) {
        return document.body.parentNode.clientWidth;
    }
}

function getWinHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    } else if (document.body &&
        document.body.parentNode && document.body.parentNode.clientHeight) {
        return document.body.parentNode.clientHeight;
    }
}

function getWinLeft() {
    return typeof window.pageXOffset != "undefined" ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function getWinTop() {
	return typeof window.pageYOffset != "undefined" ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function getWinRight() { return getWinLeft() + getWinWidth(); }

function getWinBottom() { return getWinTop() + getWinHeight(); }
