// Build the sharing functions.
function buildShare() {
	encodedUrl = encodeURIComponent(window.location);
	$("#countfacebook .countbutton").click(function(){ window.open('http://www.facebook.com/sharer.php?u='+encodedUrl+'&t='+encodeURIComponent(document.title), "Share", 'height=350,width=700'); });

	tweetString = document.title+" : "+window.location;
	$("#counttwitter .countbutton").click(function(){ window.open('http://twitter.com/home?status='+encodeURIComponent(tweetString), "Tweet"); });
}


$(document).ready(function() {
	if(document.images){
		fb=new Image();fb.src="/_img/facebook-button-over.jpg";
		tw=new Image();tw.src="/_img/twitter-button-over.jpg";
	}
	
	buildShare();

	$("#countfacebook .countbutton img").hover(function(){ $(this).attr("src","/_img/facebook-button-over.jpg"); },
	function() { $(this).attr("src","/_img/facebook-button.jpg"); });

	$("#counttwitter .countbutton img").hover(function(){ $(this).attr("src","/_img/twitter-button-over.jpg"); },
	function() { $(this).attr("src","/_img/twitter-button.jpg"); });


	// Get number of tweets about this page.
	$.getJSON(
		'http://api.tweetmeme.com/url_info.jsonc?url='+encodeURIComponent(window.location)+'&callback=?',
		function(data) {
			if(data.story.url_count > 0) $("#counttwitter .countnumber").show().text(data.story.url_count);
		}
	);

	// Get number of facebook shares of this page.
	facebookFetch = encodeURIComponent('http://api.facebook.com/restserver.php?method=links.getStats&urls='+encodeURIComponent(window.location)),
	$.get(
		"/crossFetch.php?url="+facebookFetch,
		function(data) {
			var shareCount = $(data).find('share_count').text();
			if(shareCount > 0) $("#countfacebook .countnumber").show().text(shareCount);
		}
	);
});
