
if (typeof jQuery === 'undefined') {
    throw new Error('Mobfish_SASS\'s JavaScript requires jQuery');
}


var mfElement = (function($) {

    /**
     * This method will check if an element is visible
     * @param element
     * @returns {boolean}
     */
    function isVisible(element) {
        // console.log( 'isVisible', (element.offsetWidth > 0 && element.offsetHeight > 0), element.offsetWidth, element.offsetHeight );
        return (element.offsetWidth > 0 && element.offsetHeight > 0);
    }

    /**
     * This method will check if an element is in viewport
     * @param element
     * @returns {boolean}
     */
    function isInViewport(element) {

        if( $(element).length ) {
            element = element[0];

            var visible = isVisible( element );
            if( !visible ) {
                element.style.visibility = 'hidden';
                element.style.opacity = '0';
                element.style.display = 'block';
            }

            var rect = element.getBoundingClientRect();
            var html = document.documentElement;
            var inView = (
                rect.top >= 0 &&
                rect.left >= 0 &&
                rect.bottom <= (window.innerHeight || html.clientHeight) &&
                rect.right <= (window.innerWidth || html.clientWidth)
            );


            if( !visible ) {
                element.style.visibility = 'inherit';
                element.style.opacity = 'inherit';
                element.style.display = 'none';
            }

            return {
                inView: inView,
                top: rect.top >= 0,
                left: rect.left >= 0,
                bottom: rect.bottom <= (window.innerHeight || html.clientHeight),
                right: rect.right <= (window.innerWidth || html.clientWidth)
            };
        }

        return {
            inView: false
        };
    }

    /**
     * Method which tries to move a element in viewport
     * @param ele
     */
    function moveElementInViewport(ele) {

        if( !$(ele).length ) return;

        var inView = isInViewport(ele);

        if (! inView.inView ) {

            if( !inView.left || !inView.right ) {
                $(ele).css('max-width', '90vw');

                inView = isInViewport(ele);

                if( !inView.left ) {
                    $(ele).css('left', '0');
                    $(ele).css('right', 'auto');
                }

                if( !inView.right ) {
                    if( $(ele).parents('.dropdown').length > 1 ) {
                        $(ele).css('right', '100%');
                    } else {
                        $(ele).css('right', '0');
                    }

                    $(ele).css('left', 'auto');
                }
            }


            if( !inView.bottom ) {

                $(ele).css('max-height', '50vh');
                $(ele).css('overflow', 'hidden');
                $(ele).css('overflow-y', 'auto');

                inView = isInViewport(ele);
                if( !inView.bottom ) {
                    $(ele).css('bottom', '0');
                    $(ele).css('top', 'auto');
                }
            }
        }
    }


    function isDescendant(parent, child) {
        var node = child;

        while (node != null) {
            if (node === parent) {
                return true;
            }
            node = node.parentNode;
        }
        return false;
    }

    return {
        isVisible: function( element ) {
            return isVisible( element );
        },
        isInViewport: function( element ) {
            return isInViewport( element );
        },
        moveElementInViewport: function( element ) {
            return moveElementInViewport( element );
        },
        isDescendant: function( parent, child ) {
            return isDescendant( parent, child );
        }
    };

})(jQuery);