$(function() {
	getShots();
	twitterInit();
});

function getShots() {
	$.ajax({
	    url: "http://api.dribbble.com/players/micah/shots",
	    dataType: 'jsonp',
	    beforeSend: function( xhr ) {
			xhr.overrideMimeType( 'text/plain; charset=x-user-defined' );
		},
		success: function( data ) {
			showShots(data);
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (console && console.log){
				console.log(jqXHR);
				console.log(textStatus);
				console.log(errorThrown);
			}
		}
	});
	var yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdribbble.com%2Fplayers%2Fmicah%22%20and%0A%20%20%20%20%20%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22main%22%5D%2Fol%2Fli'&format=json&diagnostics=true&&callback=getShots";
	$.support.cors = true;
	$.getJSON(yql, showShots).success(function (data) {
		if (console && console.log){
			console.log(data);
		}
	});
}

function showShots(data) {
	if (data.shots.length) {
		$(data.shots).each(function () {
			var shot = this;
			$('#shots').append('<figure class="item"><a href="'+shot.short_url+'" title="'+shot.title+'" class="shot"><img src="'+shot.image_url+'" alt="'+shot.title+'" /></a><p class="stats"><a class="likes" href="'+shot.likes_count+'" title="'+shot.likes_count+' people like this"><span class="icon"></span> '+shot.likes_count+'</a> &middot; <a class="views" href="'+shot.views_count+'" title="'+shot.views_count+' people viewed this"><span class="icon"></span> '+shot.views_count+'</a></p></figure>')
		})
	} else {
		if (console && console.log){
			console.log("error");
		}
	}
}

function twitterInit() {
	twttr.anywhere(function (T) {
		T(".twitter").hovercards({
			username: function(node) {
				return node.rel;
			}
		});
	});
}
