$(document).ready(function(){
	
	// Click to start video
	if ($("#content li:first-child .meta").length) {
		$("#content li:first-child a").click(function() {
			$("#content").fadeTo(200, .3);
			$("#background img").fadeTo(200, .2);
			$("#wrapper").addClass("engaged");
			$("#content li:first-child").fadeOut();
			$("#player").show();
			if (!$("#player .flashContainer").length) {
				$("#player").youTubeEmbed({
					video : $(this).parent().find(".meta .videourl").text(),
					width : 910,
					height: 512
				});
			}
			$("#backlink").delay(1500).fadeIn("fast", function() {
				// play it
				$(".controlDiv.play").eq(0).delay(1500).click();
			});
			
			return false;
		});
	}
	
	// Hide video show content again
	$("#backlink, #background").click(function() {
		// pause it first
		$(".controlDiv.pause").eq(0).click();
		$("#player, #backlink").fadeOut("normal");
		$("#content").fadeTo(200, 1);
		$("#background img").fadeTo(200, 1);
		$("#content li:first-child").fadeIn();
		$("#wrapper").removeClass("engaged");
		return false;
		
	});
	
	// Hide video show content if Esc is hit
	// via http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery
	$(document).keyup(function(e) {
		if (e.which == 27) {   // esc
			$("#backlink").click();
			return true;
		}
	});

	// Social networking tools
	
	// Facebook
	$(".sntools .facebook").click(function() {
	
		addURL = "http://facebook.com/sharer.php?";
		addURL = addURL + "u=" + encodeURIComponent('http://' + window.location.hostname + $("#content ul li:first-child a").attr("href"));
		addURL = addURL + "&t=" + encodeURIComponent($("#content ul li:first-child .meta .title").text());
				
		if (typeof analyticsAccount != 'undefined') {
			_gaq.push(['_trackPageview', '/social/facebook/homepage/']);
		}
	
		window.open(addURL);
		
	});
	
	// Twitter
	$(".sntools .twitter").click(function() {
	
		// Random insert
		randomWords = "Shredding:";
	
		addURL = "http://twitter.com/?status=" + randomWords + " ";
		addURL = addURL + encodeURIComponent($("#content ul li:first-child div.meta span.title").text() + " - " + $("#content ul li:first-child div.meta span.videourl").text());
				
		if (typeof analyticsAccount != 'undefined') {
			_gaq.push(['_trackPageview', '/social/twitter/homepage/']);
		}

		window.open(addURL);

	});
	
	// Facebook like button
	window.fbAsyncInit = function() {
		FB.init({appId: "166635303351075", status: true, cookie: true, xfbml: true});
	};
	(function() {
		var e = document.createElement("script");
		e.async = true;
		e.src = document.location.protocol + "//connect.facebook.net/en_US/all.js";
		if ($("#fb-root").length) document.getElementById("fb-root").appendChild(e);
	}());
	
	$("#fb-root").after("<fb:like href=\"http://" + window.location.hostname + location.pathname + "\" layout=\"button_count\" show_faces=\"false\" width=\"50\" action=\"like\" font=\"verdana\" colorscheme=\"light\"></fb:like>");
			
});

