(function($) {

	$.fn.expand = function(link, ShowText, HideText) {
		var showtext = ShowText;
		var hidetext = HideText;
		return $(this).each(function() {
			var root = $(this);
			var links = root.find(link);
			
			links.click(function(e) {
				e.preventDefault();
				var next = $(this).next();
				
				if(next.css('display') == 'block') {
					next.hide('fast');
					$(this).text(showtext).addClass('collapsed');
				} else {
					next.show('fast');
					$(this).text(hidetext).removeClass('collapsed');
				}				
			});
		});
	};
	
	var twitterUrl = "http://twitter.com/statuses/user_timeline/[USER].json?count=[COUNT]&callback=?";
        
    var linkify = function(s) {
        return s.replace(/\b[a-z]+:\/\/([^ ]+)/i, "<a href=\"$&\" target=\"_blank\">$&</a>")
    };

    var ago = function(date) {
		return date.toString();
        var diff = Math.floor((new Date() - date) / 1000);
        if (diff < 60)
            return diff + " seconds ago";
        else if (diff < 60 * 60)
            return Math.floor(diff / 60) + " minutes ago";
        else if (diff < 60 * 60 * 24)
            return Math.floor(diff / (60 * 60)) + " hours ago";
        else if (diff < 60 * 60 * 24 * 265)
            return Math.floor(diff / (60 * 60 * 24)) + " days ago";
        else
            return "over a year ago";
    };
	
	$.fn.prompt = function(textContainer, labelContainer) {
		return $(this).each(function() {
		
			var root = $(this);
			var input = root.find(textContainer);
			var label = root.find(labelContainer);
			var prompt =
                $("<span />")
                 .addClass("prompt")
                 .insertAfter(label);
			label.show().appendTo(prompt);
			
			var pos = input.position();
			prompt.css('left', pos.left).css('top', pos.top);
			input.bind('click focus', function() {
				prompt.hide();
				if ($(this).val() == label.text()) $(this).val('');

			}).bind('blur', function() {
				if ($(this).val() == label.text() || $(this).val() == '') {
					prompt.show();
					$(this).val('');
				}
			});
			prompt.click(function() {
				$(this).hide();
				input.focus();
			});

			$(function() {
				if (input.val() != '') prompt.hide();
			});

		});
	};
	
    
    $.fn.latestTwitterUpdate = function(user, items) {
        var root = $(this);
		var li = null;
		var ul = root.find('ul');
        var itemAmount = parseInt(items) > 0 ? parseInt(items) : 1;
		
        $.get(twitterUrl.replace("[USER]", user).replace("[COUNT]", itemAmount), null, function(data) {
            if (data && data.length) {
				for(var i = 0; i < itemAmount; i++) {
					li = $('<li class="Item'+ i + '"></li>').appendTo(ul);
					$('<p class="text"></p>').html(linkify(data[i].text)).appendTo(li);	
					var d = new Date(data[i].created_at.replace('+', 'UTC+'));					
					$('<p class="time Date"></p>').text(d.getDate() + '.'+eval(d.getMonth() + 1) + '.' + d.getFullYear()).appendTo(li);	
					root.slideDown(300);
				}
            }
        }, "jsonp");
    };
	
})(jQuery);
