var api = null;
var user = null;
var loginType = null;
var formName = null;
var attachment = null;
var actionLinks = null;
var postTested = false;
var emailTested = false;
var submitForm = true;

function facebookLogin(type,form,subForm,postAttach,postAction) {
	loginType = type;
	formName = form;
	attachment = postAttach;
	actionLinks = postAction;
	postTested = false;
	emailTested = false;
	submitForm = subForm;
	FB.Connect.get_status().waitUntilReady(function(status) {
	switch(status) {
		case FB.ConnectState.appNotAuthorized:
		case FB.ConnectState.userNotLoggedIn:
			FB.Connect.requireSession(function() {
				logInSite();
			});
			break;
		case FB.ConnectState.connected:
			logInSite();
			break;
		}
	});
}
function logInSite() {
	api = FB.Facebook.apiClient;
	user = api.get_session().uid;
	if (loginType == 'post') {
		postTested = false;
		postItem();
	}
	else {
		emailTested = false;
		sendInfo();
	}
}
function sendInfo() {
	var quary = "SELECT email FROM permissions WHERE uid=" + user;
	api.fql_query(quary, function(rows) {
		if (!emailTested && !rows[0].email)
			FB.Connect.showPermissionDialog("email",function() {
				emailTested = true;
				sendInfo();
			});
		else if (rows[0].email && (emailTested || loginType == 'splash')) {
			var theForm = getForm();
			//var quary = "SELECT first_name, last_name, email, current_location FROM user WHERE uid=" + user;
			//api.fql_query(quary, function(rows) {
			api.users_getInfo(user,['first_name', 'last_name', 'email', 'current_location'],function(rows){
				if (rows[0].first_name != null && theForm.first_name) theForm.first_name.value = rows[0].first_name;
				if (rows[0].last_name != null && theForm.last_name) theForm.last_name.value = rows[0].last_name;
				if (rows[0].email != null && theForm.email_address) theForm.email_address.value = rows[0].email;
				if (rows[0].current_location != null) {
					if (rows[0].current_location.city != null && theForm.city) theForm.city.value = rows[0].current_location.city;
					if (rows[0].current_location.state != null && theForm.state) theForm.state.value = stateSwitch(rows[0].current_location.state);
					if (rows[0].current_location.zip != null && theForm.zip) theForm.zip.value = rows[0].current_location.zip;
				}
				if (submitForm) {
					theForm.submit();
				}
			});
		}
		else if (loginType != 'splash'){
			postTested = false;
			postItem(user,api,false);
		}
	});
}
function postItem() {
	var quary = "SELECT publish_stream FROM permissions WHERE uid=" + user;
	api.fql_query(quary, function(rows) {
		if (rows[0].publish_stream) {
			FB.Connect.streamPublish('', attachment, actionLinks);
		}
		else if (!postTested) {
			FB.Connect.showPermissionDialog("publish_stream",function() {
				postTested = true;
				postItem();
			});
		}
	});
}
function getForm() {
	for (i=0;i<document.getElementsByTagName('form').length;++i)
		if (formName == document.getElementsByTagName('form').item(i).name)
			return document.getElementsByTagName('form').item(i);
}

/* --------------------------- */

function stateSwitch(state) {
	switch(state) {
		case 'Alabama':
			state = 'AL';
			break;
		case 'Alaska':
			state = 'AK';
			break;
		case 'Arizona':
			state = 'AZ';
			break;
		case 'Arkansas':
			state = 'AR';
			break;
		case 'California':
			state = 'CA';
			break;
		case 'Colorado':
			state = 'CO';
			break;
		case 'Connecticut':
			state = 'CT';
			break;
		case 'Delaware':
			state = 'DE';
			break;
		case 'District of Columbia':
			state = 'DC';
			break;
		case 'Florida':
			state = 'FL';
			break;
		case 'Georgia':
			state = 'GA';
			break;
		case 'Hawaii':
			state = 'HI';
			break;
		case 'Idaho':
			state = 'ID';
			break;
		case 'Illinois':
			state = 'IL';
			break;
		case 'Indiana':
			state = 'IN';
			break;
		case 'Iowa':
			state = 'IA';
			break;
		case 'Kansas':
			state = 'KS';
			break;
		case 'Kentucky':
			state = 'KY';
			break;
		case 'Louisiana':
			state = 'LA';
			break;
		case 'Maine':
			state = 'ME';
			break;
		case 'Maryland':
			state = 'MD';
			break;
		case 'Massachusetts':
			state = 'MA';
			break;
		case 'Michigan':
			state = 'MI';
			break;
		case 'Minnesota':
			state = 'MN';
			break;
		case 'Mississippi':
			state = 'MS';
			break;
		case 'Missouri':
			state = 'MO';
			break;
		case 'Montana':
			state = 'MT';
			break;
		case 'Nebraska':
			state = 'NE';
			break;
		case 'Nevada':
			state = 'NV';
			break;
		case 'New Hampshire':
			state = 'NH';
			break;
		case 'New Jersey':
			state = 'NJ';
			break;
		case 'New Mexico':
			state = 'NM';
			break;
		case 'New York':
			state = 'NY';
			break;
		case 'North Carolina':
			state = 'NC';
			break;
		case 'North Dakota':
			state = 'ND';
			break;
		case 'Ohio':
			state = 'OH';
			break;
		case 'Oklahoma':
			state = 'OK';
			break;
		case 'Oregon':
			state = 'OR';
			break;
		case 'Pennsylvania':
			state = 'PA';
			break;
		case 'RhodeIsland':
			state = 'RI';
			break;
		case 'South Carolina':
			state = 'SC';
			break;
		case 'South Dakota':
			state = 'SD';
			break;
		case 'Tennessee':
			state = 'TN';
			break;
		case 'Texas':
			state = 'TX';
			break;
		case 'Utah':
			state = 'UT';
			break;
		case 'Vermont':
			state = 'VT';
			break;
		case 'Virginia':
			state = 'VA';
			break;
		case 'Washington':
			state = 'WA';
			break;
		case 'West Virginia':
			state = 'WV';
			break;
		case 'Wisconsin':
			state = 'WI';
			break;
		case 'Wyoming':
			state = 'WY';
			break;
		default:
			state = state;
	}
	return state;
}