/*

Siesta 5.1.0
Copyright(c) 2009-2018 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license

*/
Role('Siesta.Util.Role.CanCalculatePageScroll', {
    // also recognizes the "global" attribute

    methods : {

        getElForPageScroll : function (win) {
            win                 = win || this.global
            var doc             = win.document

            return doc.scrollingElement || (bowser.webkit || bowser.blink || bowser.msedge ? doc.body : doc.documentElement)
        },


        getPageScrollX : function (win) {
            win                 = win || this.global
            var doc             = win.document

            if (window.pageXOffset != null) return win.pageXOffset

            var scrollEl        = this.getElForPageScroll()

            return scrollEl ? scrollEl.scrollLeft : 0
        },


        getPageScrollY : function (win) {
            win                 = win || this.global
            var doc             = win.document

            if (window.pageYOffset != null) return win.pageYOffset

            var scrollEl        = this.getElForPageScroll()

            return scrollEl ? scrollEl.scrollTop : 0
        },


        viewportXtoPageX : function (x, win) {
            win                 = win || this.global
            var docEl           = win.document.documentElement

            // seems the "docEl.clientLeft" thing is copied from jQuery, not sure what kind of
            // edge case it is supposed to solve
            return x + this.getPageScrollX(win) - docEl.clientLeft
        },


        viewportYtoPageY : function (y, win) {
            win                 = win || this.global
            var docEl           = win.document.documentElement

            // seems the "docEl.clientLeft" thing is copied from jQuery, not sure what kind of
            // edge case it is supposed to solve
            return y + this.getPageScrollY(win) - docEl.clientTop
        },


        pageXtoViewportX : function (x, win) {
            win                 = win || this.global
            var docEl           = win.document.documentElement

            // seems the "docEl.clientLeft" thing is copied from jQuery, not sure what kind of
            // edge case it is supposed to solve
            return x - this.getPageScrollX(win) + docEl.clientLeft
        },


        pageYtoViewportY : function (y, win) {
            win                 = win || this.global
            var docEl           = win.document.documentElement

            // seems the "docEl.clientLeft" thing is copied from jQuery, not sure what kind of
            // edge case it is supposed to solve
            return y - this.getPageScrollY(win) + docEl.clientTop
        }
    }
})