/* Facebook Connect Simplified
 * Author: Andrew Munro <andrew.s.munro@gmail.com>
 * Date: February 4th, 2010
 * Version: 3
 * Package Version: Innocent Surf School
 */

(function() {
	var e = document.createElement('script');
	e.type = 'text/javascript';
	e.src = 'http://connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
}());

window.fbAsyncInit = function() {

    FB.init({
		apiKey : 'fb47bb4417690089c610f73416fec35b',
		status : true,
		cookie : true,
		xfbml  : true  
    });

	FB.getLoginStatus(function(response) {
		/*
		if (response.session) {
 			$FB.common_users = new Array(); 
 			$FB.session      = response.session;
 			$FB.logged_in    = true;
 			$FB.load();
		} else {
			FB.Event.subscribe('auth.login', function(response) {
	 			$FB.common_users = new Array(); 
 				$FB.session      = response.session;
 				$FB.logged_in    = true;
 				$FB.load();
			});
		}
		*/
	});
}

var $FB = {
	site_name:    'Surf Simply',
	site_link:    'http://www.surfsimply.com',
	common_users: [],
	session:      {},
	load: function ()
	{
		if (!$FB.session) 
		{
			return;
		}
		$FB.userid = $FB.session.uid;
		if (!$FB.userid) 
		{
			return;
		}
		$('facebook-connect').innerHTML = (
			'<div>'+
				'<div class="left" style="width:50px; margin-right:5px;">'+
					'<fb:profile-pic uid="loggedinuser" facebook-logo="true" linked="true" size="square"></fb:profile-pic>'+
				'</div>'+
				'<div class="left" style="width:112px; line-height:16px;">'+
					'Hey, <fb:name uid="loggedinuser" useyou="false" linked="false" firstnameonly="true"></fb:name>!<br/>'+
					'<span style="font-size:8px;">(<a onclick="FB.logout($FB.logout()); return false;" href="#" style="text-decoration:none;">logout</a>)</span>'+
				'</div>'+
				'<div class="clear"></div>'+
			'</div>'
		);
		FB.XFBML.parse();
	},
	logout: function ()
	{
 		if ($('facebook-connect')) 
 		{
			$('facebook-connect').innerHTML = (
				'<h3 style="margin-bottom:11px; margin-top:0px;">'+
					'<img src="/wp-content/themes/innocentsurfschool/lib/connect-icon-small.gif" style="vertical-align:middle"/> Login with Facebook'+
				'</h3>'+
				'<div style="font-size:11px;">'+
					'Connecting your Facebook account to our blog lets you add comments, share things you like and see which of your friends have connected too.'+
					'<div style="height:10px;"></div>'+
					'<fb:login-button size="medium" length="long">Login With Facebook</fb:login-button>'+
				'</div>'
			)
			FB.XFBML.parse();
		}
	},
	stream: function ()
	{
		FB.ui({
			method: 'stream.publish',
			attachment: {
				name: 'Connect',
				caption: 'The Facebook Connect JavaScript SDK',
				description: (
        'A small JavaScript library that allows you to harness ' +
        'the power of Facebook, bringing the user\'s identity, ' +
        'social graph and distribution power to your site.'
				),
				href: 'http://fbrell.com/'
			},
			action_links: [
				{ text: 'fbrell', href: 'http://fbrell.com/' }
			]
		});
	},
	share: function (url)
	{
		if (url)
		{
			url = url;
		}
		else 
		{
			url = window.location.href;
		}
		var share = {
			method: 'stream.share',
			u: url
		};
		FB.ui(share);
	}
}