// get nearest scroll element
// http://w3help.org/zh-cn/causes/SD9013
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
var overflowScrollReg = /scroll|auto/i;
export function getScroller(el) {
    var root = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window;

    var node = el;

    while (node && node.tagName !== 'HTML' && node.nodeType === 1 && node !== root) {
        var _window$getComputedSt = window.getComputedStyle(node),
            overflowY = _window$getComputedSt.overflowY;

        if (overflowScrollReg.test(overflowY)) {
            if (node.tagName !== 'BODY') {
                return node;
            }

            var _window$getComputedSt2 = window.getComputedStyle(node.parentNode),
                htmlOverflowY = _window$getComputedSt2.overflowY;

            if (overflowScrollReg.test(htmlOverflowY)) {
                return node;
            }
        }
        node = node.parentNode;
    }

    return root;
}

export function getScrollTop(element) {
    return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
}

export function setScrollTop(element, value) {
    'scrollTop' in element ? element.scrollTop = value : element.scrollTo(element.scrollX, value);
}

export function getRootScrollTop() {
    return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
}

export function setRootScrollTop(value) {
    setScrollTop(window, value);
    setScrollTop(document.body, value);
}

// get distance from element top to page top
export function getElementTop(element) {
    return (element === window ? 0 : element.getBoundingClientRect().top) + getRootScrollTop();
}

export function getVisibleHeight(element) {
    return element === window ? element.innerHeight : element.getBoundingClientRect().height;
}

export function getVisibleTop(el) {
    if (el === window) {
        return 0;
    }
    return el.getBoundingClientRect().top;
}