(function() {

$.cloud = {};
$.cloud.o = [];
$.cloud.op = {};
$.cloud.defaults = {
    zIndex: 5,
    interval: 1000,
    delay: 100,
    items: 10,
    title: false,
    fx: {
        on: {
            effect: 'fadeIn',
            speed: 250
        },
        off: {
            effect: 'fadeOut',
            speed: 250
        }
    }
};
$.fn.cloud = function( op ) {

    var s = this.serial = $.cloud.o.length;
    var o = $.extend( {}, $.cloud.defaults, op );
    $.cloud.o[ s ] = $.cloud.op = o;

    return this.each( function () {

        var $bubbles = $( this ).find( ' > div ' ),
            interval = o.interval,
            delay = o.delay,
            i = 0,
            on = o.fx.on,
            off = o.fx.off,
            n = ( $bubbles.size() < o.items ) ? $bubbles.size() : o.items,
            maxZIndex = parseInt( o.zIndex, 10 ) || 1,
            $title = $( o.title );

        var $parent = $( this ).parent(),
            // Area's Size
            area = {
                width: $parent.width(),
                height: $parent.height()
            },
            // MDC - Random Integer
            // https://developer.mozilla.org/ja/Core_JavaScript_1.5_Reference/Global_Objects/Math/random
            getRandomInt = function( max, min ) {
                return Math.floor( Math.random() * ( max - min + 1 )) + min;
            },
            // Random position in pixels
            getRandomPos = function( maxX, maxY, layerIndex ) {
                return {
                    left: getRandomInt( 0, maxX || area.width ) + 'px',
                    top: getRandomInt( 0, maxY || area.height ) + 'px',
                    zIndex: layerIndex || 9999
                };
            };

        // Trick
        // $currBubble.addClass( className )
        // Same as $curBubble['addClass']( className );
        
        var show = function ( s, t ) {
            var timer = parseInt( t, 10 ) || 1,
                $curBubble = $( $bubbles[ s ]  );
            maxZIndex += 2;
            var newDisplay = window.setTimeout( function() {
                $curBubble.css( getRandomPos( false, false, maxZIndex) ).animate( on.effect, on.speed );
            }, timer );
        };
        var timeline = function( ) {
            var nextItem = ( ( i + n ) < $bubbles.size() ) ? ( i + n ) : 0;
            $( $bubbles[ i ] ).animate( off.effect, off.speed, function() {
                show( nextItem, 0 );
            } );
            if ( nextItem > 0 ) {
                i++;
            } else {
                i = 0;
            }
        };
        // Initialize
        for ( var j=0; j<n; j++ ) {
            show( j, ( on.speed * j ) );
        }
        // Start the timeline
        var postStart = window.setTimeout( function() {
            var p = window.setInterval( timeline, interval );
        }, ( on.speed * n ) + delay );

        // Toggle Title
        var titleDelay = window.setTimeout( function() {
            var titleInterval = window.setInterval( function() {
                if ( $title.is(':hidden') ) {
                    $title.css( getRandomPos( 600, 100, false ) ).animate( on.effect, on.speed );
                }
                else {
                    $title.animate( off.effect, off.speed );
                }
            }, o.interval );
        }, o.delay + 2500 );

    } );

};

})( jQuery );

window.tatamo.initClouds = function() {
    if ( tatamo.plugins.twitter && tatamo.plugins.flickr ) {
        $( '#live-messenger h3, #live-messenger div.twitter-tag, #live-messenger div.flickr-tag' ).hover(
            function() {
                $( this ).attr( 'zindex', $( this ).css( 'zIndex' ) );
                $( this ).css( 'zIndex',  9999 );
            },
            function() {
                $( this ).css( 'zIndex',  $( this ).attr( 'zindex' ) );
            }
        );
        $( '#twitter-tags' ).cloud({
            interval: 11000,
            delay: 2000,
            items: 3,
            zIndex: 10,
            title: '#twitter-title',
            fx: {
                on: {
                    effect: {
                        opacity: 'toggle'
                    },
                    speed: 1000
                },
                off: {
                    effect: {
                        opacity: 'toggle'
                    },
                    speed: 1000
                }
            }
        });
        setTimeout(function() {
            $( '#flickr-tags' ).cloud({
                interval: 11000,
                delay: 3000,
                items: 3,
                zIndex: 11,
                title: '#flickr-title',
                fx: {
                    on: {
                        effect: {
                            opacity: 'toggle'
                        },
                        speed: 1000
                    },
                    off: {
                        effect: {
                            opacity: 'toggle'
                        },
                        speed: 1000
                    }
                }
            });
        }, 4000 );
        return true;
    }
    if ( tatamo.plugins.failed < 1 ) {
        window.setTimeout( tatamo.initClouds, 250 );
    }
};

$(window).load(function() {
    setTimeout(function() { tatamo.initClouds(); }, 500 );
});

