/*
 * Our <fb:login-button> specifies this function in its onlogin attribute,
 * which is triggered after the user authenticates the app in the Connect
 * dialog and the Facebook session has been set in the cookies.
 */
//need to figure out how to get rid of button, cuz it fires the same event if they click it and are already logged in

function facebook_onlogin_ready(message, user_state) {
	console.log("Successful connection with Facebook");
	
	if (message) {
		jQuery.facebox("<div style='text-align: right; font-size: 16px; font-weight: bold; height:50px; font-family: lucida grande,tahoma,verdana,arial,sans-serif'>Welcome, <fb:name uid=\"loggedinuser\" useyou=false linked=\"false\"></fb:name>&nbsp;" +
			"<fb:profile-pic uid=\"loggedinuser\" facebook-logo=\"true\" size=\"square\"></fb:profile-pic></div><div style=\"margin-top: 10px; font-size: 9pt;\">"+
			unescape(message) +
			"</div>");
		FB.XFBML.Host.parseDomTree(); 
	}
	else {
		fbc_show_offline_access_permission_dialog(user_state);
	}
	
	//doesn't always parse the dome tree. the only way to ensure is set a timeout, but we would rather go for almost instant profile pic load 90% of the time
	//var answer=confirm("Connecting your Facebook user account with your CollegeConfidential account allows CollegeConfidential to:\n1. Notify your when people reply to your subscribed threads\n2. Publish your activity to your wall\n\nYou control how we access your account directly from the CollegeConfidential control panel!  To get started, you need to authorize CollegeConfidential with Facebook.  You can disconnect your accounts at any time through Facebook or your control panel.  If you click 'no' we will not store any of your facebook data.");
	//	
//  refresh_page();
}

// getPageScroll() by quirksmode.com
function getPageScroll() {
  var xScroll, yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;	
  }
  return new Array(xScroll,yScroll) 
}


function fbc_initial(user_state) {
	
	
	
	
	jQuery(document).bind('beforeReveal.facebox', function() {
		 
	jQuery('#facebox').css('top', getPageScroll()[1] + 125 );
	jQuery('#facebox').css('left', jQuery(window).width() / 2 - (jQuery('#facebox').width() / 2));
	
	jQuery(".fbc_close").bind("click", function(e) {
		jQuery(document).trigger('close.facebox');	
	});
	jQuery(".fbc_ok").bind("click", function(e) {
		fbc_show_offline_access_permission_dialog();	
	});
	
	FB.XFBML.Host.parseDomTree(); 
	});
}

//SHOWS DIALOG FOR USER TO ACCEPT EXTENDED PERMISSION OR NOT
//object 'accepted' is a string with a comma separated list of the permissions the user granted.
//On failure, or if the user canceled the permissions dialog, null gets returned.
function fbc_show_offline_access_permission_dialog(user_state) {
	FB.Connect.showPermissionDialog('offline_access', function(accepted) {
			fbc_show_publish_stream_permission_dialog(user_state);
			
	}) ;
}

function fbc_show_publish_stream_permission_dialog(user_state) {
	FB.Connect.showPermissionDialog('publish_stream', function(accepted) {
		if(user_state=="new_user") {
			window.location = 'profile.php?do=editoptions&fbc=reg#facebook_connect_options';
		}
		else if(user_state=="current_user"){
			if(window.location.href.match('profile.php\\?do=editoptions&fbc=update#facebook_connect_options') || window.location.href.match('profile.php\\?do=editoptions&fbc=reg#facebook_connect_options')) {
				location.reload();
			}
			else {
				window.location = 'profile.php?do=editoptions&fbc=update#facebook_connect_options';
			}
		}
		else {
			window.location = 'profile.php?do=editoptions&fbc=reg#facebook_connect_options';
		}
	} ) ;
}






/*







 * The facebook_onload statement is printed out in the PHP. If the user's logged in
 * status has changed since the last page load, then refresh the page to pick up
 * the change.
 *
 * This helps enforce the concept of "single sign on", so that if a user is signed into
 * Facebook when they visit your site, they will be automatically logged in -
 * without any need to click the login button.
 *
 * @param already_logged_into_facebook  reports whether the server thinks the user
 *                                      is logged in, based on their cookies
 *
 
function facebook_onload(already_logged_into_facebook) {
  // user state is either: has a session, or does not.
  // if the state has changed, detect that and reload.
  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
          var is_now_logged_into_facebook = session ? true : false;

          // if the new state is the same as the old (i.e., nothing changed)
          // then do nothing
          if (is_now_logged_into_facebook == already_logged_into_facebook) {
            return;
          }

          // otherwise, refresh to pick up the state change
          refresh_page();
        });
    });
}

function facebook_permission_popup {
	FB.Bootstrap.EnsureInit(function() {
		
		
	
	
	
	
	
	
	});
}





 * Do a page refresh after login state changes.
 * This is the easiest but not the only way to pick up changes.
 * If you have a small amount of Facebook-specific content on a large page,
 * then you could change it in Javascript without refresh.
 
function refresh_page() {
  window.location = 'index.php';
}


 * Prompts the user to grant a permission to the application.
 
function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}

function publish_js_comment(form_bundle_id, post_title, post_url) {
	document.getElementsByName("fbconnect_js_submit")[0].disabled="disabled";
	comment_text = document.getElementsByName("comment")[0].value;
	var template_data = {
		"post-title":post_title,
		"post-url":post_url,
		"post":comment_text
	};
	try {
		facebook_publish_feed_story(form_bundle_id, template_data);
	} catch(err) {
		document.getElementsByName("comment")[0].value = "";
		document.getElementById("fbconnect_result").innerHTML = "Error publishing story: " + err.description;
	}
	document.getElementsByName("comment")[0].value = "";
	document.getElementById("fbconnect_result").innerHTML = "Published story via Javascript to your profile feed!";
	document.getElementsByName("fbconnect_js_submit")[0].disabled="";
}


 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published.
 *
 
function facebook_publish_feed_story(form_bundle_id, template_data) {
  // Load the feed form
  FB.ensureInit(function() {
          FB.Connect.showFeedDialog(form_bundle_id, template_data);
          //FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);
  });
}


 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 *
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 
function facebook_show_feed_checkbox() {
  FB.ensureInit(function() {
      FB.Connect.get_status().waitUntilReady(function(status) {
          if (status != FB.ConnectState.userNotLoggedIn) {
            // If the user is currently logged into Facebook, but has not
            // authorized the app, then go ahead and show them the feed dialog + upsell
            checkbox = document.getElementById('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}

// SHOWS DIALOG FOR USER TO ACCEPT EXTENDED PERMISSION OR NOT
function fbc_show_status_update_permission_dialog() {
	FB.Connect.showPermissionDialog('status_update', function(accepted) { window.location.reload(); } );
}

// SHOWS DIALOG FOR USER TO ACCEPT EXTENDED PERMISSION OR NOT
function fbc_show_offline_access_permission_dialog() {
	FB.Connect.showPermissionDialog('offline_access', function(accepted) { window.location.reload(); } );
}










*/
