;(function($){

    $.fn.hoverSlide = function(options){

        return this.each(function(){

            var settings = $.extend({}, $.fn.hoverSlide.defaults, options),
                $slider = $(this),
                $itemContainer = $('<div />'),
                $slides = $slider.find(settings.itemClass),
                totalWidth = 0,
                cursorPosition = {},
                hoverInterval,
                isHover = false;


            function init()
            {
                settings.originalSpeed = settings.speed;
                settings.speed = Math.max(1, 100 - (settings.speed*10) );

        		// Remove the margin-right on the last panel
                $(settings.itemClass + ':last').css('margin-right', 0);

                // calculate total items width
                marginRight = parseInt($(settings.itemClass).css('margin-right').replace('px', ''));
                $(settings.itemClass + ' img').each(function(){
                    totalWidth += ($(this).width() + marginRight);
                });

                if ( marginRight != 0 ) {
                    totalWidth -= marginRight;
                }

                // Set the container width to allow the panels to flow left
                $itemContainer.css('width', totalWidth);

                // Wrap an extra div around the panels
                $slides.wrapAll($itemContainer);

                // Hide the excess panels
                $slider.css('overflow', 'hidden');

                if ( settings.hoverSlide ) {
                    hoverSlide();
                }

                if ( settings.autoSlide ) {
                    autoSlide();
                }

            }

            // Update cursor position on mouseMove
            $(document).mousemove(function(e){
                cursorPosition = {
                    'x': e.pageX,
                    'y': e.pageY
                };
            });

            function hoverSlide()
            {
                // MouseEnter / mouseLeave events
                $slider.mouseenter(function(e) {
                    hoverInterval = window.setInterval(function(){
                        isHover = true;
                        var speed = (getCursorOffset($slider).x - ($slider.width() / 2)) / settings.speed;
                        if ( speed < -2 || speed > 2 ) {
                            $slider.scrollLeft($slider.scrollLeft() + speed);
                        }
                    }, 50);
                }).mouseleave(function(e){
                    isHover = false;
                    clearInterval(hoverInterval);
                });
            }

            function autoSlide()
            {
                var ltr = 1;
                window.setInterval(function(){
                    if ( !isHover ) {
                        // Determine direction
                        if ( ltr == 1 && $slider.scrollLeft() >= totalWidth - $slider.width() ) {
                            ltr = -1;
                        } else if ( ltr == -1 && $slider.scrollLeft() <= 0 ) {
                            ltr = 1;
                        }
                        $slider.scrollLeft($slider.scrollLeft() + (settings.originalSpeed / 2) * ltr);
                    }
                }, 50);
            }

            // Retrieves the cursor offset relative to $element
            function getCursorOffset($element)
            {
                return {
                    'x': cursorPosition.x - $element.offset().left,
                    'y': cursorPosition.y - $element.offset().top
                };
            }

            init();

        });

    }

    $.fn.hoverSlide.defaults = {
        itemClass: '.panel',
        autoSlide: true,
        hoverSlide: true,
        speed: 8
    };

})(jQuery);
