(function($){
    $.fn.logos = function(o){
        return this.each(function(){
            new $ls(this,o);
        });
    };
    
    var defaults = {
        step: 0.1,
        timer: 1000/60,
        clones: 3,
        wide: '.wide',
        inner: '.content',
        ghost: '.ghost'
    };
    
    $.logos = function(e,o){
        this.options    = $.extend({}, defaults, o || {});
        this.setup(e.id);
        return $ls;
    };
    
    var $ls = $.logos;

    $ls.fn = $ls.prototype = {
        Logos: '0.0.0'
    };
    
    $ls.fn.extend = $ls.extend = $.extend;
    
    $ls.fn.extend({
        setup: function(el){
            this.element = el;
            this.content = {div: $('#'+el+' '+this.options.inner),
            img: $('#'+el+' '+this.options.inner+' img')};
            
            this.wide = $('#'+el+' '+this.options.wide);
            
            this.maxwidth = 0;
            this.position = 0;
            i = 0;
            
            var contentImg = this.content.img;
            var contentDiv = this.content.div;
            var parent = this;
            contentImg.each(function(){
                $(this).load(function(){
                    i = i + 1;
                    if(i<=contentImg.length){
                        parent.maxwidth = parent.maxwidth + $(this).width();
                    }
                    if(i==contentImg.length){
                        var fst = parent.wide;
                        fst.css({width:parent.maxwidth*3});
                        var rmc = parent.options.inner;
                        var adc = parent.options.ghost;
                        contentDiv.clone(true).removeClass(rmc.slice(1,rmc.length)).addClass(adc.slice(1,adc.length)).insertAfter('#'+parent.element+' '+parent.options.inner);
                        contentDiv.css({width:parent.maxwidth});
                        if($('#'+parent.element+' '+parent.options.ghost)){
                            
                            parent.ghost = {div: $('#'+parent.element+' '+parent.options.ghost),
                            img: $('#'+parent.element+' '+parent.options.ghost+' img'),
                            fimg: $('#'+parent.element+' '+parent.options.ghost+' img:first'),
                            limg: '#'+parent.element+' '+parent.options.ghost+' img:last'};
                            
                            var c = 0;
                            parent.ghost.img.each(function(){c=c+1;if(parent.options.clones=='all' || c<=parent.options.clones){$(this).clone(true).insertAfter(parent.ghost.limg);}});
                            
                            parent.anim();
                            
                        }else {alert('no ghosts!'); return false;}
                    }
                });
            });
        },
        anim: function(){
            if(!this.position && this.position!=0){
                this.position = 0;
            }else {
                this.position = this.position-this.options.step;
            }
            if(this.position<=-1*this.maxwidth){
                this.position = 0;
            }
            this.content.div.css({marginLeft:this.position});
            var parent = this;
            setTimeout(function(){ parent.anim(); }, parent.options.timer);
        }
    });
})(jQuery);

