﻿jQuery(function($) {
    var slideshow = $('.home .slideshow img.slide');
    slideshow.hide();

    var runSlideshow = function(i, slides) {
        slideshow.fadeOut(750, function() {
            slideshow.attr('src', slides[i % slides.length]);
            slideshow.fadeIn(750);
        });

        setTimeout(function() { runSlideshow(i + 1, slides); }, 3500);
    }

    $.getJSON('/Home/HomepageImages',
        function(data) {
            // wait for first image to finish loading before beginning
            slideshow.load(function() {
                slideshow.unbind('load');
                slideshow.fadeIn(750, function() {
                    setTimeout(function() { runSlideshow(1, data); }, 3500);
                });

            });
            slideshow.attr('src', data[0]);

            // load other images            
            for (var i = 1; i < data.length; i++) {
                $('<img>').attr('src', data[i]);
            }
        }
    );

    var rotateTestimonials = function() {
        var quotes = $('.testimonial');

        // randomize startposition
        var index = Math.floor(Math.random() * (quotes.length + 1));

        var currentItem = $(quotes.get(index));

        // hide all at first
        for (var i = 0; i < quotes.length; i++) {
            if (i != index) {
                var item = $(quotes.get(i));
                if (item && item.is('div.testimonial')) {
                    item.hide();
                    item.css('left', '-1000px');
                }
            }
        }
        //        quotes.each(function() {
        //            if ((this) != currentItem) {
        //                $(this).css('display', 'none');
        //                $(this).css('left', '-1000px');
        //            }
        //        });

        // show first quote
        //        currentItem.css('display', 'block');
        //        currentItem.css('left', '0');

        var setupTimer = function() {
            timer = setInterval(function() {
                try {
                    var nextIndex = index + 1;
                    if (nextIndex >= quotes.length) {
                        nextIndex = 0;
                    }

                    var nextItem = $(quotes.get(nextIndex));

                    if (currentItem.is('div.testimonial') && nextItem.is('div.testimonial')) {
                        currentItem.animate({
                            left: '1000px'
                        }, 250,
                        function() {
                            currentItem.css('left', '-1000px');
                            currentItem.css('display', 'none');

                            index = nextIndex;
                            currentItem = nextItem;

                            currentItem.css('display', 'block');
                            currentItem.animate({
                                left: '0'
                            }, 250);
                        }
                    );
                    } else {
                        clearInterval(timer);
                        setupTimer();
                    }
                } catch (e) { }
            }, 10000);
        };

        setupTimer();
    }

    rotateTestimonials();

    $(window).unload(function() {
        clearInterval(timer);
    });
});