(function()
{
    jQuery.fn.Imagebar = function(options)
    {
        var imagebar = jQuery(this);
        var settings = jQuery.extend({
            'start': 0,
            'timeout': 50,
            'width': 173,
            'gap': 1,
            'left': 0
        }, options);
        
        function init()
        {
            checkWidth();
            start();
        }
        
        function checkWidth()
        {
            var width    = imagebar.width();
            var elements = imagebar.children();    
            var size     = $('#imagebar').width() + settings.width;

            while(width < size)
            {
                imagebar.append('<li>' + $(elements).eq(settings.start).html() + '</li>');
        
                width = imagebar.width();
        
                settings.start++;
                
                myLytebox.initialize();
            }
        }
        
        function appendElement()
        {
            var elements = imagebar.children();
            
            imagebar.append('<li>' + $(elements).eq(settings.start).html() + '</li>');
            
            myLytebox.initialize();
        }
        
        function start()
        {
            next();
        }
        
        function pause()
        {
            timer = clearTimeout(timer);
        }
        
        function resume()
        {
            next();
        }
        
        function next()
        {
            settings.left -= 1;
            
            imagebar.css('left', settings.left+'px');
            
            checkWidth();          
            
            if((settings.left + settings.width) <= 0)
            {
                var elements = imagebar.children();
                
                appendElement();
               
                elements.eq(0).remove();    
                
                settings.left += settings.width
                
                imagebar.css('left', settings.left+'px');
            }
            
            timer = setTimeout(function(){ next(); }, settings.timeout);
        }
        
        init();
        
        return imagebar.each(function()
        {
            jQuery(this).hoverIntent(
            {
                sensitivity: 1,
                interval: 20,
                timeout: 20,
                out: function()
                {
                    resume();
                },   
                over: function()
                {
                    pause();
                }
            });
        });

    }
})(jQuery);
