/*!======================================================================*\
|| #################################################################### ||
|| # vBulletin 4.0.3
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2010 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// initialize the global facebook object once the page is ready
vB_XHTML_Ready.subscribe(
	function()
	{
		vBfb = new vB_Facebook();
	}
);

/**
* vBulletin Facebook Object
*
* @package	vBulletin
* @version	$Revision: 36076 $
* @date		$Date: 2010-03-31 16:37:59 -0700 (Wed, 31 Mar 2010) $
* @author	Michael Henretty, vBulletin Development Team
* @copyright	vBulletin Solutions Inc.
*/

function vB_Facebook()
{
	// if a receiver file was specified in FB settings, use that
	// otherwise use the default location and filename
	if (FB_RECEIVER != "") 
	{
		this.recieverFile = FB_RECEIVER;
	}
	else 
	{
		this.recieverFile = BBURL + "/receiver.php";
	}

	this.apikey = FB_APIKEY;
	
	// header login ids
	this.loginform_id = "navbar_loginform";
	this.loginform_usernameid = "navbar_username";
	this.loginform_passwordid = "navbar_password";
	
	// registration page facebook ids
	this.fb_usernameid = "facebookusername";
	this.fb_passwordid = "facebookpassword";
	
	// init the Facebook API
	FB.init(this.apikey, this.recieverFile);
}

/**
* invoked when user logins in using facebook connect
* 	- reloads the page from the server
*/
vB_Facebook.prototype.handle_fbLogin = function()
{
	// redirect to current page with facebook redirect parameter in query string
	var joinchar = (window.top.location.search.substring(1) ? "&" : "?");
	window.top.location = window.top.location + joinchar + "dofbredirect=1";
}

/**
* call back from the email permission check
*/
vB_Facebook.prototype.init_autoreg = function()
{
	YAHOO.util.Dom.addClass("registerform", "hidden");
	// ensure facebook has been initialized
	FB.ensureInit ( function () {
		FB.Connect.showPermissionDialog("email", vBfb.handle_emailpermcheck)
	});
}

/**
* call back from the email permission check
*/
vB_Facebook.prototype.handle_emailpermcheck = function(perms)
{
	// if the user did not grant fb permission, display the normal reg form
	if (!perms)
	{
		YAHOO.util.Dom.removeClass("registerform", "hidden");
	}
	// if the user did grant email permissions, submit the auto reg form
	else
	{
		YAHOO.util.Dom.get("fb_autoregform").submit();
	}
}

/**
* invoked from the registration page, used to login a vb user
* 	- and associate with facebook user
*/
vB_Facebook.prototype.login_and_associate = function()
{
	// get the login form, and username and password fields
	var loginformEl = fetch_object(this.loginform_id);
	var loginformUsernameEl = fetch_object(this.loginform_usernameid);
	var loginformPasswordEl = fetch_object(this.loginform_passwordid);
	
	// get the fb association form username and password fields
	var fb_usernameEl = fetch_object(this.fb_usernameid);
	var fb_passwordEl = fetch_object(this.fb_passwordid);
	
	// set the navbar login form fields
	if (loginformUsernameEl && fb_usernameEl)
	{
		loginformUsernameEl.value = fb_usernameEl.value;
	}
	if (loginformPasswordEl && fb_passwordEl)
	{
		loginformPasswordEl.value = fb_passwordEl.value;
	}
	
	// now submit the login form
	if (loginformEl)
	{
		// run the javascript sumbit routine
		// before submitting the form
		loginformEl.onsubmit();
		loginformEl.submit();
	}
}

/**
* invoked from the registration page, used to logout of facebook
*/
vB_Facebook.prototype.register_logout = function()
{
	FB.Connect.logoutAndRedirect(BBURL);
}

/*======================================================================*\
|| ####################################################################
|| # NulleD By - FintMax
|| # CVS: $RCSfile$ - $Revision: 36076 $
|| ####################################################################
\*======================================================================*/