var prevTooltip;

function getWindowWidth() {

    if (window.innerWidth) {
        return window.innerWidth;
    }

    return document.body.clientWidth
}

function mouseX(e) {

    if (e.pageX) {
        return e.pageX;
    }

    return e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

function mouseY(e) {

    if (e.pageY) {
        return e.pageY;
    }

    return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

function getPositionXold(obj) {
    var leftValue = 0;
    while (obj) {
        alert(obj.offsetLeft);
        leftValue += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return leftValue;
}

function getPositionX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function getPositionYOld(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curleft += obj.y;
    }
    return curleft;
}

function getPositionY(obj) {
    var topValue = 0;
    while (obj) {
        topValue += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return topValue;
}

function tooltip(e, c, o, x, y) {

    var windowWidth = getWindowWidth();

    o = document.getElementById(o);
    
    if (prevTooltip && prevTooltip != o) {
        prevTooltip.style.visibility = 'hidden';
    }

    if (o.style.visibility == 'visible') {

        o.style.visibility = 'hidden';
    } else {

        if (o.offsetWidth) {
            ew = o.offsetWidth;
        } else if (o.clip.width) {
            ew = o.clip.width;
        }
        
        c = document.getElementById(c);
        x = c.offsetLeft + x;

        if (mouseY(e) > 380) { // 380 is approx halfway down visible site area
            y = mouseY(e) - 25 - o.offsetHeight;
        } else {
            y = mouseY(e) + 25;
        }

        //var position = $(me).position();

        //p_oDiv = oDiv;


        //oChkbx = $(oDiv).find(":checkbox");   // find 'Other' checkbox
        //oTxt = $(oDiv).find(":text");   // find 'Other' (initially hidden) textbox
        //oPopup = $('#OtherDescriptionPopup');

        //if ($(oChkbx).attr("checked") == true) {

            //var position = $(o).position();


            //position.left = position.left - 100;
            //position.top = position.top - 0;
            //$(oPopup).css(position); //{ left: position.left, top: position.top })

            //add related textbox to popup
            //oTxt.appendTo($(oPopup).find("#PlaceHolderDesc"));
            //$(oTxt).show();

            //$(oPopup).show();
        //}

        if (x < 2) {
            x = 2;
        } else if (x + ew > windowWidth) {
            x = windowWidth - ew - 4;
        }

        o.style.left = x + 'px';
        o.style.top = y + 'px';
        
        $(o).appendTo($("#container")); // append to top level container so it doesn't get cropped
        
        o.style.visibility = 'visible';

        prevTooltip = o;
    }
}
