


var FeaturedPostsLib = this.FeaturedPostsLib || {};
FeaturedPostsLib.fps = FeaturedPostsLib.fps || {};

(function($j) {
    var type = 'none';
    var fps_animLocked = new Array(); // Lock object for animations
    var autoscrollInterval = new Array();

    /** Initialize jQuery based animations */
    FeaturedPostsLib.fps.init = function()
    {
        // determine if the single or triple flavor is in use
        if ($j('.fps-single').length != 0 && $j('.fps-single li').length > 1)
        {
            type = 'single';

            // hide all but first entry in featured posts list
            $j('.featured-posts-wrapper').each(function() {
               $j(this).find('.fps-text').slice(1).fadeOut();
               $j(this).find('ul.featured-posts li').slice(1).css('display','none');
            });      
        }

        // init animations
        initAutoscroll();

        // initialize the scroll and slide buttons
        initScrollButtons();
        initSlideNumbers();

        // release animation locks
        for (var i=1; i<=fps_animLocked.length; i++)
        {
            fps_animLocked[i-1] = false;
        }
    };


    /** Add click event handlers to scroll buttons */
    function initScrollButtons()
    {
        if (type != 'none')
        {
            $j('.scrollFeaturedPostsRight').each(function(index) {
                $j(this).click(function() {
                    if (fps_animLocked[index] == false)
                    {
                        FeaturedPostsLib.fps.scrollFeaturedPosts(this, 'right', index);
                    }
                    clearInterval(autoscrollInterval[index]);
                });
            });

            $j('.scrollFeaturedPostsLeft').each(function(index) {
                $j(this).click(function() {
                    if (fps_animLocked[index] == false)
                    {
                        FeaturedPostsLib.fps.scrollFeaturedPosts(this, 'left', index);
                    }
                    clearInterval(autoscrollInterval[index]);
                });
            });

            $j('.scrollFeaturedPostsRight-below').each(function(index) {
                $j(this).click(function() {
                    if (fps_animLocked[index] == false)
                    {
                        FeaturedPostsLib.fps.scrollFeaturedPosts(this, 'right', index);
                    }
                    clearInterval(autoscrollInterval[index]);
                });
            });

            $j('.scrollFeaturedPostsLeft-below').each(function(index) {
                $j(this).click(function() {
                    if (fps_animLocked[index] == false)
                    {
                        FeaturedPostsLib.fps.scrollFeaturedPosts(this, 'left', index);
                    }
                    clearInterval(autoscrollInterval[index]);
                });
            });
        }
        else
        {
            $j('.scrollFeaturedPostsRight').remove(); 
            $j('.scrollFeaturedPostsLeft').remove();
            $j('.featured-posts-wrapper ul').css('margin-left', '22px');
        }
    }


    function initSlideNumbers()
    {
        if (type != 'none')
        {
            $j('ul.fps-slideNumberList').each(function(index) {
                $j(this).children('li').click(function() {
                    if (fps_animLocked[index] == false)
                    {
                        scrollToPost(this, index);
                    }
                    clearInterval(autoscrollInterval[index]);
                });
            });
        }
    }


    function initAutoscroll()
    {
        if (type != 'none')
        {
            $j('.featured-posts-wrapper').each(function(index) {
                fps_animLocked[index] = true;
                
                if (1 == 1)
                {
                    if ($j('.featured-posts-wrapper').slice(index,index+1).children('.scrollFeaturedPostsRight').length > 0)
                    {
                        var callback = 
                            "FeaturedPostsLib.fps.scrollFeaturedPosts(jQuery('.featured-posts-wrapper').slice(" + 
                            index + "," + (index + 1) + ").children('.scrollFeaturedPostsRight'), 'right', " + index + ")";
                        autoscrollInterval[index] = setInterval(
                            callback, 7000);
                    }
                    else
                    {
                        var callback = 
                            "FeaturedPostsLib.fps.scrollFeaturedPosts(jQuery('.featured-posts-wrapper').slice(" + 
                            index + "," + (index + 1) + ").children('.scrollFeaturedPostsRight-below'), 'right', " + index + ")";
                        autoscrollInterval[index] = setInterval(
                            callback, 7000);
                    }
                }
            });
        }
    }


    function scrollToPost(slideButton, index)
    {
        if (!($j(slideButton).hasClass('fps-selectedSlide')))
        {
            // lock animations
            fps_animLocked[index] = true;

            var currentItem = $j(slideButton).parent().siblings('ul.featured-posts').children('li:visible');

            // get the next item to display
            var nextItemIndex = parseInt($j(slideButton).text());
            
            var nextItem = $j(slideButton).parent().siblings('ul.featured-posts').children('li').eq(nextItemIndex-1);

            setSelectedSlide(nextItem);
            animate(nextItem, currentItem, 'right', index)
        }
    }


    FeaturedPostsLib.fps.scrollFeaturedPosts = function(button, dir, index)
    {
        if (fps_animLocked[index] != true)
        {
            // lock animations
            fps_animLocked[index] = true;

            // get the currently displayed element(s)
            var currentItem = $j(button).siblings('ul.featured-posts').children('li:visible');

            var nextItem;

            if (dir == 'right')
            {
                nextItem = currentItem.next();

                if (nextItem.length == 0)
                {
                    nextItem = currentItem.siblings().first();
                }
            }
            else if (dir == 'left')
            {
                nextItem = currentItem.prev();

                if (nextItem.length == 0)
                {
                    nextItem = currentItem.siblings().last();
                }
            }

            setSelectedSlide(nextItem);
            animate(nextItem, currentItem, dir, index);
        }
    };


    function setSelectedSlide(toShow)
    {
        // Remove class from current slide
        $j(toShow).parent().siblings('ul.fps-slideNumberList').children('li.fps-selectedSlide').removeClass('fps-selectedSlide');

        // Get the index of the next item to be displayed
        var nextSlideIndex = (($j(toShow).index()));

        $j(toShow).parent().siblings('ul.fps-slideNumberList').children('li').eq(nextSlideIndex).addClass('fps-selectedSlide');
    }


    function animate(toShow, toHide, dir, index)
    {
        var shownWidth = toHide.width();

        // fade out text on currently displayed item
        $j(toHide).find('.fps-text').fadeOut(100, function() {
            // Make new item visible.
            toShow.css('display','');

            // Position the elements to animate based on direction
            if (dir == 'right')
            {
                toShow.css({float:'right'});
                toShow.css({width:'0px'});
                toHide.css({float:'left'});
            }
            else
            {
                toShow.css({float:'left'});
                toShow.css({width:'0px'});
                toHide.css({float:'right'});
            }

            toHide.animate({width:'0px'}, 1000, function() {
                toHide.css('display','none');
                toHide.css('width','');
                toShow.css('float','');
                toHide.css('float','');
                $j(toShow).find('.fps-text').fadeIn(200, function() {
                    fps_animLocked[index] = false;
                });
            });

            toShow.animate({width: shownWidth},1000);
        });
    }

}(jQuery))

jQuery(document).ready(FeaturedPostsLib.fps.init);