﻿String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
		return url.link(url);
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};

String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

$(document).ready(function(){
	var tweetUrl = "http://api.twitter.com/1/statuses/user_timeline.json";
	var tweetHomeUser = "CNGnow";
	var tweetFeaturedUser = "grahamcolton";
	var tweetCallback = "?";
	var tweetCount = "10"; //added more results to allow supression of @replies
	var tweetUser = (IsHomePage(window.location.href))?tweetHomeUser:tweetFeaturedUser;
	var tweetFullUrl = String.format("{0}?callback={1}&screen_name={2}&count={3}&include_rts=true", 
									 tweetUrl, tweetCallback, tweetUser, tweetCount)
	$.getJSON( tweetFullUrl,
		function(tweet){ 
			if(IsHomePage(window.location.href)){
				for (var i = 0; i < tweet.length; i++) 
				{
					var text=tweet[i].text;
					if(text.substring(0,1) !="@"){
						$("#latestTweet > p").html(text.parseURL().parseUsername().parseHashtag());
						$("#latestTweet > p a:not(has(img))").filter(function() { return this.hostname && this.hostname !== location.hostname; }).attr("target", "_blank").addClass("external");
						$("html").addClass("tweet");
						$("#latestTweet").removeClass("hidden");
						break;
					}
				}
			}
		}
	);

});



