var slideshow = {
    
    auto: true,
    direction: "forward",

    init: function(settings) {
        slideshow.config = {
            $images: $(".slideshow img"),
            params: {height: 100, paddingTop: 10, paddingBottom: 10},
            interval: 3000
        }
        $.extend(slideshow.config, settings);	
        if (slideshow.getImages().length <= 1) return;
        slideshow.getImages().parent().addClass("loading");
        slideshow.getImages().hide();
        slideshow.start();
    },
    getImages: function() {
        return slideshow.config.$images;
    },
    loop: function() {
	if (!slideshow.auto) return;
        slideshow.blend();
        setTimeout(slideshow.loop, slideshow.config.interval);
    },
    blend: function() {
        var current = slideshow.getImages().filter(".current").fadeOut("slow").removeClass("current");
        if (slideshow.direction == "forward") {
            current = (current.next().length) ? current.next() : slideshow.getImages().first();
        }
        if (slideshow.direction == "back") {
            current = (current.prev().length) ? current.prev() : slideshow.getImages().last();
        }
        current.fadeIn("slow").addClass("current");
    },
    start: function() {
        slideshow.getImages().hide();
        slideshow.getImages().parent().animate(slideshow.config.params, "fast");
        slideshow.getImages().parent().removeClass("loading");
        slideshow.getImages().first().fadeIn("slow", function() {
            setTimeout(slideshow.loop, slideshow.config.interval);
        }).addClass("current");
       
        // Pause ein und aus mit Mouseover und Mauseleave (Select anpassen!).
        $('.bannershow img').mouseover(function() {
            slideshow.getImages().stop(true, true);
            slideshow.auto = false;
        });
        $('.bannershow img').mouseleave(function() {
            slideshow.auto = true;
            slideshow.loop();
        });
    
        $(".controls a.start-stop").click(function(e) {
            e.preventDefault();
            slideshow.getImages().stop(true, true);
            slideshow.auto = slideshow.auto ? false : true;
            slideshow.direction = "forward";
            if (slideshow.auto) {
                slideshow.loop(); 
                $(this).html("Stop"); 
            }
            else { 
                $(this).html("Play"); 
            }
        });

        $(".controls a.forward").click(function(e) {
            e.preventDefault();
            slideshow.getImages().stop(true, true);
            slideshow.auto = false;
            slideshow.direction = "forward";
            $(this).parent().find("a.start-stop").html("Play");
            slideshow.blend();
        });

        $(".controls a.back").click(function(e) {
            e.preventDefault();
            slideshow.getImages().stop(true, true);
            slideshow.auto = false;
            slideshow.direction = "back";
            $(this).parent().find("a.start-stop").html("Play");
            slideshow.blend();
        });
    }
}

$(document).ready(function() {
    slideshow.init({ 
        $images: $(".bannershow a"),
        params: {height: 98, paddingTop: 6, paddingBottom: 6},
        interval: 4000
    });
});




