﻿var pop;
var xoffset = 10;
var yoffset = 10;

//Finds the current mouse position on the screen
function mousePosition(event)
{
    var x = xoffset + scrollLeft();
    var y = yoffset + scrollTop();
    var right = clientWidth() + scrollLeft();
    var bottom = clientHeight() + scrollTop();
    
    if (event.x)
    {
        x += event.x;
        y += event.y;
    }
    else if (event.layerX)
    {
        x += event.layerX;
        y += event.layerY;
    }
    else
    {
        x += e.pageX;
        y += e.pageY;
    }

    //Preventing Popups from displaying off the side of the page
    if(x> right - pop.clientWidth)
    {
        x = right - pop.clientWidth - 5;
    }
    
    if(y> bottom - pop.clientHeight)
    {
        y = bottom - pop.clientHeight - 5;
    }
    pop.style.top = y + 'px';
    pop.style.left = x + 'px';
}

//Creates and displays the popup
function popup(text, event)
{
    pop = document.getElementById('popup');
    mousePosition(event);
    pop.innerHTML = text;
    pop.style.display = 'block';
}

//Hides the created popup
function popout()
{
    pop.style.display = 'none';
}

//Finds the width of the screen
function clientWidth()
{
    return filterResults
    (
        window.innerWidth ? window.innerWidth : 0,
        document.documentElement ? document.documentElement.clientWidth : 0,
        document.body ? document.body.clientWidth : 0
    );
}

//Finds the height of the screen
function clientHeight()
{
    return filterResults
    (
        window.innerHeight ? window.innerHeight : 0,
        document.documentElement ? document.documentElement.clientHeight : 0,
        document.body ? document.body.clientWidth : 0
    );
}

//Finds the left/right scroll position
function scrollLeft()
{
    return filterResults
    (
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0
    );
}

//Finds the up/down scroll position
function scrollTop()
{
    return filterResults
    (
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0
    );
}

//Find out which client the user is accessing with
function filterResults(win, doc, body)
{
    var result = win ? doc : 0;
    if (doc && (!result || (result> doc)))
    {
        result = doc;
    }
    return body && (!result || (result> body)) ? body : result;
}