
//class
function position(){}

position.getStyle = function(view, prop)
{
    if  (window && window.getComputedStyle) 
    {    
        var el = view, v, cs, camel;
        if(prop == 'float')
            prop = 'cssFloat';
        
        if(v = el.style[prop])
            return v;
        
        if(cs = window.getComputedStyle(el, ''))
            return cs[prop];

        return null;
     }
     else
     {
        var el = view, v, cs, camel;
        if(prop == 'opacity')
        {
            if(typeof el.style.filter == 'string'){
                var m = el.style.filter.match(/alpha\(opacity=(.*)\)/i);
                if(m)
                {
                    var fv = parseFloat(m[1]);
                    if(!isNaN(fv))
                        return fv ? fv / 100 : 0;
                }
            }
            return 1;
        }
        else if(prop == 'float')
            prop = 'styleFloat';
        
        if(v = el.style[prop])
            return v;
        
        if(cs = el.currentStyle)
            return cs[prop];
        
        return null;
    }
} 

position.getScroll = function(d)
{
    var isIE = !!(window.attachEvent && !window.opera);
    var isStrict = document.compatMode == 'CSS1Compat';

    var doc = document;
    if(d == doc || d == doc.body)
    {
        var l, t;
        if(isIE && isStrict){
            l = doc.documentElement.scrollLeft || (doc.body.scrollLeft || 0);
            t = doc.documentElement.scrollTop || (doc.body.scrollTop || 0);
        }else{
            l = window.pageXOffset || (doc.body.scrollLeft || 0);
            t = window.pageYOffset || (doc.body.scrollTop || 0);
        }
        return {left: l, top: t};
    }
    else
        return {left: d.scrollLeft, top: d.scrollTop};
}
    
position.getXY = function(el) 
{
    var isGecko = /Gecko/.test(navigator.userAgent) && !/Konqueror|KHTML/.test(navigator.userAgent);
    var isSafari = /KHTML/.test(navigator.userAgent) && /Apple/.test(navigator.userAgent);
    var isOpera = !!window.opera;

    var p, pe, b, scroll, bd = (document.body || document.documentElement);

    if(el == bd){
        return [0, 0];
    }

    if (el.getBoundingClientRect) 
    {
        b = el.getBoundingClientRect();
        scroll = this.getScroll(document);
        return [b.left + scroll.left - 2, b.top + scroll.top - 2];
    }
    var x = 0, y = 0;
    
    p = el;

    var hasAbsolute = this.getStyle(el, 'position') == 'absolute';

    while (p) 
    {   
        var stylePosition = this.getStyle(p, 'position');

        x += p.offsetLeft;
        y += p.offsetTop;
        
        if (!hasAbsolute && stylePosition == 'absolute') 
            hasAbsolute = true;

        if (isGecko) 
        {
            pe = p;
            
            var bt = parseInt(this.getStyle(pe, 'borderTopWidth'), 10) || 0;
            var bl = parseInt(this.getStyle(pe, 'borderLeftWidth'), 10) || 0;

            if (!(p.tagName == 'TABLE' && stylePosition != 'static') && this.getStyle(p, 'MozBoxSizing') != 'border-box')
            {
                x += bl;
                y += bt;
            }
            
            var mt = parseInt(this.getStyle(pe, 'marginTop'), 10) || 0;
            var ml = parseInt(this.getStyle(pe, 'marginLeft'), 10) || 0;
            
            if (p.tagName == 'TABLE' && stylePosition != 'static')
            {
                x += ml;
                y += mt;
            }


            if (p != el && this.getStyle(pe, 'overflow') != 'visible') 
            {
                x += bl;
                y += bt;
            }
        }
        p = p.offsetParent;
    }

    if (isSafari && hasAbsolute) 
    {
        x -= bd.offsetLeft;
        y -= bd.offsetTop;
    }

    if (isGecko && !hasAbsolute) 
    {
        var dbd = bd;
        x += parseInt(this.getStyle(dbd, 'borderLeftWidth'), 10) || 0;
        y += parseInt(this.getStyle(dbd, 'borderTopWidth'), 10) || 0;
    }

    p = el.parentNode;
    while (p && p != bd) 
    {
        if (!isOpera || (p.tagName != 'TR' && this.getStyle(p, 'display') != 'inline')) 
        {
            x -= p.scrollLeft;
            y -= p.scrollTop;
        }
        p = p.parentNode;
    }
    return [x, y ];
}

position.getPositionInCont = function(elem, cont)
{
    var elemPos = position.getXY(elem);
    var parent = cont.parentNode;
    while(parent && parent.tagName != 'HTML' && getStylePosition(parent) == 'static')
        parent = parent.parentNode;
        
    var parentPos = position.getXY(parent);

    return [elemPos[0] - parentPos[0], elemPos[1] - parentPos[1]];
}