/*
 * Fetch Twitter JSON and write to DOM
 * To do: implement some error handling and timeouts
 */
function getTwitter()
{
	/** Define the dom */
	var domId = '#twitter';

	/** Put the rotater */
	if(firstRunTwitter) {
		$(domId).append('<img src="/images/content/twitter_loader.gif" alt="Loader image" />');
	}

	/** get Twitter */
	$.getJSON("http://twitter.com/statuses/user_timeline/eMxyzptlk.json?count=1&callback=?",
	function(json)
	{
		/** Empty the dom */
		$(domId).html('');

		var html = "<ul>";
		$(json).each(function(tweet)
		{
			html += '<li>' + this.text + '<br /><span>'+ '<a href="http://twitter.com/' + this.user.screen_name + '">' + jQuery.timeago(this.created_at) +'</a></span></li>';
		});

		$(domId).append(html + "</ul>");
	});

	/** No longer first run */
	firstRunTwitter = false;
}

/*
 * Fetch LastFM and write to DOM
 * To do: implement some error handling and timeouts
 */
function getLastFm ()
{
	/** Define the dom */
	var domId = '#lastfm';

	/** Put the rotater */
	if(firstRunLastFm) {
		$(domId).append('<img src="/images/content/lastfm_loader.gif" alt="Loader image" />');
	}

	/** get Lastfm */
	$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=eMxyzptlk&format=json&limit=1&api_key=5e8b709a35634b81a8f9316c7251c284&callback=?",
	function(json)
	{
			/** Empty the dom */
			$(domId).html('');

			html = "";
			$(json.recenttracks).each(function(track)
			{
				html += '<h3><a href="' + this.track.url + '">' + this.track.artist['#text'] + '</a></h3>';
				if (this.track.image[2]['#text'] != "")
				{
					html += '<a href="' + this.track.url + '"><img src="'+ this.track.image[2]['#text'] + '" alt="'+ this.track.name +'" class="album-image" /></a>';
				}
				else
				{
					html += '<a href="' + this.track.url + '"><img src="/images/content/listening.png" alt="'+ this.track.name +'" class="album-image" /></a>';
				}

				html += '<p><a href="' + this.track.url + '">' + this.track.name + '</a></p>';
				if(this.track.date['#text'] != undefined)

				{
					html += '<p class="time">' + jQuery.timeago(this.track.date['#text']) + '</p>';
				}
			});
			$(domId).append(html);
	});

	/** No longer first run */
	firstRunLastFm = false;
}

/*
 * Fetch Flickr JSON and write to DOM
 * To do: implement some error handling and timeouts
 */
function getFlickr()
{
	/** Define the dom */
	var domId = '#flickr';

	/** Empty the dom */
	$(domId).html('');

	/** Put the rotater */
	$(domId).append('<img src="/images/content/flickr_loader.gif" alt="Loader image" class="loader" />');

	/** get Flickr */
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=42056000@N02&format=json&jsoncallback=?",
	function(json, textStatus)
		{
			$(domId + ' img.loader').remove();
			var html = "<ul>";
			$(json.items).each(function(i,photo)
			{
				var smallImage = (photo.media.m).replace("_m.jpg", "_s.jpg");

				html += '<li><a href="' + photo.link + '">';
				html += '<img src="' + smallImage;
				html += '" alt="'; html += smallImage.title + '" />';
				html += '</a></li>';
				if ( i == 2 ) return false;
			});
			$(domId).append(html + "</ul>");
		});
}

/*
 * Fetch picasaweb JSON and write to DOM
 */
function getPicasa()
{
	/** Define the dom */
	var domId = '#picasa';

	/** Empty the dom */
	$(domId).html('');

	/** Put the rotater */
	$(domId).append('<img src="/images/content/picasa_loader.gif" alt="Loader image" class="loader" />');

	/** Get Picasa */
	$(domId + ' img.loader').remove();
	$(domId).pwi({
		username: 'wael.nasreddine',
		albumMaxResults: 3,
		showAlbumTitles: false,
		showAlbumDescription: false,
		albumThumbSize: 160,
		blockUIConfig: {
			message: null,
		}
	});
}


/*
 * Fetch Lighthouse JSON and write to DOM
 */
function getLighthouse()
{
	project = $('#tickets table').attr("class");
	$.getJSON("http://wael.lighthouseapp.com/projects/" + project + "/tickets.json?_token=b7be0ea3f8655236d2e709d846ab31a9d8971f47&callback=?",
	function(json)
		{
			$(json.tickets).each(function(ticket)
			{
				if(this.ticket != null && this.ticket.url != null) {
					html = '<tr><td><a href="' + this.ticket.url + '">' + this.ticket.number + '</a></td>';
					html += '<td><a href="' + this.ticket.url + '">' + this.ticket.title + '</a></td>';
					html += '<td><a href="' + this.ticket.url + '">' + this.ticket.milestone_title + '</a></td>';
					html += '<td><a href="' + this.ticket.url + '">' + jQuery.timeago(this.ticket.created_at) + '</a></td>';
					html += '<td><a href="' + this.ticket.url + '">' + jQuery.timeago(this.ticket.updated_at) + '</a></td>';
					html += '<td class="' + this.ticket.state + '"><a href="' + this.ticket.url + '">' + this.ticket.state + '</a></td></tr>';
					$('#tickets table').append(html);
				}
			});

		});
}

/*
 * Fetch Redmine JSON and write to DOM
 */
function getRedmine()
{
	/** Get the project */
	project = $('#tickets table').attr("class");

	/** Sanity Check */
	if(project == null)
		return;

	/** Get the XML from redmine */
	$.jGFeed("http://projects.nasreddine.com/projects/" + project + "/issues.atom?key=0b2f4500419e30b89de1d297a1622229cd0ad549",
	function(feed) {
		console.log(feed);
		$(feed.entries).each(function(ticket)
		{
			if(this.link != null) {
				/** Get the number / status / title */
				var number_status_title = this.title;
				/** Define the RegExp to be used to parse the number_status_title */
				var title_RegExp = new RegExp(/^(\w+) (#[0-9]*) \((\w+)\): (.*)/gi);
				/** Run the RegExp on the number_status_title */
				var matches = title_RegExp.exec(number_status_title);
				/** Get the vars from the matches */
				var number = matches[2];
				var tracker = matches[1];
				var status = matches[3];
				var title = matches[4];

				/** Build the html */
				html = '<tr><td><a href="' + this.link + '">' + number + '</a></td>';
				html += '<td><a href="' + this.link + '">' + title + '</a></td>';
				html += '<td><a href="' + this.link + '">' + tracker + '</a></td>';
				html += '<td><a href="' + this.link + '">' + this.publishedDate + '</a></td>';
				html += '<td><a href="' + this.link + '">' + status + '</a></td>';
				$('#tickets table').append(html);
			}
		});
	});
}

/*
 * Contact me function
 */
function contactme()
{
	window.location = String.fromCharCode(109,97,105,108,116,111,58,119,97,101,108,64,110,97,115,114,101,100,100,105,110,101,46,99,111,109);
}
