var Twitter = Ext.extend( Ext.util.Observable, {
	template: '<div id="{id}"><h3>{date}</h3><p>{text}</p></div>',
	
	constructor: function(element, username, template) {
		Ext.apply(this, {
			el: Ext.get(element),
			username: username,
			template: new Ext.Template(template || this.template)
		})
	},
	
	load: function() {
		Ext.Ajax.request({
			url: 'request.php',
			success: function(response) {
				this.renderData(Ext.decode(response.responseText));
			},
			failure: this.renderFailure,
			method: 'GET',
			scope: this,
			params: { 
				login: this.username
			}
		});
	},
	
	renderData: function(data) {
		Ext.each(data, function(item) {
			item.date = new Date(item.created_at).getFormatedDate();
			this.template.append(this.el, item);
		}, this);
	},

	renderFailure: function() {
		this.el.update('<p>Při načítání dat došlo bohužel k chybě. Zachovejte klid a za chvilku to zkuste znovu.</p>');
	}
})