$(document).ready(function() {
	
	function linkify(string) {
		var linktext = string.split(" ");
		var newLinktext = [];
		
		for (i=0;i<=linktext.length;i++) {
			var word = linktext[i];
			var wordString = String(word);
			if (wordString.indexOf('http') != -1) {
				newLinktext[i] = "<a href='" + word + "'>" + word + "</a>";
			}
			else if (wordString.indexOf('@') === 0) {
				var tweeter = wordString.replace(/\W/g, '');
				newLinktext[i] = "<a href='http://www.twitter.com/" + tweeter + "'>" + word + "</a>";
			}
			else if (wordString.indexOf('#') === 0) {
				var hashtag = wordString.replace(/\W/g, '');
				newLinktext[i] = "<a href='http://www.twitter.com/#search?q=%23" + hashtag + "'>" + word + "</a>";
			}			
			else {newLinktext[i] = word;}
			}
		
		return newLinktext.join(" ");
	}

						   
	function loadTweets() {
		$.ajax({
			   type: "GET",
			   dataType: "jsonp",
			   url: 'http://search.twitter.com/search.json?q=from%3Aukmsl&rpp=3',
			   success: function(data) {
				   $.each(data.results, function (i) {
												  // These variables populate the markup we will insert into the list
												  var text = linkify(data.results[i].text);
												  var tweetText = "<div class='tweet-text'>" + text + "</div>";
												  var createdAt = String(data.results[i].created_at);
												  var tweetTime = "<div class='tweet-time'>" + createdAt.slice(0,-9) + "</div>";
												  // var tweetAuthor = "<div class='tweet-author'>" + data.results[i].from_user + "</div>";
												  
												  $('.tweets ul').append('<li>' + tweetText + tweetTime + '</li>');
												  });
			   }
			   });
	}
	
	loadTweets();

	$('#facts').cycle({timeout:4000});

	loginCheck = $('#newsblog'); 
	if (loginCheck.length) {
		$('.blog .rss_description').each(function() {
			patt = /([\S'"-]+\s?){0,19}([\S'"-]+){1}/g;
			post = $(this).text();
			precis = post.match(patt);
			postlink = $(this).parent().find('a').attr('href');
			$(this).empty().append(precis[0] + "... <a href='" + postlink + "'>read more</a>");
		});
	}

});
