(function() {
	var TwitterBox = function( username ) {
		var self = this;
		this.username = username;
		
		$.getJSON("http://twitter.com/statuses/user_timeline/"+this.username+".json?callback=?",{count:10,page:1},function(data) {
			self.updateTweet(data);
		});
		
		
		var template = "\
			<div class=\"twitter-box\">\
				<div class=\"header\">\
					<div class=\"profile-image\" ></div>\
					<div class=\"profile-title\"></div>\
					<div class=\"clearer\"></div>\
				</div>\
				<div class=\"tweets\">\
					<div class=\"tweet\">\
						<div class=\"tweet-text\">text</div>\
						<div class=\"tweet-by\"> von <span class=\"tweet-username\"></span></div>\
						<div class=\"tweet-time\">time</div>\
					</div>\
				</div>\
			</div>";
		
		this.icon = jQuery("<div class='twitter-button'></div>");
		this.element = $(template);
		this.tweetTpl = this.element.find(".tweet").remove();
	}
	
	TwitterBox.prototype = {
		getElement : function() {
			return this.element;
		},
		
		setContent : function( content ) {
			this.content = content;
		},
		
		getPosition : function() {
			return "right";
		},
		
		getToolbarIcon : function() {
			return this.icon;//twitter.png
		},
		
		getToolbarTitle : function() {
			
		},
		
		getElement : function() {
			return this.element;
		},
		
		setHeight : function( height ) {
			
		},
		
		isAvailable : function() {
			return true;
		},
		
		
		updateTweet : function(data) {
			var tpl;
			var self = this;
			
			$.each(data, function () {
				tpl = self.tweetTpl.clone();
				tpl.find(".tweet-text").html(self.formatTweet(this.text));
				tpl.find(".tweet-username").text(this.user.name);
				tpl.find(".tweet-time").text(self.relativeTime(this.created_at));
				self.element.find(".tweets").append(tpl);
			});
			
			
			this.element.find('.profile-image').css("background-image", "url("+data[0].user.profile_image_url+")");
			this.element.find('.profile-title').text(data[0].user.name);
			this.content.updateSize();
		},
		
		formatTweet : function(str) {
		    str = ' ' + str;
		    str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm, '<a href="$1" target="_blank">$1</a>');
		    str = str.replace(/([^\w])\@([\w\-]+)/gm, '$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
		    str = str.replace(/([^\w])\#([\w\-]+)/gm, '$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
		    return str;
		},
		
		relativeTime : function(created_at) {
		    var currentTime = new Date();
		    var timeOfTweet = Date.parse(created_at);
		    var timeStamp = currentTime.getTime();
		    var difference = parseInt((timeStamp - timeOfTweet) / 1000);
		    var result = '';
		    if (difference < 0) return false;
		    else if (difference < 10) result = 'Gerade Eben';
		    else if (difference <= 60) result = 'Vor weniger als einer Minute';
		    else if (difference <= 3600) result = 'Vor weniger als einer Stunde';
		    else if (difference <= 7200) result = 'Vor weniger als 2 Stunden';
		    else if (difference <= 86400) {
		        var dHour = Math.round(difference / 3600);
		        result = 'Vor weniger als ' + dHour + ' Stunden';
		    } else if (difference <= (604800)) {
		        var dDay = Math.round(difference / 86400);
		        result = 'Vor weniger als ' + dDay + ' Tagen';
		    } else {
		        result = 'Vor weniger als ' + Math.round(difference / (604800)) + ' Wochen';
		    }
		    return result;
		}
		
	}
	
	

	window.TwitterBox = TwitterBox;
})();
