// by james <at> bandit.co.nz
// 07/05/08
// special thanks to nik <at> codetocustomer.com for the regex help
// and Alan <at> alanhogan.com for @twitter matching
function autoLink(what) 
{
	str = what; out = ""; url = ""; i = 0;
	do {
		url = str.match(/(((ht|f)tps?:\/\/)?([a-z\-]+\.)*[\-\w]+(\.[a-z]{2,4})+(\/[\w\_\-\?\=\&\.]*)*(?![a-z]))/i);
		if(url!=null) {
			// get href value
			href = url[0];
			if(href.substr(0,7)!="http://") href = "http://"+href;
		
			// where the match occured
			where = str.indexOf(url[0]);
		
			// add it to the output
			out += str.substr(0,where);
		
			// link it
			out += "<a href=\"" + href + "\" target=\"_blank\">" + url[0] + "</a>";
		
			// prepare str for next round
			str = str.substr((where+url[0].length));
		} else {
			out += str;
			str = "";
		}
	} while(str.length>0);
	return out.replace(/\B@([_a-z0-9]+)/ig,'@<a href="http://twitter.com/$1">$1</a>');
}