/* position.js - positioning functions for HTML Elements */

function positionWindowHeight() {
    //return document.body.scrollHeight;
    //return window.innerHeight;
    return document.body.clientHeight;
}
function positionWindowWidth() {
    //return document.body.scrollWidth;
    //return window.innerWidth;
    return document.body.clientWidth;
}
function positionPercent(element, x, y, attach) {
    if (x > 1) x = 1.0;
    else if (x < 0) x = 0.0;
    if (y > 1) y = 1.0;
    else if (y < 0) y = 0.0;
    
    var h = positionWindowHeight();
    var w = positionWindowWidth();
    
    positionPixel(element, (w*x), (h*y), attach);
}
function positionPixel(element, x, y, attach) {
    var offsetRegex = /^offset:([-]?[0-9]+)([%]?),\s*([-]?[0-9]+)([%]?)/;
    var ew = element.offsetWidth;
    var eh = element.offsetHeight;
    
    var dx = 0;
    var dy = 0;
    if (!attach) attach = "center";
    
    if (attach.match(offsetRegex)) {
        var matches = attach.match(offsetRegex);
        dx = matches[1]*1; dy = matches[3]*1;
        if (matches[2] == "%") dx = Math.floor(ew * ((dx + 0.0)/100.0));
        if (matches[4] == "%") dy = Math.floor(eh * ((dy + 0.0)/100.0));
    } else {
        if (attach.match(/^top/)) {
            dy = 0;
        } else if (attach.match(/^bottom/)) {
            dy = eh;
        } else {
            dy = Math.floor(eh / 2);
        }

        if (attach.match(/left$/)) {
            dx = 0;
        } else if (attach.match(/right$/)) {
            dx = ew;
        } else {
            dx = Math.floor(ew / 2);
        }
    }
    
    //alert("Positioning " + x + ", " + y + " (attach " + attach + "): " + Math.floor(x - dx) + ", " + Math.floor(y-dy));
    
    element.style.left = (x - dx) + "px";
    element.style.top = (y - dy) + "px";
    //element.style.position = "absolute";
}
function positionXY(element, attach) {
    var offsetRegex = /^offset:([-]?[0-9]+)([%]?),\s*([-]?[0-9]+)([%]?)/;
    var ew = element.offsetWidth;
    var eh = element.offsetHeight;
    
    var dx = 0;
    var dy = 0;
    if (!attach) attach = "topleft";

    if (attach.match(offsetRegex)) {
        var matches = attach.match(offsetRegex);
        dx = matches[1]*1; dy = matches[3]*1;
        if (matches[2] == "%") dx = Math.floor(ew * ((dx + 0.0)/100.0));
        if (matches[4] == "%") dy = Math.floor(eh * ((dy + 0.0)/100.0));
    } else {
        if (attach.match(/^top/)) {
            dy = 0;
        } else if (attach.match(/^bottom/)) {
            dy = eh;
        } else {
            dy = Math.floor(eh / 2);
        }

        if (attach.match(/left$/)) {
            dx = 0;
        } else if (attach.match(/right$/)) {
            dx = ew;
        } else {
            dx = Math.floor(ew / 2);
        }
    }
    
    offsetX = 0;
    offsetY = 0;
    for (p = element; p != null; p = p.offsetParent) {
        offsetX += p.offsetLeft;
        offsetY += p.offsetTop;
    }
    
    return [dx+offsetX, dy+offsetY];
}
function positionX(element, attach) {
    return positionXY(element, attach)[0];
}
function positionY(element, attach) {
    return positionXY(element, attach)[1];
}
function positionRelative(elementA, attachA, elementB, attachB, offsetX, offsetY) {
    if (!elementA) return;
    if (!attachA) attachA = "topleft";
    if (!elementB) elementB = document.body;
    if (!attachB) attachB = "topleft";
    if (!offsetX) offsetX = 0;
    if (!offsetY) offsetY = 0;
    
    var xy = positionXY(elementB, attachB);
    positionPixel(elementA, xy[0]+offsetX, xy[1]+offsetY, attachA);
}
