var norml={
	timer:null,
	n_twitter_refreshes:0,
	max_twitter_refreshes:10
}

norml.init=function() {
	norml.init_twitter();
}

norml.init_twitter=function() {
	var url="http://twitter.com/statuses/user_timeline.json?screen_name=tedxcc&callback=norml.load_twitter&count=25&clientsource=tumblr_tedxcc",dest=$("nav.pages div.twitter"),feed = $("nav.pages");
	
	if (dest.length < 1) {
		feed.append("<div class='twitter'></div>");
		dest = $("nav.pages div.twitter");
		dest.slideUp("fast");
	}
	
	norml.add_script(url)
}

norml.load_twitter=function(obj) {
	var feed = $("nav.pages"),n=obj.length,i=0,out, dest=$("nav.pages div.twitter");
	
	out = "<h3><a href='http://twitter.com/tedxcc'>The Latest from Twitter</a></h3><ul>";
	for (i=0;i<n;i++) {
		var item = obj[i];
		out+="<li>"+item.text.parseURL().parseUsername().parseHashtag()+"</li>";
	}
	out +="</ul>";
	
	dest.html(out);
	
	$("nav.pages div.twitter").slideDown('slow');
	
	if (norml.n_twitter_refreshes < norml.max_twitter_refreshes) {
		norml.timer = setTimeout("norml.init_twitter()",45000);
		norml.n_twitter_refreshes++;
	}
}

norml.add_script=function(url) {
	var s = document.createElement("script");
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',url)
	document.getElementsByTagName("head")[0].appendChild(s);
}

jQuery(function() {
	norml.init();
})

/* twitter parsing stuff from: http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript */

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);
	});
};





