/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/


function slideSwitch() {
    var $active = $('#brandLinks DIV.active');

    if ( $active.length == 0 ) $active = $('#brandLinks DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#brandLinks DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');
        });
}


String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
}

function mycarousel_initCallback(carousel, state)
{
    // Lock until all items are loaded. That prevents jCarousel from
    // setup correctly and we have to do that in the ajax callback
    // function with carousel.setup().
    // We're doing that because we don't know the exact height of each
    // items until they are added to the list.
    carousel.lock();

    $(".jcarousel-clip-vertical").addClass('loading');

    $.getJSON('http://twitter.com/status/user_timeline/provu_uk.json?count=5&callback=?', function(data){
            $.each(data, function(index, item) {
                    carousel.add(index + 1, twitter_getItemHTML(item));
            });

            carousel.size(data.length);

            // Unlock and setup.
            carousel.unlock();
            carousel.setup();

            $(".jcarousel-clip-vertical").removeClass('loading');
    });
}

function twitter_getItemHTML(item)
{
    return '<p>'+item.text.linkify()+'</p>';
}


$(document).ready(function() {
    $(".nojs").remove();

    $('#twitter').jcarousel({
        vertical: true,
        auto: 4,
        size: 0,
        scroll: 1,
        wrap: 'circular',
        animation: 'normal',
        easing: 'linear',
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    window.setInterval(slideSwitch, 3000);

    $('.pngFix').pngFix();


});
