$(document).ready(function() {
	
	var apiKey = 'a9301e7f500f9b9d8e8e0c80a810a40f';
	var xOffset = 10;
	var yOffset = 30;
    
	
	$('a.person').live('mouseover click mousemove mouseout', function(event) {
		
		
		if (event.type == 'mouseover') {
			$(this).stop().animate({ opacity: 0.8 }, 100);
		}
		else if (event.type == 'click') {
		
		
		event.preventDefault();
		
	    var href = $(this).attr('href');	
		
	    $.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + $(this).attr('id') + '&format=json&jsoncallback=?', 
	        
			function (data){
				
				var location;
				
				if (typeof data.photo.location.locality  != "undefined"){
					location = data.photo.location.locality._content;
				}
				else if(typeof data.photo.location.county  != "undefined"){
					location = data.photo.location.county._content;
				}
				else{
					location = data.photo.location.region._content;
				}
				

		        $("body").append('<div id="preview"><img src="' + href + '" alt="' + data.photo.title._content + '" /><p><strong>' + data.photo.title._content + '</strong> <em>from ' + data.photo.tags.tag[0].raw + '</em></p><p>We met ' + jQuery.timeago(data.photo.dates.taken) + ' in ' + location + ', ' + data.photo.location.country._content + ".</p></div>");								 
				$("#preview")
					.css("top",(event.pageY - yOffset) + "px")
					.css("left",(event.pageX + xOffset) + "px")
					.fadeIn("fast");
			});

		
		}
		else if (event.type == 'mousemove'){
			$("#preview")
				.css("top",(event.pageY - yOffset) + "px")
				.css("left",(event.pageX + xOffset) + "px");
		}
		else {
			$("#preview").remove();
			$(this).stop().animate({ opacity: 1.0 }, 100);
		}
	});
	
	
	
	
	
	
});
