/* Copyright 2003-2008 Emergent Music LLC  All rights reserved.
$Id$
*/

// adapted from http://blog.firetree.net/2005/07/04/javascript-find-position/


FLYFI.cssPxToInt = function (x) {
    // css attributes will have 'px' appended, so remove that and convert to an integer
    if (x.slice(-2) == 'px') {
        return parseInt(x.slice(0, -2), 10);
    }
    return parseInt(x, 10);
};

FLYFI.findPosX = function(obj) {   
    var curleft = 0;
    if (obj.offsetParent) {
        while (true) {
            var borderLeftWidth = $(obj).css('border-left-width');
            if (borderLeftWidth == "medium") {
                borderLeftWidth = "2px"; // For IE6
            } else if (isNaN(FLYFI.cssPxToInt(borderLeftWidth))) {
                borderLeftWidth = "0px";
            }
            curleft += (obj.offsetLeft + FLYFI.cssPxToInt(borderLeftWidth));
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.x) {
        curleft += obj.x;
    }
    return curleft;
};

FLYFI.findPosY = function(obj) {
    var curtop = 0;
    if(obj.offsetParent) {
        while (true) {
            curtop += obj.offsetTop;
            if(!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.y) {
        curtop += obj.y;
    }
    return curtop;
};
