$(document).ready(function () {

    $('#testimonials .quote').hide();
    
    var container = $('#testimonials .quotes>.inner')
    var last_i = -1;
    
    function show_random_quote(animate)
    {
        if (!container.size())
        {
            return;
        }
        if (!window.Jasmine || !window.Jasmine.quotes)
        {
            window.setTimeout(function () { show_random_quote(animate); }, 100);
            return;
        }
        var quotes = window.Jasmine.quotes;
        var new_i;
        do {
            new_i = Math.floor(Math.random()*quotes.length);
        } while (new_i == last_i);
        var quote = quotes[new_i];
        if (!animate)
        {
            container.find('.visible .text').text(quote.quote);
            container.find('.visible .source').text(quote.source);
            container.find('.visible').show();
        }
        else
        {
            var oldq = container.find('.visible');
            var newq = oldq.clone();
            newq.find('.text').text(quote.quote);
            newq.find('.source').text(quote.source);
            newq.css({'opacity':0}).show();
            newq.appendTo(container);
            oldq.animate({'opacity':0}, 500, function () {
                oldq.remove();
                newq.animate({'opacity':1.0}, 500, function () {
                    $(this).css('filter', '');
                });
            });
        }
    }
    
    function show_random_quote_animate()
    {
        show_random_quote(true);
    }
    
    if ($('body').hasClass('home'))
    {
        // rotating quotes
        show_random_quote();
        window.setInterval(show_random_quote_animate, 6000);
    }
    else
    {
        // just set a random quote
        show_random_quote();
    }
    
    // quick quote box
    $('.box-quote').each(function () {
        var container = $(this);
        var steps = $(this).find('.step');
        var cur_step = 0;
        container.find('.button input').click(function (e) {
            var this_step = $(this).closest('.step');
            var step_i = steps.index(this_step);
            if (step_i + 1 >= steps.size())
            {
                // let the form submit
                return;
            }
            // show the next step
            e.preventDefault();
            var next_step = steps.eq(step_i+1);
            next_step.css({opacity:0});
            this_step.animate({opacity:0}, 250, function () {
                this_step.hide();
                next_step.show().animate({opacity:1.0}, 500, function () {
                    $(this).css('filter', '');
                });
            });
        });
        container.find('.back a').click(function (e) {
            e.preventDefault();
            var this_step = $(this).closest('.step');
            var step_i = steps.index(this_step);
            if (step_i < 1)
            {
                // do nothing; shouldn't ever happen
                return;
            }
            // show the previous step
            var prev_step = steps.eq(step_i-1);
            prev_step.css({opacity:0});
            this_step.animate({opacity:0}, 250, function () {
                this_step.hide();
                prev_step.show().animate({opacity:1.0}, 500, function () {
                    $(this).css('filter', '');
                });
            });
        });
    });
    
    /* article scroller */
    
    var $articles = $('.article-scroll .article-link');
    $articles.hide();
    var articles = [];
    $articles.each(function () {
        var l = $(this).find('a').attr('href');
        if (window.location.href.indexOf(l) == -1)
        {
            articles.push($(this).get(0));
        }
    });
    articles.sort(function () { return 0.5 - Math.random(); });
    $(articles[0]).show();
    var cur_article = 0;
    var article_animating = false;
    var article_timer = false;
    var article_pause = 5000;
    function rotate_article()
    {
        if (article_animating)
        {
            return;
        }
        article_animating = true;
        var old_article = cur_article;
        cur_article = old_article + 1;
        if (cur_article >= articles.length)
        {
            cur_article = 0;
        }
        $(articles[old_article]).fadeOut(function () {
            $(articles[cur_article]).fadeIn(function () { article_animating = false; });
        });
    }
    $('.article-scroll').hover(function () {
        if (article_timer)
        {
            window.clearInterval(article_timer);
        }
    }, function () {
        article_timer = window.setInterval(rotate_article, article_pause);
    });
    article_timer = window.setInterval(rotate_article, article_pause);
});
