(function($){
    $.fn.extend({
        // Прокрутить tinyscrollbar({ axis : "x" })
        tinyscrollbar_scrollTo: function(pxLeft){
            return this.each(function(){
                var $this = $(this);
                var $thumb = $this.find(".thumb");
                $this.tinyscrollbar_update(Math.min(Math.max(0, (pxLeft - $thumb.width() / 2) / (1 + $thumb.width() / $this.width())), $this.find(".overview").width() - $this.width()));

                var $marker = $this.find(".marker");
                $marker.css("display", "block");
                $marker.css("left", pxLeft + "px");
            });
        },

        // Включить для страницы с .feed бесконечную прокрутку и синхронизировать с ней tinyscrollbar({ axis : "x" })
        useAsInfiniteScroll:    function(itemWidth){
            var executingInfiniteScroll = false;

            var initialFeedEnd = $(".initial-feed-end").offset().top;
            var skippedFeedItems = $(".skipped-feed-items").data("n");

            var $pagination = $(this);
            $pagination.css("position", "fixed");
            $pagination.css("bottom", "-" + $pagination.height() + "px");
            $pagination.after($("<div/>").css("height", $pagination.height() + "px"));
            $(window).bind("scroll", function(){
                if ($(window).scrollTop() + $(window).height() + 300 > initialFeedEnd)
                {
                    $pagination.stop();
                    $pagination.animate({ bottom: 0 });
                }
                else
                {
                    $pagination.stop();
                    $pagination.animate({ bottom: -$pagination.height() });
                }

                if (!executingInfiniteScroll && $(document).height() - $(window).scrollTop() - $(window).height() < 300)
                {
                    executingInfiniteScroll = true;
                    $.ajax({
                        url     :   document.location,
                        data    :   {"no-wrapper" : "1", "last-item-datetime" : $(".feed .feed-item:last").data("datetime")},
                        success :   function(html)
                        {
                            $(".feed").append(html);
                            executingInfiniteScroll = false;

                            // TODO: Перенести в другое место, чо-нибудь придумать и т.п.
                            $().handle_flowplayers();
                        },
                        error   :   function(html)
                        {
                            // :(
                            executingInfiniteScroll = false;
                        }
                    });
                }


                $(".feed-item").each(function(i){
                    if ($(this).offset().top > $(window).scrollTop())
                    {
                        $pagination.tinyscrollbar_scrollTo(Math.round(itemWidth * (skippedFeedItems + i)));
                        return false;
                    }
                });
            });
        }
        
    });
})(jQuery);

$(function(){
    var $pagination = $(".pagination");
    if ($pagination.length)
    {
        $pagination.tinyscrollbar({ axis : "x"});
        var $a_active = $pagination.find("a.active");
        if ($a_active.length)
        {
            $pagination.tinyscrollbar_scrollTo($a_active.offset().left);
        }
        $pagination.useAsInfiniteScroll(document.location.toString().indexOf("http://thelogin.ru/gallery/") == 0 ? 0.925 : 3.7);
        $(".page-footer").remove();
    }
});

