$(document).ready(
	function ()
	{
		// set up "feature" box
		var news_moving = 0;
		var clicked = false;
		var rotating = true;
		var switch_feature = function (l)
    		{
                l.blur();
                if (l.hasClass('active'))
                {
                    return;
                }
                if (news_moving > 0)
                {
                    return;
                }
                news_moving = 2;
                var prev_l = $('div.home-feature div.viewing a.active');
                prev_l.removeClass('active');
                l.addClass('active');
                // figure out if the animation is forward or backward
                var prev_li = prev_l.closest('li');
                var new_li = l.closest('li');
                var direction = prev_li.prevAll().index(new_li) == -1 ? 'forward' : 'backward';
                
                var cur_story = $('div.home-feature div.features div.active');
                var new_story = $('div.home-feature div.features '+l.attr('href'));
                
                var anim = {};
                anim.new_end = { left : '0px', opacity : 1.0 };
                if (direction == 'forward')
                {
                    anim.cur_end = { left : '-360px', opacity : 0.0 };
                    anim.new_start = { left : '360px', opacity : 0.0 };
                }
                else
                {
                    anim.cur_end = { left : '360px', opacity : 0.0 };
                    anim.new_start = { left : '-360px', opacity : 0.0 };
                }
    
                // make current story absolute so the other story takes
                // over the height of the container
                cur_story.css({ position : 'absolute' });
                new_story.show();
                // animation for hiding current story
                cur_story.animate(
                    anim.cur_end,
                    function ()
                    {
                        news_moving -= 1;
                        cur_story.removeClass('active').hide().css({ position : 'relative' });
                    });
                // animation for showing new story
                new_story.css(anim.new_start).animate(
                    anim.new_end,
                    function ()
                    {
                        news_moving -= 1;
                        new_story.addClass('active');
                    });
    		}
		$('div.home-feature div.viewing a').click(
			function (e)
			{
			    clicked = true;
			    if (timer)
			    {
			        window.clearTimeout(timer);
			    }
				e.preventDefault();
				switch_feature($(this));
			}
		);
		// start rotating
		var rotate_time = 7000;
		var timer;
		var rotate = function ()
    		{
		        if (clicked || !rotating)
		        {
		            return; // and don't reschedule
		        }
                // show the next feature and reschedule
                var cur_l = $('div.home-feature div.viewing a.active');
                var next = cur_l.closest('li').next();
                if (next.size())
                {
                    switch_feature(next.find('a'));
                }
                else
                {
                    switch_feature(cur_l.closest('li').siblings().slice(0,1).find('a'));
                }
                timer = window.setTimeout(rotate, rotate_time);
    		};
        $('div.home-feature').hover(
                function (e)
                {
                    rotating = false;
                },
                function (e)
                {
                    rotating = true;
                    if (!clicked)
                    {
                        window.clearTimeout(timer); 
                        timer = window.setTimeout(rotate, Math.round(rotate_time/2));
                    }
                }
            );
        timer = window.setTimeout(rotate, rotate_time);
	}
);