jQuery.fn.bannerscroll = function (options) {
    options = jQuery.extend({
        speed: 2000,
        delay: 3000,
        direction: 'horizontal'
    }, options);

    $(this).each(function () {

        var theSpeed = options.speed;
        var theDelay = options.delay;

        
        var run = true;

        var theBanner = $(this).find(".items").eq(0);

        var slides = $(this).find(".slides").eq(0);

        var theTimeout = null;

        var runOnce = false;

        var currentSlide = null;

        if (theBanner.find("li").size() <= 4) {
            theSpeed = 500;
            theDelay = 5000;
        }


        slides.find("li").click(function () {
            $(this).find("a").each(function () {
                window.location = $(this).attr("href");
            });
        });

        /*$(this).mouseenter(function () {
        run = false;
        }).mouseleave(function () {
        run = true;
        clearTimeout(theTimeout);
        theTimeout = setTimeout(function () { scrollTo(); }, options.delay);
        });*/


        var positionItems = function () {
            var startPos = 0;
            // create a buffer

            theBanner.find("li").each(function () {
                var currElement = $(this);
                currElement.data("slide", currElement.index());
                if (theBanner.find("li").size() > 4) {
                    theBanner.append($(this).clone(true));
                }
            });

            // position all items
            theBanner.find("li").eq(0).addClass("selected");
            if (options.direction == 'vertical') {
                theBanner.find("li").each(function () {
                    $(this).css("top", startPos + "px");
                    startPos += $(this).outerHeight(true);
                });
            } else if (options.direction == 'horizontal') {
                theBanner.find("li").each(function () {
                    $(this).css("left", startPos + "px");
                    startPos += $(this).outerWidth(true);
                });
            }
            theBanner.find("li").click(function () {
                if (currentSlide.index() != $(this).data("slide")) {
                    clearTimeout(theTimeout);
                    if (!runOnce) {
                        run = false; runOnce = true; scrollTo($(this));
                    }
                }
            });
            currentSlide = slides.find("li").eq(0);
            slides.find("li").each(function () { $(this).css("display", "none"); });
            currentSlide.css("display", "block").css("z-index", "2");
        }

        var scrollTo = function (item) {
            if (item == null) {
                // if no item specified, scroll to next in list
                item = theBanner.find("li").eq(1);
                var posTop1 = item.position().top;
                var posLeft1 = item.position().left;
            }
            if (run || runOnce) {
                if (options.direction == 'vertical') {
                    theBanner.find("li").each(function () {
                        var toslide = 0;
                        if (theBanner.find("li").size() > 4) {
                            toslide = item.position().top;
                        }
                        $(this).animate(
                        {
                            top: '-=' + toslide
                        },
                        theSpeed,
                        function () {
                            if (theBanner.find("li:last").index() == $(this).index()) {
                                theBanner.find("li").each(function () {
                                    if ($(this).offset().top <= (-$(this).outerHeight(true))) {
                                        var itemToMove = $(this).detach();
                                        var posTop = theBanner.find("li:last").position().top + theBanner.find("li:last").outerHeight(true);
                                        theBanner.append(itemToMove);
                                        itemToMove.css("top", posTop + "px");
                                    }
                                    $(this).removeClass("selected");
                                });
                                item.addClass("selected");
                                var slideToShow = slides.find("li").eq(item.data("slide"));
                                slideToShow.css("z-index", "1").css("display", "block");
                                currentSlide.fadeOut(200, function () {
                                    slideToShow.css("z-index", "2");
                                    currentSlide.css("z-index", "1").css("display", "none");
                                    currentSlide = slideToShow;
                                });
                                if (runOnce) {
                                    runOnce = false;
                                }
                                if (run) {
                                    clearTimeout(theTimeout);
                                    var itemToScrollTo = null;
                                    if (theBanner.find("li").size() <= 4) {
                                        if (item.index() < theBanner.find("li").size() - 1) {
                                            itemToScrollTo = theBanner.find("li").eq(item.index() + 1);
                                        } else {
                                            itemToScrollTo = theBanner.find("li").eq(0);
                                        }
                                    }
                                    theTimeout = setTimeout(function () { scrollTo(itemToScrollTo); }, theDelay);
                                }
                            }
                        });
                    });
                } else if (options.direction == 'horizontal') {
                    theBanner.find("li").each(function () {
                        var toslide = 0;
                        if (theBanner.find("li").size() > 4) {
                            toslide = item.position().left;
                        }
                        $(this).animate(
                        {
                            left: '-=' + toslide
                        },
                        theSpeed,
                        function () {
                            if (theBanner.find("li:last").index() == $(this).index()) {
                                theBanner.find("li").each(function () {
                                    if ($(this).position().left <= (-$(this).outerWidth(true))) {
                                        var itemToMove = $(this).detach();
                                        var posLeft = theBanner.find("li:last").position().left + theBanner.find("li:last").outerWidth(true);
                                        theBanner.append(itemToMove);
                                        itemToMove.css("left", posLeft + "px");
                                    }
                                    $(this).removeClass("selected");
                                });
                                item.addClass("selected");
                                var slideToShow = slides.find("li").eq(item.data("slide"));
                                slideToShow.css("z-index", "1").css("display", "block");
                                currentSlide.fadeOut(200, function () {
                                    slideToShow.css("z-index", "2");
                                    currentSlide.css("z-index", "1").css("display", "none");
                                    currentSlide = slideToShow;
                                });
                                if (runOnce) {
                                    runOnce = false;
                                }
                                if (run) {
                                    clearTimeout(theTimeout);
                                    var itemToScrollTo = null;
                                    if (theBanner.find("li").size() <= 4) {
                                        if (item.index() < theBanner.find("li").size() - 1) {
                                            itemToScrollTo = theBanner.find("li").eq(item.index() + 1);
                                        } else {
                                            itemToScrollTo = theBanner.find("li").eq(0);
                                        }
                                    }
                                    theTimeout = setTimeout(function () { scrollTo(itemToScrollTo); }, theDelay);
                                }
                            }
                        });
                    });
                }
            }
        };

        positionItems();
        clearTimeout(theTimeout);
        theTimeout = setTimeout(function () { scrollTo(null); }, theDelay);
    });
};
