var HorizontalTicker = new Class({
    
    Implements: [Options],
    
    options: {
        container_id: '#ticker_1',
        container_class: '.horizontal_ticker_container',
        item_wrapper_class: '.item_wrapper',
        item_class: '.item',
        speed: 2000,
        direction: 'left'
    },
    
    initialize: function(options){
        this.setOptions(options);
        this.setWrapperSize();
        this.tick();
    },
    
    setWrapperSize: function(){
        var new_wrapper_size = 0;
        var items = $$(this.options.container_id + ' ' + this.options.item_class);
        var wrapper = $$(this.options.container_id + ' ' + this.options.item_wrapper_class)[0];
        
        items.each(function(el){
            new_wrapper_size += el.getComputedSize().width;
        });
        
        wrapper.setStyle('width', new_wrapper_size);
    },
    
    tick: function(){
        var current_item = $$(this.options.container_id + ' ' + this.options.item_class)[0];
        var width = current_item.getComputedSize().width;
        var wrapper = $$(this.options.container_id + ' ' + this.options.item_wrapper_class)[0];
        var items = $$(this.options.container_id + ' ' + this.options.item_class);
        var margin_side;
        
        if(this.options.direction == 'left'){
            margin_side = 'margin-left';
            wrapper.setStyle('left', 0);
            
            items.each(function(el){
                el.setStyle('float', 'left');
            });
        }
        else{
            margin_side = 'margin-right';
            wrapper.setStyle('right', 0);
            
            items.each(function(el){
                el.setStyle('float', 'right');
            });
        }
        
        current_item.setStyle(margin_side, -width);
        
        (function(){ 
            wrapper.grab(current_item);
            current_item.setStyle(margin_side, 0);
        }).delay(this.options.speed, this);
        
        this.tick.delay(this.options.speed, this);
    }
});

window.addEvent('domready', function(){
    window.ticker1 = new HorizontalTicker({
        container_id: '#ticker1',
        container_class: '.ticker_horizontal',
        item_wrapper_class: '.ticker_content_wrapper',
        item_class: '.ticker_content',
        speed: 5000,
        direction: 'left'
    });
});
