
function fbConnectSteps (already_logged_into_facebook) {
	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;
			}

			if ($('backgroundOverlay') == null) {
				var arrayPageSize = [document.body.offsetWidth+document.documentElement.scrollLeft, document.body.offsetHeight+document.documentElement.scrollTop];
				var loaderTop  = 260;
				var loaderLeft = (arrayPageSize[0]-220)/2;
				var html = '<div id="backgroundOverlay" style="position: absolute; top: 0px; left: 0px; z-index: 190;';
				html += 'width: '+arrayPageSize[0]+'px; height: '+arrayPageSize[1]+'px; background-color: rgb(0, 0, 0); display:none;"></div>';
				html += '<img id="ajax-loader-big" src="/images/ajax-loader-big.gif" style="position:absolute;top:'+loaderTop+'px;left:'+loaderLeft+'px;z-index:191;display:none;" />';
				new Insertion.Bottom(document.body, html);
				new Effect.Appear('backgroundOverlay', {
					duration: 0.3,
					from: 0.0,
					to: 0.5
				});
			}

			$('backgroundOverlay').show();
			if ($('ajax-loader-big') != null) {
				$('ajax-loader-big').show();
			}

			// otherwise, refresh to pick up the state change
			var opt = {
				method: 'get',
				onSuccess: fbConnectStepsResult
			}

			new Ajax.Request('fb_connect?step=2', opt);
		});
	});

}

function fbConnectStepsResult(msg) {
	var myJSON = eval('(' + msg.responseText + ')');

	if (myJSON.result == '1') {

		if(myJSON.loggedInUser) {
			fbSignupProcessResult(msg);
		} else {
			if ($('fb_connect_window') == null) {
				new Insertion.Bottom(document.body, '<div id="fb_connect_window" style="width:500px;position:absolute;z-index: 200; left: 30%; top: 200px;"></div>');

			}

			$('fb_connect_window').innerHTML = myJSON.form;
			FB.init("", "/fb_connect");
			$('fb_connect_window').show();
		}
	} else {
		// recheck permissions

		// permission dialog
		FB.ensureInit(function() {
			FB.Connect.showPermissionDialog(myJSON.required_perms_string, function(perms){

				var opt = {
					method: 'get',
					onSuccess: fbConnectStepsResult
				}
				// publish_stream is optional so if user doesn't allow it we log the user in anyway
				//if (!perms && myJSON.required_perms_string == 'publish_stream') {
				if (myJSON.required_perms_string == 'publish_stream') {
					new Ajax.Request('fb_connect?step=2&login_user=1', opt);
				} else {
					new Ajax.Request('fb_connect?step=2', opt);
				}
			});
		});
	}
}

function fbConnectFormClose() {
	if ($('fb_connect_window') != null) {
		$('fb_connect_window').hide();
	}
	if ($('backgroundOverlay') != null) {
		$('backgroundOverlay').hide();
	}
	if ($('ajax-loader-big') != null) {
		$('ajax-loader-big').hide();
	}
}

function fbTextEnabled() {
	controlEnable = $("have_mineeds_account").checked;
	if(controlEnable) {
		$("fb_username_or_email").disabled = false;
		$("fb_username_or_email").style.backgroundColor = "#ffffff";
		$("fb_username_or_email").style.color = "#000000";
		$("fb_password").disabled = false;
		$("fb_password").style.backgroundColor = "#ffffff";
		$("fb_password").style.color = "#000000";
		$("fb_username_label").style.color = "";
		$("fb_password_label").style.color = "";

		$("fb_email").disabled = true;
		$("fb_email").style.backgroundColor = "#eeeeee";
		$("fb_email").style.color = "#777777";
		$("fb_email_label").style.color = "#cccccc";
	} else {
		$("fb_username_or_email").disabled = true;
		$("fb_username_or_email").style.backgroundColor = "#eeeeee";
		$("fb_username_or_email").style.color = "#777777";
		$("fb_password").disabled = true;
		$("fb_password").style.backgroundColor = "#eeeeee";
		$("fb_password").style.color = "#777777";
		$("fb_username_label").style.color = "#cccccc";
		$("fb_password_label").style.color = "#cccccc";

		$("fb_email").disabled = false;
		$("fb_email").style.backgroundColor = "#ffffff";
		$("fb_email").style.color = "#000000";
		$("fb_email_label").style.color = "";
	}
}

var fbEmailPermFlag = false;
function fbLastStep(required_perms_string) {
	fb_loading_process(1);
	if (required_perms_string) {
		if(!fbEmailPermFlag) {
			FB.ensureInit(function() {
				FB.Connect.showPermissionDialog(required_perms_string, fbDialogResult);
			});

		//FB.init("{$facebook_api_key}", "/fb_connect");
		} else {
			fbDialogResult();
		}
	} else {
		fbDialogResult();
	}
}

function fbLastStepResult(msg) {
	var myJSON = eval('(' + msg.responseText + ')');
	if (myJSON.result == '1') {
		fbSignupProcess();
	} else {
		if(myJSON.required_perms_string) {
			fbEmailPermFlag = false;
			$('fb_connect_error_msg').innerHTML = '<ul><li>'+myJSON.message+' <a href="javascript:;" onclick="fbLastStep(\''+myJSON.required_perms_string+'\');"><u>Try Again</u></a></li></ul>';
		} else {
			$('fb_connect_error_msg').innerHTML = '<ul><li>'+myJSON.message+'</li></ul>';
		}

		if(myJSON.extra == 'exists_email') {
			$("have_mineeds_account").checked = true;
			$("fb_username_or_email").value = $("fb_email").value;
			$("fb_email").value = "";
			fbTextEnabled();
		}

		$('fb_connect_error_msg').style.display = '';
		fb_loading_process(0);
	}

}

function fbDialogResult() {
	fbEmailPermFlag = true;
	var opt = {
		method: 'post',
		onSuccess: fbLastStepResult ,
		postBody: $('fb_connect_form').serialize()
	}

	new Ajax.Request('fb_connect?step=3', opt);
}

function fbSignupProcess() {
	var opt = {
		method: 'get',
		onSuccess: fbSignupProcessResult
	}

	new Ajax.Request('fb_connect?step=4', opt);
}

function fbSignupProcessResult(msg) {
	var myJSON = eval('(' + msg.responseText + ')');
	if (myJSON.result == '1') {
		//window.location.reload();
		//window.location.href='/my_profile';
                window.location.reload();
	}
}

function fb_loading_process(enabled) {
	if(enabled) {
		$('fb_login_btn').hide();
		$('fb_loading_img').show();
	} else {
		$('fb_login_btn').show();
		$('fb_loading_img').hide();
	}
}

function facebook_publish_feed_story(tempalteData) {

	tempalteData = eval('(' + tempalteData + ')');
	FB.ensureInit(function() {
		FB.Connect.streamPublish('', tempalteData);
	});

	if($('feed_loading')) {
		$('feed_loading').style.visibility = "hidden";
	}
}

