window.addEvent('domready', function()
{
    var flowthing_container = document.getElement('.tx-stotzflowthing-pi1');
    if (flowthing_container != null)
    {
        flowthing_container.addClass('flowthingified');
        var flowthing_container_width = flowthing_container.getSize().x
        
        var flowthings = flowthing_container.getChildren();
        flowthings.each(function(flowthing)
        {
            flowthing.setStyles
            ({
                'color': '#000',
                'position': 'absolute',
                'left': flowthing_get_random_x(),
                'top': flowthing_get_random_y(),
                'font-size': flowthing_get_random(14, 50)
            });
            
            var x_tween = new Fx.Tween(flowthing, {duration: 1000, transition: 'linear'});
            x_tween.addEvent('complete', function(target)
            {
                target.setStyles
                ({
                    'font-size': flowthing_get_random(14, 50),
                    'left': flowthing_get_random_x(),
                    'top': flowthing_get_random_y()
                });
                flowthing_flow(target);
            });
            flowthing.store('x-tween', x_tween);
            
            var o_tween = new Fx.Tween(flowthing, {duration: 1000 / 2, link: 'chain', transition: 'linear'});
            flowthing.store('o-tween', o_tween);
            
            flowthing.store('p-morph', new Fx.Morph(flowthing));
            
            flowthing_flow(flowthing);
        });
    }
    
    function flowthing_flow(flowthing)
    {
        var distance = flowthing_get_random(500, 1000);
        var isnow = parseInt(flowthing.getStyle('left'));
        if (isnow > flowthing_container_width / 2)
        {
            whereto = flowthing_get_random(-200, 200);
        }
        else
        {
            whereto = flowthing_get_random(flowthing_container_width - 200, flowthing_container_width + 200);
        }

        var flowtime = flowthing_get_random(10000, 18000)
        var x_tween = flowthing.retrieve('x-tween');
        var o_tween = flowthing.retrieve('o-tween');
        x_tween.setOptions({'duration': flowtime});
        o_tween.setOptions({'duration': flowtime / 2});
        
        x_tween.start('left', whereto);
        o_tween.start('color', '#FFF');
        o_tween.start('color', '#000');
        
        flowthing_add_behaviour(flowthing);
    }
    
    function flowthing_add_behaviour(flowthing)
    {
        flowthing.addEvent('click', function()
        {
            var display_item = flowthing_container.getElement('.flowthing-display');
            if (display_item != null)
            {
                display_item.removeClass('flowthing-display');
                display_item.retrieve('p-morph').cancel();
                flowthing_flow(display_item);
            }
            
            this.retrieve('x-tween').cancel();
            this.retrieve('o-tween').cancel();
            
            this.tween('color', '#FFF');
            this.addClass('flowthing-display');
            
            this.retrieve('p-morph').start
            ({
                left: Math.round(flowthing_container_width / 2 - this.getSize().x / 2),
                top: 100
            });
            
            this.addEvent('click', function()
            {
                this.removeClass('flowthing-display');
                this.retrieve('p-morph').cancel();
                flowthing_flow(this);
            });
        });
    }
    
    
    function flowthing_get_random_x()
    {
        return flowthing_get_random(-200, flowthing_container.getSize().x + 200);
    }
    
    function flowthing_get_random_y()
    {
        return flowthing_get_random(50, flowthing_container.getSize().y - 50);
    }
    
    function flowthing_get_random(min, max)
    {
        if (min > max) return -1;
        if (min == max) return min;
        return min + parseInt(Math.random() * (max - min + 1));
    }
});

