/* 
 * Copyright 2010 Benjamin Davis <ben@bnhw.co.uk>
 *
 * Distributed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 */

(function($)
{
    var methods =
    {
        init : function(options)
        {
            var settings =
            {
                'speed': 0.03,
                'position': 0.0,
				'padding': 0,
				'margin': 0
            };
            
            if (options)
            {
                $.extend(settings, options);
            }
            
            return this.each(function()
            {
                var $this = $(this),
                    data = $this.data('passiveSlide'),
                    ul = $this.children('ul');
                
                if (settings['width'])
                {
                    $this.width(settings['width']);
                }
                if (settings['height'])
                {
                    $this.height(settings['height']);
                }
                
                $this.css('overflow', 'hidden').css('position', 'relative');
                
                var totalWidth = 0;
                ul.children('li').css('float', 'left').css('display', 'block');
                ul.children('li').each(function() { totalWidth += $(this).width(); });
                ul.css('position', 'relative').css('list-style-type', 'none').css('padding', settings['padding']).css('margin', settings['margin']).width(totalWidth);
                
                $this.data('passiveSlide',
                {
                    'speed': settings['speed'],
                    'position': settings['position'],
                    'velocity': 0.0,
                    'maxWidth': (ul.innerWidth() - $this.width()),
                    'timerId': -1
                });
                
                $this.bind('mouseenter.passiveSlide', function(event)
                {
                    var $this = $(this),
                        data = $this.data('passiveSlide');
                        
                    window.clearInterval(data.timerId);
                    
                    $this.trigger('mousemove');
                    data.timerId = window.setInterval(function()
                    {
                        if ($this.children('ul').width() > $this.width())
                        {
                            data.position += data.velocity;
                            
                            if (data.position > 0.0)
                                data.position = 0.0;
                            else if (data.position < -data.maxWidth)
                                data.position = -data.maxWidth;
                            
                            $this.children('ul').css("left", data.position);
                        }
                    }, 15);
                });
                $this.bind('mouseleave.passiveSlide', function(event)
                {
                    var $this = $(this),
                        data = $this.data('passiveSlide');
                        
                    if (data.timerId != -1)
                    {
                        window.clearInterval(data.timerId);
                        data.timerId = -1;
                    }
                });
                $this.bind('mousemove.passiveSlide', function(event)
                {
                    var $this = $(this),
                        data = $this.data('passiveSlide');
                        
                    var distanceFromCentre = event.pageX - $this.offset().left - $this.width() / 2;
                    
                    data.velocity = -distanceFromCentre * data.speed;
                });
            });
        },
        
        destroy : function()
        {
            return this.each(function()
            {
                var $this = $(this),
                    data = $this.data('passiveSlide'),
                    timerId = data.timerId;
                
                $(window).unbind('.passiveSlide');
                if (timerId) window.clearInterval(timerId);
                data.passiveSlide.remove();
                $this.removeData('passiveSlide');
            });
        }
    };
    
    $.fn.passiveSlide = function(method)
    {
        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.passiveSlide' );
        }
    };
})(jQuery);


