/*
 * Load a Twitter profile using YQL & jQuery
 *
 * created by: Jocelyn Stretton
 * 						 ninjagirl.com | @jsfwd
 */

$(document).ready(function() {
	
	var form = $('form#get-profile');
	var fieldset = form.find('.fieldset');
	var screen_name = form.find('input#screen-name');
	
	// data will be loaded here... don't show this until we have something to show
	var stage = $('#profile').hide();

	function twitter_profile(name) {
		// display loading graphic while we're grabbing the data
		stage.show().removeClass('empty').html('<div class="loading">&nbsp;</div>');

		// yql query url (url encoded)
		var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20"
		          + "from%20twitter.user.profile%20where%20id%3D'"
		          + name + "'&format=json&callback=?"
							+ "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
							
		$.getJSON(url, function(data){
			// twitter user doesn't exist
			if (!data.query.results) {
				stage.addClass('empty').html('Twitter doesn\'t have anyone by the name <em>' + screen_name.val() + '</em>');
				return;
			}
			
			// create an object from the json response
			var meta = {
				'url' : data.query.results.item.resource,
				'img' : data.query.results.item.item[0].resource
			};
			$.each(data.query.results.item.meta, function(key, val){
				meta[val.property] = val.content;
			});
			
			// build the profile output
			var title = '@' + meta['foaf:nick'] + ' (' + meta['url'].replace('http://', '') + ')';
			
			var out = '<a href="' + meta['url'] + '" id="avatar" title="' + title + '" rel="external"><img src="' + meta['img'] + '" alt="' + meta['foaf:name'] + '" /></a>';
			out += '<div class="content">';
			out += '<h2><a href="' + meta['url'] + '" title="' + title + '" rel="external">' + meta['foaf:name'] + '</a></h2>';
			out += '<a href="' + meta['url'] + '" title="' + title + '" class="nick" rel="external">@' + meta['foaf:nick'] + '</a>';
			
			if (meta['dc:description']) {
				out += '<div class="description">' + meta['dc:description'] + '</div>';
			}
			
			out += '<div class="extra">';
				if (meta['geo:location']) {
					out += meta['geo:location'] + ' | ';
				}
				out += meta['twit:followers'] + ' followers &middot; ' + meta['twit:friends'] + ' following | '
				out += meta['twit:statuses'] + ' tweets';
				
				// check if this is a protected user. the json response doesn't directly
				// identify protected, but won't return homepage
				if (!meta['foaf:homepage']) {
					stage.addClass('protected');
					out += '<span class="protected" title="@' + meta['foaf:nick'] + ' has protected their tweets">&nbsp;</span>';
				}
			out += '</div>';
			
			out += '</div>';
			
			stage.html(out);
		});
		
	}
	
	// get the twitter profile for the name entered
	form.submit(function() {
		var name = screen_name.val();
		// check that we have a name & it's not the hint text
		if (name.length && name != default_name) {
			twitter_profile(name);
			// remove the error message
			if ($('#error').length) {
				$('#error').remove();
			}
		} else if (!$('#error').length) {
			fieldset.append('<div id="error">Enter a Twitter Username</div>');
		}
		
		return false;
	});
	
	// add hint styling & clear field on focus
	var default_name = 'Twitter Username';
	screen_name.val(default_name).addClass('hint');
	
	screen_name.focus(function() {
		if ($(this).val() == default_name) {
			$(this).val('');
		}
		
		$(this).removeClass('hint');
	});
	
	screen_name.blur(function() {
		if ($(this).val() == '') {
			$(this).val(default_name);
		}
		if ($(this).val() == default_name) {
			$(this).addClass('hint');
		}
	});
	
	// open external links in a new window
	$('a[rel="external"]').attr('target', '_blank');
});
