/**
Version: 0.1
Release: 2010-04
Author: Ulrich Kautz <uk@fortrabbit.de>
License: Artistic Licencense (http://www.perl.com/pub/a/language/misc/Artistic.html)


Usage:

    <div id="blogout"></div>
    <script type="text/javascript">
    <!--
    jQuery( function( $ ) {
        $( '#blogout' ).feedTicker( {
            url: 'http://fortrabbit.de/blog/feed/rss/'
        } );
    } );
    //-->
    </script>



*/


$(document).ready(function () {
    
    var ticker_list = [], ticker_entries = [];
    
        
    /**
     * Load Feed via google ajax api
     *
     * @param String url of the RSS feed
     * @param Function callback to bcalled on load
     */
    $.extend( {
        loadFeed: function( url, callback ) {
            url = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+ encodeURIComponent( url );
            $.getJSON( url, function( data ) {
                if ( callback instanceof Function ) callback( data.responseData.feed );
            } );
        }
    } );
    
    
    $.fn.extend( {
        
        
        /**
         * Create a ticker outputting latest blog headlines from a given RSS url via the
         * google ajax api (thus https-conform).
         *
         * @param Hash args.tick = miliseconds, interval of typing letters; args.wait = amount of ticks to wait before switch to the next entry, args.url = the RSS url of the blog, args.callback = will be called after the blog is loaded
         */
        feedTicker: function( args ) {
            args = $.extend( {
                url: null,
                callback: null,
                tick: 28,
                wait: 100,
                max: 10,
                encaps: true
            }, args || {} );
            var out = this;
            
            // load feed in to ul
            $.loadFeed( args.url, function( d ) {
                var ul = $( '<ul style="display: none"></ul>' ).appendTo( 'body' );
                $( d.entries ).each( function( i ) {
                    if ( ++i > args.max ) return;
                    var li = $( '<li></li>' ).appendTo( ul );
                    var title = args.encaps !== false
                        ? $( '<a href="'+ this.link+ '"></a>' ).text( this.title )
                        : $( '<em></em>' ).text( this.title+ ': ' )
                    title.append($( '<span></span>' ).html( this.contentSnippet ) );
                    li.append( title );
                } );
                
                if ( args.tick == false ) {
                    ul.noTicker( out, args.wait );
                }
                // run ticker
                else {
                    ul.ticker( out, { tick: args.tick, wait: args.wait } );
                }
            } );
        },
        
        noTicker: function( out, interval ) {
            var lis = $( this ).find( 'li' ), num = 0;
            $( out ).empty().append( $( lis[num++] ).html() );
            setInterval( function() {
                if ( num >= lis.length ) num = 0;
                $( out ).empty().append( $( lis[num++] ).html() );
            }, interval );
        },
        
        /**
         * Create a ticker of a set of li's ..
         *
         * @param jQuerySelector the output target (content will be wiped)
         * @param Hash args.tick = miliseconds, interval of typing letters; args.wait = amount of ticks to wait before switch to the next entry
         */
        ticker: function( output, args ) {
            args = $.extend( {
                tick: 28,
                wait: 100 // wait for 100 ticks after each timeout
            }, args || {} );
            
            $( this ).each( function() {
                var ticker = [];
                $( this ).find( 'li' ).each( function() {
                    ticker.push( $( this ).html() );
                } );
                ticker_list.push( [ ticker, 0, 0, false, output, 0 ] );
            } );
            
            setInterval( function() {
                $( ticker_list ).each( function() {
                    var typeOut = function( me ) {
                        var ticker = me[0], item = me[1], pos = me[2], in_tag = me[3], output = me[4], wait = me[5];
                        if ( wait > 0 ) {
                            me[5] = wait - 1;
                            return false;
                        }
                        
                        var text = ticker[ item ];
                        
                        var c = text.substr( pos, 1 );
                        if ( c == '<' ) in_tag = true;
                        else if ( c == '>' ) in_tag = false;
                        
                        $( output ).html( "&nbsp;" + text.substr(0, pos++ ) );
                        
                        // last for this round ..
                        if ( pos > text.length ) {
                            item++;
                            if ( item >= ticker.length ) item = 0;
                            pos = 0;
                            in_tag = false;
                            wait = args.wait;
                        }
                        
                        me[1] = item, me[2] = pos, me[3] = in_tag, me[5] = wait;
                        return in_tag;
                    };
                    
                    while ( typeOut( this ) ) true;
                } );
            }, args.tick );
        }
    } );
    
});

