function FBConnect(baseimg) {

	var key;
	var receiver;
	var wLogin = null;
	
	this.oFlashData = new Object();
	this.BASE_IMG = baseimg;
	
	this.init = function( key, receiver ) {
		this.key = key;
		this.receiver = receiver;
		FB.init(this.key, this.receiver, {"ifUserConnected":function( user_id ) {fbConnect.setUserConnected(true,user_id)}, "ifUserNotConnected":function(){fbConnect.setUserConnected(false)}});  
		
	}
	
	this.connect = function() {
		FB.Connect.requireSession(null, isUserActionHint=true);
	}
	

	this.setUserConnected = function( connected, fbId ) {
		this.oFlashData.connected = connected;
		this.oFlashData.fbId = (connected ? Number(fbId) : null);
		
		// callbacks not registered until app is fully loaded, so wait.
		if( document.getElementById("main").onConnectChange ) {
			document.getElementById("main").onConnectChange( connected, fbId );
		}
	
	}
	
	this.logout = function() {
		FB.Connect.logout(null);
	}
	
	this.revokeAuthorization = function() {
		FB.Facebook.apiClient.revokeAuthorization( this.oFlashData.fbId, function() {fbConnect.setUserConnected( false );} );
	}
	
	this.getFriendInfo = function() {
		FB.Facebook.apiClient.friends_getAppUsers( function(result) { fbConnect.onReceiveAppUsers( result ); } );
	}
	
	this.onReceiveAppUsers = function( aUsers ) {
		FB.Facebook.apiClient.users_getInfo(aUsers, 'name, pic_square', function(result) { fbConnect.onReceiveAppUsersDetail( result ); } );
	}
	
	this.onReceiveAppUsersDetail = function( result ) {
		var info = "";
		for( var i = 0; i < result.length; i++ ) {
			for( var key in result[ i ] ) {
				info += key + "/" + result[ i ][key] + "\n";
			}
		}
		alert( info );
	}
	
	this.publishNewTokens = function( data ) {
		var aImages = data.split("|");
		var images = new Array();
		for( var i = 0; i < aImages.length-1; i++ ) {
			images[ i ] = {'type':'image','src':this.BASE_IMG+aImages[ i ]+'.jpg','href':'http://iceage3.tokenzone.com/us/'};
		}
	
		var message = 'just got new Ice Age 3 Tokens!'; 
		var attachment1={'name':'My new tokens',
			'href':'http://iceage3.tokenzone.com/us/',
			'description':'Start your own Ice Age 3: Dawn of the Dinosaurs Tokenzone if you haven\'t already and help your friends complete their collections.',
			'caption':'These are 3 new Tokens that {*actor*} got via Auto-Deposit',
			'media':images};
		var action_links = [{'text':'Get your own Tokens', 'href':'http://iceage3.tokenzone.com/us/'}]; 
		FB.Connect.streamPublish(message, attachment1, action_links, null, null, function(result) { fbConnect.checkStreamPermission(); }, true);
	}
	
	this.checkStreamPermission = function() {
		FB.Facebook.apiClient.users_hasAppPermission("publish_stream", function(result) { fbConnect.onCheckStreamPermission(result); } );
	}
	
	this.onCheckStreamPermission = function(result) {
		if (result == 0) fbConnect.requestStreamPermission();
	}
	
	this.requestStreamPermission = function() {
		FB.Connect.showPermissionDialog("publish_stream", null, false);
	}
	
	this.inviteFriends = function(inviteStr) {
		var dialog = new FB.UI.FBMLPopupDialog('Invite your friends to join you on Tokenzone', '');
		var fbml = "<fb:fbml> <fb:request-form action=\"http://iceage3.tokenzone.com/us/\" method=\"GET\" invite=\"true\" type=\"Ice Age 3 Tokenzone\" content=\"Come join me at the Ice Age: Dawn of the Dinosaurs Tokenzone and help me complete my collection of fun, exclusive movie stills and clips. <fb:req-choice url=\'" + inviteStr + "\' label=\'Confirm\' />\"> <fb:multi-friend-selector showborder=\"false\" exclude_ids=\"\" actiontext=\"Invite your friends to collect the Ice Age 3 Tokenzone\" rows=\"4\" bypass=\"cancel\" /> </fb:request-form> </fb:fbml>";
		dialog.setFBMLContent(fbml);
		dialog.setContentWidth(760);  
		dialog.setContentHeight(580);
		dialog.show();
	}
	
	this.getFlashData = function() {
		return this.oFlashData;
	}

	return this;
}