﻿function GetAbsoluteOffsetLeft(obj) {
    var left = obj.offsetLeft;
    var parent = obj.offsetParent;
    while ((parent != null && parent != document.body)) {
        left += parent.offsetLeft;
        parent = parent.offsetParent;
    }
    return left;
}

function GetAbsoluteOffsetTop(obj) {
    var top = obj.offsetTop;
    var parent = obj.offsetParent;
    while ((parent != null && parent != document.body)) {
        top += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return top;
}

function GetLeftLocationForDiv(eventLoc, divWidth, parentElement) {
    if (eventLoc == null || divWidth == null) {
        return -1;
    }

    if (parentElement == null)
        parentElement = document.body;

    // Get the direction of the body: Right-to-Left or Left-to-Right.
    var bodyDir = "ltr";
    if (document.body.dir && document.body.dir != "") {
        bodyDir = document.body.dir;
    }

    var left;

    // If it is Left-to-Right we want to display the div to the RIGHT of the event location (e.g. the left of the div is in the current
    // event location).
    if (bodyDir == "ltr") {
        // Check we have place for it, if not we will display it LEFT to the event location.
        if (eventLoc + divWidth > document.body.offsetWidth) {
            left = eventLoc - divWidth;
        }
        else {
            left = eventLoc;
        }
    }
    // If it is Right-to-Left we want to display the div RIGHT to the event location.
    else {
        // Check if we have room RIGHT to the event location, if not show it to the LEFT of the event location.
        if (eventLoc - divWidth < 0) {
            left = eventLoc;
        }
        else {
            left = eventLoc - divWidth - 30;
        }
    }

    // Since we return the left realtive to the parent element we need to subtract the parent's left from
    // the event left.
    left = left - parentElement.offsetLeft;

    return left;
}
