// JS / Site / Load More

SITE.loadMore = {

    init: function() {

        var obj = this,
            loadMoreEls = [].slice.call(document.querySelectorAll('*[data-loadmore]'));

        // State
        window.addEventListener('popstate', function(_e) {

            // Exit if not load more action
            if (_e.state === null || !_e.state.type || _e.state.type != 'loadmore')
                return;

            // Load
            DE.http.get({
                url: document.location.pathname + (document.location.search ? document.location.search : '?'),
                type: 'document',
                onSuccess: function(_data) {

                    var currentListingEl = document.querySelector('*[data-loadmore="' + _e.state.id + '"]'),
                        currentNextEl = currentListingEl ? currentListingEl.parentNode.querySelector('*[data-loadmore-next="' + _e.state.id + '"]') : null,
                        loadListingEl = _data.querySelector('*[data-loadmore="' + _e.state.id + '"]'),
                        loadNextEl = loadListingEl ? loadListingEl.parentNode.querySelector('*[data-loadmore-next="' + _e.state.id + '"]') : null;

                    if (!currentListingEl)
                        return;

                    currentListingEl.innerHTML = loadListingEl.innerHTML;

                    if (currentNextEl)
                        currentNextEl.remove();

                    if (loadNextEl) {
                        currentListingEl.insertAdjacentElement('afterend', loadNextEl);
                        obj.bindNext(currentListingEl, loadNextEl);
                    }

                }
            });


        });

        // Load More
        loadMoreEls.forEach(function(_listingEl) {

            window.history.replaceState({
                type: 'loadmore',
                id: _listingEl.dataset.loadmore
            }, null, window.location.pathname + window.location.search);

            obj.bind(_listingEl);

        });

    },

    bind: function(_listingEl) {
        var nextEl = _listingEl.parentNode.querySelector('*[data-loadmore-next="' + _listingEl.dataset.loadmore + '"]');
        if (nextEl)
            this.bindNext(_listingEl, nextEl);
    },

    bindNext: function(_listingEl, _nextEl) {

        var obj = this;

        // Bind
        _nextEl.addEventListener('click', function(_e) {

            if (_e.target.nodeName != 'A')
                return;

            _e.preventDefault();

            DE.http.get({
                url: _e.target.pathname + _e.target.search + '&next=1',
                type: 'document',
                onSuccess: function(_data) {

                    var listingEl = _data.querySelector('*[data-loadmore="' + _listingEl.dataset.loadmore + '"]'),
                        nextEl = listingEl.parentNode.querySelector('*[data-loadmore-next="' + _listingEl.dataset.loadmore + '"]');

                    _listingEl.insertAdjacentHTML("beforeend", listingEl.innerHTML);

                    if (nextEl)
                        _nextEl.innerHTML = nextEl.innerHTML;
                    else
                        _nextEl.remove();

                }
            });

            // State
            window.history.pushState({
                type: 'loadmore',
                id: _listingEl.dataset.loadmore
            }, null, _e.target.pathname + _e.target.search);

        });

    }

}

window.addEventListener("load", function() {
    SITE.loadMore.init();
});