(function ($) {

    $.fn.slideshow = function (arrayOfImages, buttonLeft, buttonRight) {

        var options = {
            animationSpeed: 600
        };

        var $size = arrayOfImages.length;
        var $index = 0;
        var $ready = true;
        var $containerWidth = $(this).width();

        $mainContainer = $(this);

        arrayOfContent = new Array();
        for (var i = 0; i < arrayOfImages.length; i++) {
            arrayOfContent[i] = "<div style=\"float:left; width:" + $containerWidth + "px;\">" + arrayOfImages[i] + "</div>";
        }

        $(this).css("overflow", "hidden");
        $(this).css("position", "relative");

        $(this).append("<div id=\"sliderScroll_container\"></div>");
        $sc = $(this).find("#sliderScroll_container");
        $sc.css("position", "relative");
        $sc.css("top", "0px");
        $sc.css("left", "0px");
        $sc.css("width", $containerWidth * 2 + "px");
        $sc.html(arrayOfContent[$index]);

        buttonLeft.click(function () {
            $mainContainer.height($sc.children().outerHeight(true));

            if ($ready) {
                $ready = false;
                if ($index - 1 < 0) {
                    $index = $size - 1;
                } else {
                    $index--;
                }

                $sc.css("left", -$containerWidth);
                $sc.prepend(arrayOfContent[$index]);

                $sc.animate({
                    left: 0
                }, options.animationSpeed, function () {
                    $sc.html(arrayOfContent[$index]);
                    animateHeight();
                    $ready = true;
                });
            }
        });

        buttonRight.click(function () {
            $mainContainer.height($sc.children().outerHeight(true));
            if ($ready) {
                $ready = false;
                if ($index + 1 > $size - 1) {
                    $index = 0;
                } else {
                    $index++;
                }

                $sc.append(arrayOfContent[$index]);

                $sc.animate({
                    left: -$containerWidth
                }, options.animationSpeed, function () {
                    $sc.css("left", 0);
                    $sc.html(arrayOfContent[$index]);
                    animateHeight();
                    $ready = true;
                });
            }
        });

        function animateHeight() {
            $mainContainer.animate({
                height: $sc.children().outerHeight(true)
            }, options.animationSpeed);
        }

    };
})(jQuery);

