(function ($) {
    $.fn.featureBanner = function () {
        return this.each(function () {
            var $wrapper = $('.fb-wrapper', this),
                $slider = $wrapper.find('> ul'),
                $items = $slider.find('> li'),
                $single = $items.filter(':first'),
                $nav = $('.fb-nav', this).css('display', 'block'),
                $navList = $nav.find('> ul'),
                $navItems = $navList.find('> li'),
                singleWidth = $single.outerWidth(),
                currentPage = (fbCurrent ? (fbCurrent + 1) : 1),
                pages = $items.length;
            $slider.css('width', singleWidth * ($items.length + 2));
            $navList.css('width', $navItems.length * 12);
            $items.filter(':first').before($items.slice(-1).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, 1).clone().addClass('cloned'));
            $items = $slider.find('> li');
            $wrapper.scrollLeft(singleWidth * currentPage);

            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * n;
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function () {
                    if (page == 0) {
                        $wrapper.scrollLeft(singleWidth * pages);
                        page = pages;
                    } else if (page > pages) {
                        $wrapper.scrollLeft(singleWidth);
                        page = 1;
                    }
                    $($navItems[(currentPage - 1)]).find('> a').removeClass('fb-nav-active');
                    currentPage = page;
                    $($navItems[(currentPage - 1)]).find('> a').addClass('fb-nav-active');
                });
                return false;
            };

            function showNavTip(event) {
                var el = $(this),
                    banner = el.attr('href'),
                    position = el.offset(),
                    thPath = fbBanners[0][(banner - 1)].path
                    thAlt = fbBanners[0][(banner - 1)].alt,
                    tipEl = $('.fb-nav-tip').css({
                        visibility: 'hidden',
                        display: 'block'
                    }),
                    tipPosition = tipEl.offset();
                tipEl.css({
                    top: ((position.top - tipPosition.top) - 80) + 'px',
                    left: ((position.left - tipPosition.left) - 90) + 'px'
                }).html('<img src="' + thPath + '" width="150" />');
                tipEl.css({
                    visibility: 'visible'
                });
            }

            function hideNavTip(event) {
                tipEl = $('.fb-nav-tip');
                tipEl.css({
                    display: 'none',
                    top: '0px',
                    left: '0px'
                });
            }
            $('a', $nav).click(function (event) {
                event.preventDefault();
                return gotoPage(+$(this).attr('href'));
            });
            $('a', $nav).hover(showNavTip, hideNavTip);
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);
$(document).ready(function () {
    $('.feature-banner').each(function (index, el) {
        var item, i, current = (fbCurrent ? fbCurrent : 0),
            wrapper = $(el).find('> .fb-wrapper > ul'),
            toPrepend = '';
        for (i = 0; i < fbBanners[index].length; i++) {
            if (i !== current) {
                item = '<img src="' + fbBanners[index][i]['path'] + '" alt="' + fbBanners[index][i]['alt'] + '" />';
                if (fbBanners[index][i]['url'] && fbBanners[index][i]['url'] !== '') {
                	item = '<a href="' + fbBanners[index][i]['url'] + '">' + item + '</a>';
                }
                item = '<li>' + item + '</li>';
                if (i < current) {
                    toPrepend += item;
                } else {
                    wrapper.append(item);
                }
            } else {
                if (toPrepend !== '') {
                    wrapper.prepend(toPrepend);
                }
            }
        }
    });
    var autoscrolling = true;
    $('.feature-banner').featureBanner().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });

    setTimeout(function () {
        setInterval(function () {
            if (autoscrolling) {
                $('.feature-banner').trigger('next');
            }
        }, fbDelay);
    }, 2000);
});
