/* $Rev: 122 $ */
var _gaq = _gaq || [];

window.fbAsyncInit = function() {
	FB.init({appId: facebookSettings.appId, status: true, cookie: true, xfbml: true, oauth: true});
	FB.getLoginStatus(function(response){
		jQuery.facebookAuth(response);
	});
  	FB.Event.subscribe('auth.statusChange', function(response){
  		jQuery.facebookAuth(response);
  	});
  	FB.Event.subscribe('auth.login',function(response){
  		_gaq.push(['_trackSocial','facebook','login']);
  	});
  	FB.Event.subscribe('edge.create',function(response){
  		_gaq.push(['_trackSocial','facebook','like',response]);
  	});
  	FB.Event.subscribe('edge.remove',function(response){
  		_gaq.push(['_trackSocial','facebook','unlike',response]);
  	});
  	FB.Event.subscribe('message.send',function(response){
  		_gaq.push(['_trackSocial','facebook','send',response]);
  	});
  	FB.Event.subscribe('comment.create',function(response){
  		_gaq.push(['_trackSocial','facebook','comment']);
  	});
  	jQuery(document).trigger('facebookInit',[true]);
};

(function( $ ){
	var $initialized = false;
	var $authorized = false;
	var $session;
	var $user;
	
	var methods = {
		init: function(){
			$initialized = true;
		},
		
		main: function(response){
    		if (response.authResponse) {
      			//user is already logged in and connected
      			if(!$authorized){
	      			$authorized = true;
	      			$session = response.authResponse;
	      			
	      			FB.api('/me', function(response) {
	        			$user = response;
	        			$(document).trigger('facebookAuth',[true]);
	      			});
	      			
	      			try{
	      				$.getJSON(facebookSettings.url + '/facebook/',{accessToken:$session.accessToken});
	      			} catch (e) {/* Block out IE */}
	      		}
    		}
    		else
    		{
      			//user is not connected to your app or logged out
     			$(document).trigger('facebookAuth',[false]);
   			}
		},
		
		login: function(){
			FB.login(function(response) {
				if (response.authResponse) {
    				$authorized = true;
      				$session = response.authResponse;
      			
	      			FB.api('/me', function(response) {
	        			$user = response;
	        			$(document).trigger('facebookAuth',[true]);
	        			$(document).trigger('facebookLogin',[true]);
	      			});
	      			
	      			/* Do server side auth */
	      			try{
	      				$.getJSON(facebookSettings.url + '/facebook/',{accessToken:$session.accessToken});
	      			} catch (e) {/* Block out IE */}
  				} else {
    				//user cancelled login or did not grant authorization
  				}
			}, {scope:'email'});
			
		},
		
		logout: function(){
			FB.logout(function(){
				/* Clear Settings */
				$authorized = false;
				$session = null;
				$user = null;
				
				/* Clear server session */
				$.getJSON(facebookSettings.url + '/facebook/logout/');
				
				/* Trigger Event */
				$(document).trigger('facebookAuth',[false]);
				$(document).trigger('facebookLogout',[true]);
			});
		},
		
		getSession: function(){
			return $session;
		},
		
		getUser: function(){
			return $user;
		}
	}
	
	$.extend({
		facebookAuth: function( method ) {
	    	// Method calling logic
	    	if(!$initialized && methods[method]) methods.init.apply( this, Array.prototype.slice.call( arguments, 1 ));
	    	else if(!$initialized) methods.init.apply( this, arguments);
	    	
	    	if ( methods[method] ) {
	      		return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    	} else if ( typeof method === 'object' || ! method ) {
	      		return methods.main.apply( this, arguments );
	    	} else {
	      		$.error( 'Method ' +  method + ' does not exist on jQuery.callbackDialog' );
	    	}
		}
	});
})( jQuery );

(function( $ ){
	var $initialized = false;
	var $authorized = false;
	
	var $nameNode;
	var $menuNode;
	
	var methods = {
		init: function(options){
			
			$nameNode = $('#myspot-top-username');
			$menuNode = $('#myspot-top-menu');
			
			$(document).bind('facebookAuth', methods.authStatus);
			
			$('#fb-logout').click(function(){
				$.facebookAuth('logout')
			});			
			$('#fb-login').click(function(){
				$.facebookAuth('login')
			});	
			
			$initialized = true;
			methods.authStatus();
		},
		
		authStatus: function(){
			var u = $.facebookAuth('getUser');
			if(u){
				if(!$authorized){
					$nameNode.html('Welcome ' + u.name);
					$('#fb-logged-out').hide();
					$('#fb-logged-in').show();
					$authorized = true;
				}
			}
			else
			{
				$nameNode.html('MySpot');
				$('#fb-logged-in').hide();
				$('#fb-logged-out').show();
				$authorized = false;
			}
		}
	}
	
	
	$.extend({
		memberWidget: function( method ) {
	    	// Method calling logic
	    	if ( methods[method] ) {
	    		if(!$initialized)
	    			methods.init.apply( this, Array.prototype.slice.call( arguments, 1 ));
	      		return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    	} else if ( typeof method === 'object' || ! method ) {
	    		if(!$initialized)
	      			return methods.init.apply( this, arguments );
	    	} else {
	      		$.error( 'Method ' +  method + ' does not exist on jQuery.callbackDialog' );
	    	}
		}
	});
	
	$(document).bind('facebookInit', jQuery.memberWidget);
})( jQuery );
