// JavaScript Document
Ext.onReady(function() {
	function setValues() {
		var thep = Ext.getDom('twitter-text');
		thep.innerHTML = '';
		var date = store.getAt(0).get('created_at');
		var text = store.getAt(0).get('text');
		thep.innerHTML = text.parseURL().parseUsername().parseHashtag();
		//var t=setTimeout("hideValues()",8000);
	}
	var store = new Ext.data.XmlStore({
		// store configs
		autoDestroy: true,
		storeId: 'myStore',
		url: '/_custom/php/twitter.php',
		record: 'status',
		fields: [
			'created_at', 'text'
		]
	});
	function hideValues() {
		var thep = Ext.getDom('twitter-text');
		thep.innerHTML = '';
		Ext.DomHelper.append(thep, {tag:'img', id:'loading-image', src:'/_custom/images/icons/ajax-loader.gif'});
		Ext.DomHelper.insertAfter('loading-image','   Loading latest tweet...');
		//var t = setTimeout("store.load({callback:setValues})",2000);
	}
	String.prototype.parseURL = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
			return url.link(url);
		});
	};
	String.prototype.parseUsername = function() {
		return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
			var username = u.replace("@","")
			return u.link("http://twitter.com/"+username);
		});
	};
	String.prototype.parseHashtag = function() {
		return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
			var tag = t.replace("#","%23")
			return t.link("http://search.twitter.com/search?q="+tag);
		});
	};
	store.load({callback: setValues});
});
