function trace(msg) {
	try {
		if(console) {
			console.log(msg);
		}
	} catch(e) {
	}
}

function facebook_login() {
	// Bootstrap is only default FB feature loaded -- make it load connect
	FB.Bootstrap.requireFeatures(["Connect"], function() {
		// The object with your API and path parameters should be included in your header. Something like:
		//	DT = new Object(); DT.FBConnect = new Object();
		//	DT.FBConnect.apiKey = "{$smarty.const.FB_CONNECT_APIKEY}";
                //	DT.FBConnect.connectPath = "/connect/"; 

		//trace("Got connect. API:"+DT.FBConnect.apiKey+" path:"+DT.FBConnect.connectPath);
		// Initiatialize the FB 
		FB.init(DT.FBConnect.apiKey, DT.FBConnect.connectPath+"xd_receiver.htm");	
		FB.Connect.requireSession();
		FB.Facebook.get_sessionState().waitUntilReady(
			function(s) {
				//trace("Session ready:"+s+"-- checking for AB account");
				$.getJSON('/ajax/check_credentials.php?type=facebook', function(u) {
						if(!u.has_ab_account) {
							//trace('New account');
							$.popup.iframe('/register.php?popup=1&facebook_reg=1', {width: 600, height: 660});
							//jQuery.facebox('You are a new account');
						} else {
							trace('Has account. Info already stored in server session. Activate login.');
							window.location = ('/login/?persistent=on&processlogin=1&return=/');
						}
				});
			}
		);
	});
}

function facebook_logout() {
	FB.Bootstrap.requireFeatures(["Connect"], function() {
			FB.init(DT.FBConnect.apiKey, DT.FBConnect.connectPath+"xd_receiver.htm");
			FB.Connect.ifUserConnected(
				function() { FB.Connect.logoutAndRedirect('/logout?return=/'); },
				function() { window.location = ('/logout?return=/'); }
			);
	});	
}	
