﻿function findLeft(obj) {
    if (!obj) return 0;
    return obj.offsetLeft + findLeft(obj.offsetParent);
}
function findTop(obj) {
    if (!obj) return 0;
    return obj.offsetTop + findTop(obj.offsetParent);
}
function getWidth(obj) {
    if (!obj) return 0;
    if (obj.offsetWidth &&  obj.offsetWidth != 0) return obj.offsetWidth;
    if (obj.clientWidth && obj.clientWidth != 0) return obj.clientWidth;
    if (obj.width && obj.width != 0) return obj.width;
    return parseInt(obj.style.width.split('px')[0]);
}
function getHeight(obj) {
    if (!obj) return 0;
    if (obj.offsetHeight && obj.offsetHeight != 0) return obj.offsetHeight;
    if (obj.clientHeight && obj.clientHeight != 0) return obj.clientHeight;
    if (obj.height && obj.height != 0) return obj.height;
    return parseInt(obj.style.height.split('px')[0]);
}
function AbsPos(obj_id, related_obj_id, pos, margin) {    
    var obj = document.getElementById(obj_id);
    var related_obj = document.getElementById(related_obj_id);        
    if (obj == null || related_obj == null)
        return;

    var rwidth = getWidth(related_obj);
    var rheight = getHeight(related_obj);
    var owidth = getWidth(obj);
    var oheight = getHeight(obj);

    //alert(related_obj.style.width + ' ' + related_obj.style.height + ' ' + obj.style.width + ' ' + obj.style.height);
    //alert(rwidth + '==' + rheight + '==' + owidth + '==' + oheight);    
    
    var top = findTop(related_obj);
    var left = findLeft(related_obj);
    var bottom = top + rheight;
    var right = left + rwidth;

    //alert(top + ' ' + left + ' ' + bottom + ' ' + right);

    var obj_top = 0;
    var obj_left = 0;       
        
    if (pos == "top:left") {
        obj_top = top + margin;
        obj_left = left + margin;
    }
    if (pos == "top:right") {
        obj_top = top + margin;
        obj_left = right - margin - owidth;
    }
    if (pos == "bottom:left") {
        obj_top = bottom - margin - oheight;
        obj_left = left + margin;
    }
    if (pos == "bottom:right") {
        obj_top = bottom - margin - oheight;
        obj_left = right - margin - owidth;
    }
    obj.style.top = obj_top + 'px';
    obj.style.left = obj_left + 'px';
    obj.style.display = "";
    //alert(obj.style.top + ' ' + obj.style.left);
}