﻿var stoppedCarousel = true,
startedCarousel = false,
pausedCarousel = false,
pausedCarouselByPopup = false,
interval,
scrollYOffset,
lastMovedColumn = -1,
stoppedColumnIndex = -1,
defaulCarouselInterval = 4000,
pauseLoading = false,
carouselCount = 0;

function moveEvents() {
    var currentIndex;
    do {
        currentIndex = Math.floor(Math.random() * 4);
    }
    while (currentIndex == lastMovedColumn || currentIndex == stoppedColumnIndex)
    lastMovedColumn = currentIndex;
    carouselCount++;
    moveCol(currentIndex);
}

function loadNextEvents() {
    //$(function () {
    //    if (!stop) {
    //        if (!pauseLoading && nearBottom()) {
    //            pauseLoading = true;
    //            f(0);
    //        }
    //        setTimeout(loadNextEvents, 200);
    //    }
    //});
}
function f(mode) {
    $("#live_spinner").show();
    $("body").css("cursor", "wait");
    if (mode == 0) {
        stopCarousel();
    }
    else {
        pauseCarousel(false);
    }
    $.ajax({
        url: "/GetEvents.ashx",
        dataType: 'json',
        data: {
            lc: pageNumber
        },
        success: function (data) {
            if (data.response == undefined) {
                var $boxes = "";
                $(data).each(function (i, n) {
                    $boxes += _.template($("#eventTemplate").html(), {
                        title: data[i].title,
                        url: data[i].url,
                        img: data[i].img == "" ? null : data[i].img,
                        museum: data[i].museum,
                        teaser: data[i].teaser,
                        categories: data[i].categories != null && data[i].categories.length ?
                            '<ul class="categories"><li>' + data[i].categories.join('</li><li>') + '</li></ul>' : ''
                    }).replace(/(\r\n|\n|\r)/gm, "");
                });
                $boxes = $($boxes);
                $container.masonry('option', {
                    animationOptions: {
                        complete: function () {
                            $boxes.fadeIn();
                            unpauseCarousel(false);
                            pauseLoading = false;
                        },
                        duration: mode == 0 ? 0 : 1200
                    }
                });
                $boxes.imagesLoaded(function () {
                    $boxes.each(function (i, n) {
                        $(n).hide();
                        var colIndex = i % 4;
                        if (mode == 0) {
                            $container.eq(colIndex).append($(n)).masonry('appended', $(n), true);
                        }
                        else {
                            if (colIndex == 0) {
                                $container.eq(colIndex).find('.black-box').after($(n));
                            }
                            else {
                                $container.eq(colIndex).prepend($(n));
                            }
                            $container.masonry('reload');
                        }
                    });
                    pageNumber++;
                });
            }
            else {
                stop = true;
                pauseLoading = false;
                unpauseCarousel(false);
            }
        },
        complete: function () {
            $("#live_spinner").hide();
            $("body").css("cursor", "default");
        }
    });
}
function startCarousel() {
    $(".front-boxes .col").each(function (index, e) {
        $(e).mouseenter(function () {
            stoppedColumnIndex = index;
        }).mouseleave(function () {
            stoppedColumnIndex = -1;
        });
    });
    if (stoppedCarousel && !startedCarousel) {
        stoppedCarousel = false;
        startedCarousel = true;
        interval = setInterval(moveEvents, defaulCarouselInterval);
    }
}
function moveCol(colIndex) {
    $col = $container.eq(colIndex);
    $col.children().last().fadeOut(function () {
        $lastBoxCopy = $col.children().last().clone();
        $container.masonry('option', {
            animationOptions: {
                complete: function () {
                    $lastBoxCopy.fadeIn(function () {
                        //if (carouselCount >= 4) {
                        //    pauseLoading = true;
                        //    if (!stop)
                        //        f(1);
                        //    carouselCount = 0;
                        //}
                    });
                },
                duration: 2000
            }
        });
        $col.children().last().remove();
        if (colIndex == 0) {
            $col.find('.black-box').after($lastBoxCopy);
        }
        else {
            $col.prepend($lastBoxCopy);
        }
        $col.masonry('reload');
    });
}

function nearBottom() {
    return scroll_distance_from_bottom() < 200;
}
function scroll_distance_from_bottom() {
    return page_height() - (get_scroll_height() + get_inner_height());
}
function page_height() {
    return Math.max(document.body.scrollHeight, document.body.offsetHeight);
}
function get_scroll_height() {
    var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    return h ? h : 0;
}
function get_inner_height() {
    var h = self.innerHeight || document.documentElement.clientHeight;
    return h ? h : 0;
}

function stopCarousel() {
    clearInterval(interval);
    stoppedCarousel = true;
    startedCarousel = false;
}

function unpauseCarousel(byPopup) {
    if (byPopup) {
        if (pausedCarouselByPopup && !pausedCarousel && !startedCarousel && !stoppedCarousel) {
            pausedCarouselByPopup = false;
            startedCarousel = true;
            interval = setInterval(moveEvents, defaulCarouselInterval);
        }
    }
    else {
        if (pausedCarousel && !pausedCarouselByPopup && !startedCarousel && !stoppedCarousel) {
            pausedCarousel = false;
            startedCarousel = true;
            interval = setInterval(moveEvents, defaulCarouselInterval);
        }
    }
}

function pauseCarousel(byPopup) {
    clearInterval(interval);
    if (byPopup) {
        pausedCarouselByPopup = true;
    }
    else {
        pausedCarousel = true;
    }
    startedCarousel = false;
}

function saveScrollPosition() {
    scrollYOffset = $('.sub-menu').scrollTop();
}

function setScrollPosition() {
    $('.sub-menu').scrollTop(scrollYOffset);
}

function setActiveTab(index) {
    $(".top-menu a").removeClass("active");
    $(".top-menu a").eq(index - 1).addClass("active");
}
