/* make empty objects */
fb_logger = new Object();
fb_logger.info = new Array();

fb_logger.attempt_login = function() {

	FB.login(function(response) {
		
		if (response.session.uid) {
			/*console.log(response.session.uid);*/
			// logged in and connected user, someone you know

			fb_logger.info['access_token'] = response.session.access_token;
			fb_logger.info['base_domain'] = response.session.base_domain;
			fb_logger.info['expires'] = response.session.expires;
			fb_logger.info['secret'] = response.session.secret;
			fb_logger.info['session_key'] = response.session.session_key;			
			fb_logger.info['sig'] = response.session.access_gig;			
			fb_logger.info['uid'] = response.session.uid;
			
			fb_logger.login();

		} else {

			// no user session available, someone you dont know
			/*console.log('I dont think youre logged in');*/
		}
	}, {perms:'read_stream,publish_stream,offline_access, email'});

}

fb_logger.login = function() {

	/*
	 * send their facebook id over to a table to see if it exists
	 * if it does, look up their username and password based on the corresponding userID
	 * then log them in and redirect them to the home page
	 *
	 */
	var serialized_string = fb_logger.stringify(fb_logger.info);
	$j.ajax({
		type: "GET",
		url: "loader.php?modAct=fb_login",
		data: "fb_info="+serialized_string,
		success: function(msg){
			fb_logger.handle_results(msg);
		}
	});

}

fb_logger.handle_results = function(msg) {
	/* console.log('msg: '+msg); */
	var messageObj = eval( "(" + msg + ")" );

	if (messageObj['username']) {
		/*console.log('username = '+messageObj['username']);*/
		
		window.location='login.php?sendto='+location.href;

		return false;
	} else {
		/*console.log('error = '+messageObj['error']);*/
		
		var output_text = messageObj['error'];

		
		$('popupModalBK3').innerHTML = '<div id=\"systemNotifBox\"><div class=\"systemNotifContent\">'+output_text+'<div style=\"text-align:right;padding-top:10px;\"><a href=\"\"><img src=\"images/close_btn.jpg\" alt=\"close\"/></a></div></div>';
		sm('popupModalBK3', 400, 400, false);

		return true;

	}

}

/* I stole this since there is no fucntion built-in to stringify that is all-browser compatible */
fb_logger.stringify = function (jsonData) {
	var strJsonData = '{';
	var itemCount = 0;
	for (var item in jsonData) {
		if (itemCount > 0) {
			strJsonData += ', ';
		}
		temp = jsonData[item];
		if (typeof(temp) == 'object') {
			s = fb_logger.stringify(temp);
		} else {
			s = '"' + temp + '"';
		}
		strJsonData += '"' + item + '":' + s;
		itemCount++;
	}
	strJsonData += '}';
	return strJsonData;
}

fb_logger.reg = function () {

	FB.login(function(response) {
		/*console.log(response);*/
		if (response.session.uid) {
			/*console.log(response.session.uid);*/
			// logged in and connected user, someone you know

			fb_logger.info['access_token'] = response.session.access_token;
			fb_logger.info['base_domain'] = response.session.base_domain;
			fb_logger.info['expires'] = response.session.expires;
			fb_logger.info['secret'] = response.session.secret;
			fb_logger.info['session_key'] = response.session.session_key;
			fb_logger.info['sig'] = response.session.access_gig;
			fb_logger.info['uid'] = response.session.uid;
			
			fb_logger.get_user_info();			
		}
	}, {perms:'read_stream,publish_stream,offline_access, email'})
		
}


fb_logger.get_user_info = function() {

	/*
	 * send their facebook id over to a tale to see if it exists
	 * if it does, look up their username and password based on the corresponding userID
	 * then log them in and redirect them to the home page
	 *
	 */
	var serialized_string = fb_logger.stringify(fb_logger.info);
	$j.ajax({
		type: "GET",
		url: "loader.php?modAct=fb_reg",
		data: "fb_info="+serialized_string,
		success: function(msg){
			fb_logger.handle_reg_results(msg);
		}
	});

}

fb_logger.handle_reg_results = function(msg) {
	/* console.log('msg: '+msg); */
	//var messageObj = eval( "(" + msg + ")" );

	if($('firstName')==undefined) {

		window.location='compare_users-a171.html';
	} else {

		window.location = location.href;
	}


	//if (messageObj['name']) {
		/*console.log('username = '+messageObj['username']);*/

		//$('firstName').value = messageObj['first_name'];
		//$('lastName').value = messageObj['last_name'];
		//$('email').value = messageObj['email'];
		// maybe in the future we can access these, let's not delete them just yet'
		//$('address1').value = messageObj['address']['street'];
		//$('city').value = messageObj['address']['city'];
		//$('state').value = messageObj['address']['state'];
		//$('zip').value = messageObj['address']['zip'];
		//$('phone').value = messageObj['mobile_phone'];



		//return false;
	//} else {
		/*console.log('error = '+messageObj['error']);*/

		//var output_text = messageObj['error'];
		/*
	 * this is not using a javascript close, on click on the close link stupidly REFERSHES THE PAGE,
	 * which defeats any gains of using ajax behavior in the first place.
	 */
		//$('popupModalBK3').innerHTML = '<div id=\"systemNotifBox\"><div class=\"systemNotifContent\">'+output_text+'<div style=\"text-align:right;padding-top:10px;\"><a href=\"\"><img src=\"images/close_btn.jpg\" alt=\"close\"/></a></div></div>';
		//sm('popupModalBK3', 400, 400, false);

		//return true;

	//}

}

