( function($){ 
$(document).ready(function(){
function mycarousel_itemLoadCallback(carousel, state)
{
	if (state != 'init') return;

	$.getJSON('http://twitter.com/status/user_timeline/TrainingRoomPT.json?count=8&callback=?', function(data){
		$.each(data, function(index, item){
		        carousel.add(index+1, '<li><div class="blue mt_10">' + linkify(item.text) + '</div><div class="time">' + parseTwitterDate(item.created_at) + '</div><br/></li>');

		});
	
	});

    carousel.size(5);
};

function parseTwitterDate(stamp)
{		
var v=stamp.split(' ');
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Novr", "Dec");

var d = new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }

var h = d.getHours();
var p;
if (h > 12 ) { p = "PM"; h -= 12; }
if (h <= 12 ) { p = "AM"; }
if (h == 0 ) { p = "PM"; h = 12; }
var m = d.getMinutes();

var curr_month = d.getMonth();

return h + ":" + m + " " + p + " " + m_names[curr_month] + " " + curr_date + sup;
}


 $( '#twitter' ).jcarousel(
    {
        vertical: true,
//	visible:  1,
	scroll:	  1,
        auto:     1,
	wrap:	  "circular",
        itemLoadCallback: mycarousel_itemLoadCallback,
        animation:    3000
    });
//alert($( '#twitter' ).html);

function linkify(text){
    if (text) {
        text = text.replace(
            /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
            function(url){
                var full_url = url;
                if (!full_url.match('^https?:\/\/')) {
                    full_url = 'http://' + full_url;
                }
                return '<div class="http"> <a href="' + full_url + '" target="_blank">' + url + '</a> </div>';
            }
        );
    }
    return text;
}
});
})( jQuery );
