(function ($) {
$.fn.lightcontentslide = function (options) {
    var $this = this;
    var opts = $.extend({}, $.fn.lightcontentslide.defaults, options);
    var stopautostep = function () {
        clearTimeout(opts.steptimer);
        clearTimeout(opts.resumeautostep);
    };
    this.attr('_ismouseover');
    var autorotate = function () {
        stepBy();
        opts.steptimer=setTimeout(function(){autorotate();}, opts.autostep.pause);
    };
    var stepBy = function () {
        stopautostep();
        var pindex=opts.currentpanel+3;
        if (pindex > opts.lastvisiblepanel) {
            pindex=(opts.currentpanel < opts.lastvisiblepanel)? opts.lastvisiblepanel : 0;
        }
    
        var endpoint=opts.paneloffsets[pindex]+(pindex==0? 0 : opts.beltoffset);
        if (pindex==0){
            opts.$belt.animate({left: - opts.paneloffsets[opts.currentpanel]- 100 +'px'},
                                 'normal',
                                 function(){
                                     opts.$belt.animate({left: -endpoint+'px'}, opts.speed);
                                 });
        }
        else {
            opts.$belt.animate({left: -endpoint+'px'}, opts.speed);
        }
        opts.currentpanel=pindex;
    };
    
    this.css({'overflow': 'hidden'});
    return this.each(function () {
            $panels = $('.'+opts.slideClass, this);
            opts.$belt = $('.'+opts.containerClass, this);
			opts.beltoffset = parseInt(opts.$belt.css('marginLeft')) || 0;
            opts.currentpanel= 0;            
            var paneloffset = 0;
            opts.paneloffsets = [0];
            var panelwidths = [];

            $panels.each(function(index) {
                    $(this).css({'float': 'none','position': 'absolute','left': paneloffset + 'px'});
                    paneloffset += parseInt($(this).css('marginRight'))
                        + parseInt($(this).get(0).offsetWidth || $(this).css('width'));
                    opts.paneloffsets.push(paneloffset);
                    panelwidths.push(paneloffset-opts.paneloffsets[opts.paneloffsets.length-2]);
                });
            opts.$belt.css({'width' : paneloffset + 'px'});
            opts.paneloffsets.pop();

            var addpanelwidths=0;
            var lastpanelindex=$panels.length-1;
            opts.lastvisiblepanel=lastpanelindex;
            for (var i=lastpanelindex; i>=0; i--){
                addpanelwidths+=(i==lastpanelindex? panelwidths[lastpanelindex] : opts.paneloffsets[i+1]-opts.paneloffsets[i]);
                if ($(this).width() > addpanelwidths){
                    opts.lastvisiblepanel=i; 
                }
            }
            
            $(this).hover(function(){
                    stopautostep(opts);
                }, function(){
                    if (opts.steptimer){
                        opts.resumeautostep = setTimeout(function(){
                                autorotate();
                            }, 500);
                    }
                });
            opts.steptimer = setTimeout(function(){
                    autorotate();
                }, opts.autostep.pause);
        });
};
$.fn.lightcontentslide.defaults = {
    containerClass : 'belt',
    slideClass : 'metier',
    autostep: {pause:2000},
    speed: 500
};

})(jQuery);

