
$(document).ready(function() {
    if (isHomepage()) {
        highlightIndex(0);
        setInterval(doTimer, 4000);
    }
    var index = 0;
    var lock = 0;
    var isDoneHighlighting = 1;
    
    function highlightIndex(index) {
        isDoneHighlighting = 0;
        $(".category_container").removeAttr('style');
        var category = $(".category_container:eq(" + index + ")");
        category.css('background-color', '#f3f3f3');
        if (isHomepage()) {
            var position = category.position();
            $("#category_triangle").show().css({'position': 'absolute'
                , 'left': position.left + 25
                , 'top': position.top - 11
            });

            var image = category.attr('header-image');
            $("#viewer").css('background-image', 'url("' + image + '")');
            isDoneHighlighting = 1;
        } else {
            isDoneHighlighting = 1;
        }
    }
    
    function doTimer() {
        if (isDoneHighlighting == 0) {
            return;
        }
        if (lock == 0) {
            index++;
            if (index > 5) {
                index = 0;
            }
            highlightIndex(index);
        }
    }
    
    $(".category_container").hover(function() {
        if (isDoneHighlighting == 0) {
            return;
        }
        if (lock == 0) {
            index = $(".category_container").index($(this));
            highlightIndex(index);
            lock = 1;
        }
    }, function() {
        lock = 0;
    });
    
});

