(function($) {
    $.fn.getTwitter = function(options) {
        var o = $.extend({}, $.fn.getTwitter.defaults, options);
        if ($('.loadscreen')) {
            $('.loadscreen').show();
            $('.loadscreen').css({ opacity: 0.4 });
        }
        // hide container element
        $(this).hide();
        // add twitter list to container element
        $(this).append('<ul id="' + o.userName + '_update_list"><li></li></ul>');

        // hide twitter list
        $("ul#" + o.userName + "_update_list").hide();

        $(this).show();
        if (o.widget) {
            displayTweet(0, o.userName);
        }
        else {
            displayItem(0, o.userName);
        }
        // show twitter list
        if (o.slideIn) {
            $("ul#" + o.userName + "_update_list").slideDown(500);
        }
        else {
            $("ul#" + o.userName + "_update_list").show();
        }

        // give first list item a special class
        $("ul#" + o.userName + "_update_list li:first").addClass("firstTweet");

        // give last list item a special class
        $("ul#" + o.userName + "_update_list li:last").addClass("lastTweet");
        if ($('.loadscreen')) {
            $('.loadscreen').hide();
        }
    };    
    // plugin defaults
    $.fn.getTwitter.defaults = {
        userName: null,
        numTweets: 1,
        preloaderId: "preloader",
        loaderText: "Loading tweets...",
        slideIn: false
    };
})(jQuery);

function displayTweet(index, username) {
    var statusHTML = [];
    var list = eval(username + "_status_list");
    if (list.length > index) {
        var current = list[index];
        var status = current.Text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
            return reply.charAt(0) + '<a href="http://www.twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<li id="item_' + username + '_' + index + '"><p class="t-status">' + status + '</p><p class="t-date">' + formatTime(current.Date) + '</p></li>');
        $('#' + username + '_update_list').html(statusHTML.join(''));
        $('#website').attr('href', current.Website);
        $('#website').attr('title', current.DisplayName);
        $('#website').html(current.DisplayName);        

        $('#follow').attr('href', 'http://twitter.com/'+current.UserName);
        $('#follow').attr('title', 'Follow ' + current.UserName);

        $('#twitterUrl').attr('href', 'http://twitter.com/' + current.UserName);
        $('#twitterUrl').attr('title', 'Follow ' + current.UserName);
        $('#twitterUrl').html('http://twitter.com/' + current.UserName);
    }
}

// function callback
// twitterResult read result and fill to list
function displayItem(index, username) {
    var statusHTML = [];    
    var list = eval(username + "_status_list");    
    if (list.length > index) {
        var current = list[index];        
        var status = current.Text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
            return reply.charAt(0) + '<a href="http://www.twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<li id="item_' + username + '_' + index + '"><p class="t-status scroll-pane">' + status + '</p><p class="t-date">' + formatTime(current.Date) + '</p></li>');
        $('#' + username + '_update_list').html(statusHTML.join(''));
        if ($('#follow-link-' + username)) {
            $('#follow-link-' + username).attr('href', 'http://twitter.com/' + current.UserName);
            $('#follow-link-' + username).attr('title', 'Follow ' + current.UserName);            
        }
        if ($('#follow-image')) {
            $('#follow-image').attr('href', 'http://twitter.com/' + current.UserName);
            $('#follow-image').attr('title', 'Follow ' + current.UserName);            
        }
        
    }
}

function formatTime(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}
