$(document).ready(function () {
    var boxes = $('.app\\/home\\/featured-stories-combined .rotator');
    var hero = boxes.find('.featured-box.hero');
    var moment = boxes.find('.featured-box.moment');
    var buttons = $('.app\\/home\\/featured-stories-combined .rotator .switcher a');
    var hero_button = buttons.filter('.hero-button');
    var moment_button = buttons.filter('.moment-button');
    var rotating = true;
    var animating = false;
    var rotate_time = 5000;
    var rotate_duration = 500;
    var active;
    var timer;
    if (hero.hasClass('active-box'))
    {
        active = hero;
    }
    else
    {
        active = moment;
    }
    var switch_to = function (new_box) {
        if (animating)
        {
            return;
        }
        if (new_box == active)
        {
            return;
        }
        var old_box;
        var new_box;
        var old_button;
        var new_button;
        if (new_box == hero)
        {
            old_box = moment;
            old_button = moment_button;
            new_box = hero;
            new_button = hero_button;
        }
        else
        {
            old_box = hero;
            old_button = hero_button;
            new_box = moment;
            new_button = moment_button;
        }
        animating = true;
        old_button.removeClass('active');
        new_button.addClass('active');
        old_box.animate({opacity:0.0}, rotate_duration, function () { 
            new_box.addClass('active-box');
            old_box.removeClass('active-box');
            old_box.css({opacity:1.0});
            active = new_box;
            animating = false;
            if (rotating)
            {
                window.clearTimeout(timer);
                timer = window.setTimeout(rotate, rotate_time);
            }
        });
    }
    var rotate = function () {
        if (!rotating)
        {
            return; // and don't reschedule
        }
        if (active == hero)
        {
            switch_to(moment);
        }
        else
        {
            switch_to(hero);
        }
    };
    timer = window.setTimeout(rotate, rotate_time);
    boxes.hover(
        function () {
            rotating = false;
            window.clearTimeout(timer);
        },
        function () {
            rotating = true;
            timer = window.setTimeout(rotate, rotate_time/2);
        });
    buttons.click(function (e) {
        if ($(this).hasClass('hero-button'))
        {
            switch_to(hero);
        }
        else
        {
            switch_to(moment);
        }
    });
    

    // news
    var news_boxes = $('.app\\/home\\/featured-stories-combined .latest-news');
    var stories = news_boxes.find('.story')
    var news_buttons = $('.app\\/home\\/featured-stories-combined .latest-news .switcher a');
    var news_rotating = true;
    var news_animating = false;
    var news_rotate_time = 5000;
    var news_rotate_duration = 500;
    var news_active;
    var news_timer;
    news_active = 1; 
    var news_switch_to = function (num) {
        if (news_animating)
        {
            return;
        }
        if (num == news_active)
        {
            return;
        }
        var old_box;
        var new_box;
        var old_button;
        var new_button;
        old_box = $(stories.get((news_active-1)))
        old_button = $(news_buttons.get((news_active-1)))
        new_box = $(stories.get((num-1)))
        new_button = $(news_buttons.get((num-1)))
        news_animating = true;
        old_button.removeClass('active');
        new_button.addClass('active');
        new_box.addClass('next-active-box');
        old_box.animate({opacity:0.0}, news_rotate_duration, function () { 
            new_box.addClass('active-box'); 
            new_box.removeClass('next-active-box');
            old_box.removeClass('active-box');
            old_box.css({opacity:1.0});
            news_active = num;
            news_animating = false;
            if (news_rotating)
            {
                window.clearTimeout(news_timer);
                news_timer = window.setTimeout(news_rotate, news_rotate_time);
            }
        });
    }
    var news_rotate = function () {
        if (!news_rotating)
        {
            return; // and don't reschedule
        }
        var num = news_active+1
        if (num > stories.size())
        {
            num = 1
        }
        news_switch_to(num)
    };
    news_timer = window.setTimeout(news_rotate, news_rotate_time);
    news_boxes.hover(
        function () {
            news_rotating = false;
            window.clearTimeout(news_timer);
        },
        function () {
            news_rotating = true;
            news_timer = window.setTimeout(news_rotate, news_rotate_time/2);
        });
    news_buttons.click(function (e) {
        e.preventDefault()
        var num = news_buttons.index($(this))+1
        news_switch_to(num)
    });
});
