
(function($) {

    var LetItSlide = function(node, options) {
        var
        forwardButton = node.find(options.forwardSelector),
        backButton = node.find(options.backSelector),
        imageContainer = node.find(options.imageContainerSelector),
        images = imageContainer.find("img"),
        tooltipWrapperSelector = options.tooltipWrapperSelector,
        tooltipSelector = options.tooltipSelector,
        tooltips = node.find(tooltipWrapperSelector),
        tooltipsContainer = tooltips.parent(),
        numImages = images.length,
        imageWidth = options.imageWidth,
        slideshowInterval = options.slideshowInterval,
        slideshowIntervalRef,
        activeTooltip,

        back = function() {
            unbindButtons();
            var containerLeft = imageContainer.css("left").replace(/px/, "");
            var lastElement = imageContainer.find(":last").remove();
            imageContainer.prepend(lastElement);
            imageContainer.css("left", (containerLeft - imageWidth) + "px");
            imageContainer.animate({left: (containerLeft) + "px"}, {
                duration: 500,
                queue:false,
                easing: "easeinout"
            });
            
            var tooltipLeft = tooltipsContainer.css("left").replace(/px/, "");
            var firstElement = tooltipsContainer.find(tooltipWrapperSelector + ":first");

            tooltipsContainer.animate({left: (tooltipLeft - imageWidth) + "px"}, {
                duration: 500,
                queue:false,
                easing: "easeinout",
                complete: function() {
                    firstElement.remove();
                    tooltipsContainer.append(firstElement);
                    tooltipsContainer.css("left", tooltipLeft + "px");
                    bindButtons();
                }
            });

            tooltipsContainer.find(tooltipWrapperSelector).eq(1).animate({delay: 1}, 300, "easein", function() {
                $(this).fadeTo(200, 0);
            });
            tooltipsContainer.find(tooltipWrapperSelector).eq(1).find(tooltipSelector).removeClass("active-tooltip");
            tooltipsContainer.find(tooltipWrapperSelector).eq(2).find(tooltipSelector).addClass("active-tooltip");

            tooltipsContainer.find(tooltipWrapperSelector).eq(2).fadeTo(400, 1);
        },

        forward = function() {
            unbindButtons();
            
            var containerLeft = imageContainer.css("left").replace(/px/, "");
            var firstElement = imageContainer.find(":first");
            
            imageContainer.animate({left: (containerLeft - imageWidth) + "px"}, {
                duration: 500,
                queue:false,
                easing: "easeinout",
                complete: function() {
                    firstElement.remove();
                    imageContainer.append(firstElement);
                    imageContainer.css("left", containerLeft + "px");
                }
            });

            var tooltipLeft = tooltipsContainer.css("left").replace(/px/, "");
            var lastElement = tooltipsContainer.find(tooltipWrapperSelector + ":last").remove();
            tooltipsContainer.prepend(lastElement);
            tooltipsContainer.css("left", (tooltipLeft - imageWidth) + "px");

            tooltipsContainer.animate({left: (tooltipLeft) + "px"}, {
                duration: 500,
                queue:false,
                easing: "easeinout",
                complete: function() {
                    bindButtons();
                }
            });
            
            tooltipsContainer.find(tooltipWrapperSelector).eq(2).animate({delay: 1}, 300, "easein", function() {
                $(this).fadeTo(200, 0);
            });
            activeTooltip = tooltipsContainer.find(tooltipWrapperSelector).eq(1);
            activeTooltip.fadeTo(400, 1);
            activeTooltip.find(tooltipSelector).addClass("active-tooltip");
            tooltipsContainer.find(tooltipWrapperSelector).eq(2).find(tooltipSelector).removeClass("active-tooltip");
        },

        bindButtons = function() {
            backButton.bind("click", handleClearSlideshowInterval);
            forwardButton.bind("click", handleClearSlideshowInterval);
            forwardButton.bind("click", forward);
            backButton.bind("click", back);
        },

        unbindButtons = function() {
            forwardButton.unbind("click");
            backButton.unbind("click");
        },

        handleSlideshowTick = function() {
            forward();
            backButton.bind("click", handleClearSlideshowInterval);
            forwardButton.bind("click", handleClearSlideshowInterval);
        },

        handleClearSlideshowInterval = function() {
            clearInterval(slideshowIntervalRef);
            slideshowIntervalRef = null;
        },

        handleMouseEnterActiveTooltip = function() {
            if(slideshowIntervalRef != null) {
                handleClearSlideshowInterval();

                $(".active-tooltip").one("mouseleave", function() {
                    slideshowIntervalRef = setInterval(handleSlideshowTick, slideshowInterval);    
                });
            }
        },

        init = function() {
            imageContainer.empty();
            images.each(function() {
                imageContainer.append(this);
            });

            imageContainer.css("width", (imageWidth * numImages) + "px");
            tooltipsContainer.css("width", (imageWidth * numImages) + "px");
            slideshowIntervalRef = setInterval(handleSlideshowTick, slideshowInterval);

            tooltipsContainer.find(tooltipWrapperSelector).css("display", "block");
            tooltips.fadeTo(0, 0);
            activeTooltip = tooltips.eq(1);
            activeTooltip.find(tooltipSelector).addClass("active-tooltip");
            activeTooltip.fadeTo(600, 1);
            bindButtons();

            $(".active-tooltip").live("mouseenter", handleMouseEnterActiveTooltip);
        }
        ;

        init();
    };


    $.fn.letItSlide = function(options){
        var opts = $.extend({}, $.fn.letItSlide.defaults, options);
        
        return new LetItSlide(this, opts);
    };

    $.fn.letItSlide.defaults = {
        slideshowInterval : 9000,
        forwardSelector : '.forward',
        backSelector : '.back',
        imageContainerSelector : '.images',
        tooltipWrapperSelector : '.tooltip-wrapper',
        tooltipSelector : '.tooltip',
        imageWidth : 960
    };
    
})(jQuery);
