﻿/*!
* Widget with items that bounce up/down on hovering, and periodically on their own.
*
* Copyright 2010, David Gill
* http://gilldave.co.uk
*/

var projects = { };

(function () {
    projects.show = function () {
        var project = $('.project');
        var loader = $('<div class="content-loading"></div>').hide().insertBefore(project);

        loader.fadeIn(500);
        project.stop(true, true).slideUp(function () {
            loader.delay(1000).fadeOut(250, function () {
                project.slideDown();
            });
        });
    }

    projects.gallery = function (gallery) {
        var belt = $(gallery + ' .belt');
        var items = belt.children();
        var itemWidth = items.outerWidth(true);
        belt.width((items.length + 1 /* +1 for pre IE8*/) * itemWidth);
        belt.hover(function () {
            $(this).stop(true);
        },
        function () {
            scrollGallery();
        });

        belt.find('a').click(function (event) {
            event.preventDefault();
            projects.show();
        });

        function scrollGallery() {
            var item = belt.children().first();
            var scrollAmount = item.children().outerWidth(true);
            var speedPercent = 1 - (-belt.position().left / scrollAmount);
            var scrollSpeed = 1000 + 4000 * speedPercent;
            belt.delay(2000).animate({ 'left': '-' + scrollAmount }, scrollSpeed, function () {
                item.detach().appendTo(belt);
                belt.css('left', 0);
                scrollGallery();
            });
        }

        scrollGallery();
    }
})();
