$(document).ready(function() 
{
	var LINK = 'link';
	var PHOTO = 'photo';
	var STATUS = 'status';	
	
    //Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    var url = 'https://graph.facebook.com/timessquarenyc/posts?access_token=242273199131981|0b049b3dbeca1056f71b87a0f6a3879a&limit=5&callback=?';
	var listId = '#facebookList';
	
    //Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
    $.getJSON(url, function(data)
	{
		//console.log(data);
				
		if(data && data.data)
		{
			$.each(data.data, function(i, item)
			{
				var type = item.type;
				var html;
				var li = $(document.createElement('li'));
				if(i >= 2)
				{
					$(li).addClass('hide');
				}
				switch(type)
				{
					case LINK :
						$(li).addClass('facebookFeedLink');
						html = '<h1><a href="' + item.message + '">' + item.name + '</a></h1>';
						if(item.picture)
						{
							html += '<p><img src="' + item.picture + '" />' + item.description + '</p>';
						}
						else
						{
							html += '<p>' + item.description + '</p>';
						}
						html += '<p><abbr data-date="' + item.created_time + '">' + prettyDate(item.created_time) + '</abbr></p>';
						break;
						
					case PHOTO :
						$(li).addClass('facebookFeedPhoto');
						html = '<p>' + item.message + '<br /><a href="' + item.link + ' target="new"><img src="' + item.picture + '" /></a></p>';
						html += '<p><abbr data-date="' + item.created_time + '">' + prettyDate(item.created_time) + '</abbr></p>';
						break;
						
					case STATUS :
						$(li).addClass('facebookFeedStatus');
						html = '<p>' + item.message + '</p>';
						html += '<p><abbr data-date="' + item.created_time + '">' + prettyDate(item.created_time) + '</abbr></p>';
						break;
				}
				$(li).append(html);
				$(listId).append(li);	
			});
		}
    });
});
