/* global AJS:false */
AJS.test.require("com.atlassian.bitbucket.server.bitbucket-web:scroll", function() {

    'use strict';

    var $ = jQuery;
    var _ = require('lodash');
    var scroll = require('bitbucket/internal/util/scroll');
    var $container = $(document.body);

    // Using my own fixture here because it seems
    // that QUnit cleans its fixture before the async tests finish
    var $fixture = $('<div id="custom-fixture"></div>').css({
        position: 'absolute',
        top: 0,
        left: 0,
        height: (document.documentElement.clientHeight + 1000) + 'px' // make sure the fixture is taller than the window
    });

    var $dummy = $('<div id="dummy"></div>').css({
        position: 'absolute',
        top: 500,
        width: 10,
        height: 10,
        backgroundColor: '#ddd'
    });
    $fixture.append($dummy);

    $container.append($fixture);

    module('util-scroll', {
        setup: function () {
            window.scrollTo(0, 0);
        }
    });

    asyncTest('Positioning', function () {

        scroll.scrollTo($dummy, {
            getScrollPadding: function () {
                return 0;
            },
            duration: 0
        });

        _.defer(function () {
            equal($dummy.offset().top, window.scrollY, 'Window was scrolled to the position of the element.');
            start();
            stop();

        });

        _.defer(function () {
            // Move the dummy so we have another place to scroll to
            $dummy.css('top', 800);

            // emulate the default scroll padding
            var scrollPadding = document.documentElement.clientHeight / 4;


            scroll.scrollTo($dummy, {
                getScrollPadding: function () {
                    return scrollPadding;
                },
                duration: 0
            });

            _.defer(function () {
                start();
                equal(Math.floor($dummy.offset().top - scrollPadding), window.scrollY, 'Window was scrolled to element with correct scroll padding.');
            });
        });


    });


    asyncTest('Animation', function () {
        window.scrollTo(0, 0);

        scroll.scrollTo($dummy, {
            getScrollPadding: function () {
                return 0;
            },
            duration: 100
        });

        notEqual($dummy.offset().top, window.scrollY, 'Window has not yet scrolled to the position of the element.');

        setTimeout(function () {
            equal($dummy.offset().top, window.scrollY, 'After a timeout greater than animation duration the window has scrolled to the position of the element.');
            start();
        }, 125);


    });

    asyncTest('Cancellation', function () {

        scroll.scrollTo($dummy, {
            getScrollPadding: function () {
                return 0;
            },
            duration: 0,
            cancelIfScrolled: true
        });
        window.scrollTo(0, 1); // scroll the window to trigger the event handler set up by the scroll util

        _.defer(function () {
            notEqual($dummy.offset().top, window.scrollY, 'Window was not scrolled to the position of the element.');
            start();
        });


    });


});