/* Create a namespace */
var GioiaLaw = {};

/* Fix minutes to add zero if it's less than 10 */
function fixTime(the_time) {
 	if (the_time <10) {
 		the_time = "0" + the_time;
 	}
 	return the_time;
}

/* Functions to parse URLs, usernames, and hash tags in Twitter feed */
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g,"");
}

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);
	});
};

/* Enable Twitter sample for sidebar */
GioiaLaw.enableTweetSample = function() {
	$.jTwitter('randygioia', 1, function(posts){
	  
		var rawtweet = posts[0].text;
		var parsedtweet = rawtweet.parseURL().parseUsername().parseHashtag();
		
		$('#tweets').append(parsedtweet);
	
		var rawtime = posts[0].created_at;

		var baddate = rawtime.split(" ");
		var newdate = baddate[0]+" "+baddate[1]+" "+baddate[2]+" "+baddate[5]+" "+baddate[3]+" "+baddate[4];
		
		var tweetDate = new Date(newdate);

		var AMPM = "PM";
 		var myHours;
		if(tweetDate.getHours() < 12){
			AMPM = "AM";
			if(tweetDate.getHours() == 0){
				myHours = "12";
			}else{
				myHours = tweetDate.getHours();
			}
		}else{
			myHours = tweetDate.getHours()-12;
		}

		var myDate = tweetDate.getDate() +"th";
		if(tweetDate.getDate() == 1 || tweetDate.getDate() == 21 || tweetDate.getDate() == 31){
			myDate = tweetDate.getDate() +"st";
		}else if(tweetDate.getDate() == 2 || tweetDate.getDate() == 22){
			myDate = tweetDate.getDate() +"nd";
		}else if(tweetDate.getDate() == 3 || tweetDate.getDate() == 23){
			myDate = tweetDate.getDate() +"rd";
		}
		
		var myMonth=new Array(12);
		myMonth[0]="Jan";
		myMonth[1]="Feb";
		myMonth[2]="Mar";
		myMonth[3]="Apr";
		myMonth[4]="May";
		myMonth[5]="June";
		myMonth[6]="July";
		myMonth[7]="Aug";
		myMonth[8]="Sept";
		myMonth[9]="Oct";
		myMonth[10]="Nov";
		myMonth[11]="Dec";
		
		var timeDiff = myHours+":"+fixTime(tweetDate.getMinutes())+" "+AMPM+" "+myMonth[tweetDate.getMonth()]+" "+myDate;
		
		$("#tweetstamp").append(timeDiff);
	});
}

