function FBonConnected(user_id){
	//if (SESSaccounttype == "" and user_id !="") this means that the user is not logged it at all.
	//so run script to login user and update session variables. send user_id to script.
	//else do nothing.
	$(function(){
		if(accountType == '' && user_id != ''){
			$.ajax({
				url: root + '/includes/fb_login.php',
				type: 'POST',
				data: 'user_id=' + user_id,
				success: function(){
					window.location.reload();
				}
			});		
		}
	});
} 
function FBonNotConnected(){ 
     FB.ensureInit(function(){
        FB.Connect.get_status().waitUntilReady(function(status){
            if(status == FB.ConnectState.userNotLoggedIn){
				//use is not logged into facebook so they shouldnt be logged into tp either....
				//if SESSaccounttype = facebook then run script to clear all session variables...
				$(function(){
					if(accountType == 'facebook'){
						$.ajax({
							url: root + '/includes/fb_logout.php',
							type: 'POST',
							data: 'authorized=1&user_id=' + accountID,
							success: function(){
								window.location.reload();
							}
						});		
					}
				});
            }
            if(status == FB.ConnectState.appNotAuthorized){
                //"User is logged into FB but this app is not authorized"
				//user has not authorized tp to use their info so...
				//if SESSaccounttype = facebook then run script to clear all session variables...
				$(function(){
					if(accountType == 'facebook'){
						$.ajax({
							url: root + '/includes/fb_logout.php',
							type: 'POST',
							data: 'authorized=0&user_id=' + accountID,
							success: function(){
								window.location.reload();
							}
						});		
					}
				});				
            }
        });
    });
}
function fbLogout(){
	FB.Connect.logout(function(){
		$(function(){
			$.ajax({
				url: root + '/includes/fb_logout.php',
				type: 'POST',
				data: 'authorized=1&user_id=' + accountID,
				success: function(){
					window.location.reload(); 
				}
			});				
		});	
	});
	return false;
}

//not being used but keeping it around for example purposes.
function fbClearCookies(){
	FB.ensureInit(function(){ 
		FB.Facebook.apiClient.fql_query("SELECT uid from user WHERE uid IN ("+accountID+")", function(result, ex) {  
			if(result == null){  
				//first delete cookies
				document.cookie ='e4bd252023cf9fc7e33e68ad9ecc9b3d_expires=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='e4bd252023cf9fc7e33e68ad9ecc9b3d_session_key=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='e4bd252023cf9fc7e33e68ad9ecc9b3d_ss=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='e4bd252023cf9fc7e33e68ad9ecc9b3d_user=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='e4bd252023cf9fc7e33e68ad9ecc9b3d=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='fbsetting_e4bd252023cf9fc7e33e68ad9ecc9b3d=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				document.cookie ='base_domain_e4bd252023cf9fc7e33e68ad9ecc9b3d=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;';
				window.location.reload();
			}
		}); 	
	});	
}

function showInviteFriendsDialog(title, header, invite_text, next_page, type){
       var the_fbml = '\
           <fb:fbml>\
               <fb:request-form \
			type="' + type + '"  \
			content=" ' + invite_text + '" \
			invite="true" \
			action="' + next_page + '" \
			method="POST"> \
               <fb:multi-friend-selector showborder="true" cols="5" rows="3" bypass="cancel" actiontext="' + header + '">  \
               </fb:request-form> \
           </fb:fbml>';
       var dlg = new FB.UI.FBMLPopupDialog(title, the_fbml);
       dlg.setContentWidth(625);
       dlg.setContentHeight(550);
       dlg.set_placement(FB.UI.PopupPlacement.center);
       dlg.show();
}
