﻿/*Thank you atari.com / FI for the inspiration, hey at least I re-wrote it*/

$(document).ready(function () {

    var slideshow = (function () {

        var that = this;

        var options = {
            slides: '.slide',
            thumbs: '#slideThumbs li',
            scrollbar: '#slideScroll',
            handle: '#handle',
            delay: 8000,
            fadeTime: 600,
            easing: "easeInOutQuint",
            speed: 1000
        }

        var slides = $(options.slides),
            thumbs = $(options.thumbs),
            scrollBar = $(options.scrollbar),
            handle = $(options.handle),
            handleY,
            mouseY,
            slideInterval,
            currentSlide = 0,
            nextSlide = 1,
            activeThumb = 0,
            thumbHeight,
            isAnimating = false;

        that.init = function () {
            var _this;
            thumbHeight = thumbs.eq(0).height() + parseFloat(thumbs.eq(0).css('marginBottom'));
            adjustHandleHeight();
            thumbs.parents('ul').css({ 'height': thumbs.length * thumbHeight + 'px' }).end().each(function () {
                _this = $(this)
                _this.css({ 'top': thumbHeight * _this.index() + 'px' }).attr('class', _this.attr('class') + " " + _this.index());
                if (_this.index() === thumbs.length - 1) {
                    slides.eq(0).animate({ 'left': '4px' }, options.speed, options.easing);
                    thumbAdjust(0);
                    run();
                }
            });
        }

        function run() {
            slideInterval = setInterval(function () {
                activeThumb = 0;
                slideAdjust(currentSlide);
                thumbAdjust(currentSlide);
                checkIndex();
            }, options.delay);
        }

        function slideAdjust() {
            isAnimating = true;
            slides.eq(currentSlide).stop(true, true).animate({ 'left': '-620px' }, options.speed, options.easing, function () {
                $(this).css({ 'left': '620px' });
            });
            slides.eq(nextSlide).stop(true, true).animate({ 'left': '4px' }, options.speed, options.easing);
        }

        function thumbAdjust() {
            thumbs.eq(activeThumb).stop(true, true).animate({ 'marginLeft': '-316px', 'height': '0px' }, options.speed, options.easing, function () {
                $(this).css({ 'height': '114px' }).animate({ 'marginLeft': '0px' }, options.speed, options.easing);
                thumbs.eq(activeThumb).appendTo('#slideThumbs ul');
                thumbs = $(options.thumbs);
                isAnimating = false;
            });
        }

        thumbs.click(function () {
            if (!isAnimating) {
                isAnimating = true;
                clearInterval(slideInterval);
                activeThumb = $(this).index();
                nextSlide = parseFloat($(this).attr('class'));
                slideAdjust(currentSlide);
                thumbAdjust(currentSlide);
                checkIndex();
                run();
            }
            return false;
        });

        scrollBar.bind('mousedown', function (e) {

            if (!($(e.target).attr('id') === handle.attr('id'))) {

                handle.css({ 'top': e.clientY - scrollBar.offset().top - (handle.height() * 0.5) });

            } else {
                $(document).bind('mousemove', scrollThumbs);
                $(document).bind('mouseup', handleReleased);
                $('body').addClass('clicked');
            }
            mouseY = e.clientY - handle.offset().top;
            scrollThumbs(e);

            return false;
        });

        function scrollThumbs(e) {
            handleY = Math.max(Math.min(e.clientY - mouseY, scrollBar.height() - handle.height()), 0);
            handle.css('top', handleY + 'px');
            $('#slideThumbs ul').css({ 'margin-top': "-" + handle.css('top') });

        }

        function handleReleased() {
            $(document).unbind('mousemove', scrollThumbs);
            $(document).unbind('mouseup', handleReleased);
            $('body').removeClass('clicked');
        }

        function adjustHandleHeight() {
            var handleHeight = Math.max(scrollBar.height() - (thumbHeight * (thumbs.length-1) - scrollBar.height()), 40);
            handle.css({ 'height': handleHeight + 'px' });
        }

        function checkIndex() {
            if (nextSlide >= slides.length - 1) {
                currentSlide = nextSlide;
                nextSlide = 0;
            } else {
                currentSlide = nextSlide;
                nextSlide++;
            }
        }

        return that;

    })();

    slideshow.init();

});




