var GM_fbc = Class.create({
	initialize: function( appid, receiver) {
		this.logging_enabled = false;
		this.appid = appid;
		this.receiver = receiver;
		this.fbInit();
	},
	fbInit: function() {
		FB.Facebook.init( this.appid, this.receiver );
	},
	fbReady: function( callback, option, optional ) {
		FB.Facebook.get_sessionState().waitUntilReady(function() {
			this._log("Session is ready");
			this._log( "CALLBACK: " + callback )
			this.fb_sess = FB.Facebook.apiClient.get_session();
			this._log("SESSION");
			this._print_r(this.fb_sess);
			FB.Facebook.apiClient.users_getInfo( this.fb_sess['uid'], new Array('name'), function(result,ex){
				//this._print_r(result);
				this.fb_user = result;
				this._log( 'logging fb_user' );
				try {
					this.fb_user = this.fb_user[0];
					this._print_r(this.fb_user);
					if( this.fb_user.uid ) {
						this._log( "return true");
						if( callback ) {
							switch( callback ) {
								case 'fbLoginOrRegister':
									this.fbLoginOrRegister();
								break;
								case 'fbComment':
									this.fbComment( option, optional );
								break;
								case 'fbLogout':
									this.fbLogout();
								break;
							}
						}
						return true;
					} else {
						this._log( "return false");
						return false;
					}	
				} catch( e ) {}
			}.bind(this));
		}.bind(this));
	},
	fbLoginOrRegister: function() {
		this._log('loginOrRegister');
		p = new Array();
		p.push( 'fb_uid='+this.fb_user.uid );
		p.push( 'username=' + this.fb_user.name );
		p = p.join( '&' );
		var req = new Ajax.Request( '/?op=fb&action=loginOrRegister', {
			parameters: p,
			onSuccess: function(transport) {
				txt = transport.responseText;
				txt = txt.replace('<script language="javascript" type="text/javascript">','');
				txt = txt.replace('</script>','');
				this._log( txt );
				eval(txt);
			}.bind(this)
		});
	},
	fbComment: function( id, cid ) {
		p = new Array();
		p.push( 'id=' + id );
		p.push( 'cid=' + cid );
		p = p.join( '&' );
		var req = new Ajax.Request( '/?op=fb&action=publishComment', {
			parameters: p,
			onSuccess: function(t) {
				this._log(t.responseText);
				this.fbPublishComment( eval("("+t.responseText+")") );
			}.bind(this)
		});
	},
	fbPublishComment: function( data ) {
		this._log('publishComment');
		this._log(data);
		this._log('calling apiclient');
		this._log("Bundle: " + data.bundle );
		this._log("TDdata: " + data.template );
		try{
			//FB.Facebook.apiClient.feed_publishUserAction( data.bundle, data.template );	
			FB.Connect.showFeedDialog(data.bundle, data.template);
		} catch( e ) {
			console.log( e );
		}

		this._log('done calling apiclient');
	},
	fbLogout: function() {
		this._log("calling fb logout");
		FB.Connect.logout( function() { ganjaLogout( undefined, 'topnavbar'); });
	},
	/*******
	 * utility methods below here
	 **/
	_print_r: function( obj ) {
		for (var key in obj) {
			if( typeof(obj[key]) == 'object' ) {
				this._log("KEY " + key + " is OBJ:" );
				this._print_r( obj[key] );
			} else {
				this._log( key + ": " + obj[key]);
			}
		}
	},
	_log: function( str ) {
		if( this.logging_enabled ) {
			if( window.console ) {
				console.log( str );
			}			
		}
	}
});

function redrawFBML()
{
	// only needed if delivering via ajax
	FB.Bootstrap.requireFeatures(["XFBML"], function() {
		FB.XFBML.Host.parseDomTree();
	});
}
