$(function(){
	initRandomBg();
	initTwits();
});

function initRandomBg(){
	var slides =  $('ul.background-image li');
	slides.eq(getRandomInt(slides.length)).show();
	function getRandomInt(max) {
	  return Math.floor(Math.random() * max);
	}
}

// twits init
function initTwits() {
	$('div.twiiter-row').each(function(){
		attachJS('http://www.twitter.com/statuses/user_timeline/me_no_speak.json?callback=twitterCallback&count=1')
	});

	// attach js
	function attachJS(src,code) {
		var s = document.createElement('script');
		s.setAttribute('type','text/javascript');
		if(!code) s.setAttribute('src',src);
		else s.innerHTML = code;
		document.getElementsByTagName('head')[0].appendChild(s);
	}
}

// twitter api callback function
function twitterCallback(obj) {
	var _twitter = $('div.twiiter-row');
	if(_twitter.length) {
		_twitter.html(replaceHyperlinks(obj[0].text));
	}
}

// regex link replace
function replaceHyperlinks(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return text.replace(exp,"<a href='$1'>$1</a>");
}
