$(document).on('click','.fb_login',function(){
	FBLogin();
});
$(document).on('click','.fb_logout',function(){
	FBLogout();
});
$(document).on('click','.fb_get',function(){
	//$('#status').html(FBNAME+"("+FBID+") <img src='"+FBPIC+"' />");
});
$(document).on('click','.btnStart',function(){
	location.href = "./step2.php";
});
$(document).on('click','.fb_share',function(){
	FB.ui({
	  method: 'share',
	  href: uri,
	}, function(response){});
});

var FBID;
var FBNAME;
var FBPIC;
var FBAGE;
var FBGENDER;
var FBTKN;


function statusChangeCallback(response) {
	
	if (response.status === 'connected') {
		getAPI();
		
		
		var nameset = function(){
			if(typeof FBNAME != 'undefined'){
				
				if(step == 1){
					$('#fbcontrols').html('').prepend('<a href="javascript:;" class="btnStart commonLBtn">'+FBNAME+'님으로 시작하기</a>');		
					$('#fbcontrols2').html('').append('<a href="javascript:;" class="fb_logout" style="float:right;">로그아웃</a>');
				}
				
				//키보드 숨기기 또는 내리기
				if(step == 1){
					var field = document.createElement('input');
					field.setAttribute('type', 'text');
					document.body.appendChild(field);

					setTimeout(function() {
						field.focus();
						setTimeout(function() {
							field.setAttribute('style', 'display:none;');
						}, 50);
					}, 50);
				}
				
				
			}else{
				window.setTimeout(nameset, 100);
			}
		
		}
		nameset();
		
	} else if (response.status === 'not_authorized') {
		$('#fbcontrols').html('').prepend('<a href="javascript:;" class="fb_login commonLBtn">페이스북 계정으로 로그인</a> ');
		
		if(step > 1){
			location.href="step1.php";
		}
	} else {
		$('#fbcontrols').html('').prepend('<a href="javascript:;" class="fb_login commonLBtn">페이스북 계정으로 로그인</a> ');
		
		if(step > 1){
			location.href="step1.php";
		}
	}
}

function checkLoginState() {
	FB.getLoginStatus(function(response) {
		statusChangeCallback(response);
	});
}

window.fbAsyncInit = function() {
	FB.init({
		appId      : '280686338998871',
		cookie     : true,  // enable cookies to allow the server to access 
                        // the session
		xfbml      : true,  // parse social plugins on this page
		version    : 'v2.5' // use graph api version 2.5
	});

	FB.getLoginStatus(function(response) {
		statusChangeCallback(response);
	});
};

(function(d, s, id) {
	var js, fjs = d.getElementsByTagName(s)[0];
	if (d.getElementById(id)) return;
	js = d.createElement(s); js.id = id;
	js.src = "//connect.facebook.net/ko_KR/sdk.js";
	fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function getAPI() {
	FB.api('/me',
		//{locale:'ko_KR', fields: 'name,email,picture,first_name,last_name,age_range,link,gender,locale,timezone,updated_time,verified'},
		{locale:'ko_KR', fields: 'name,email,picture,age_range,gender'},
		function(response) {
		//console.log(response);
		
		FBID = response.email;
		FBNAME = response.name;
		FBPIC = response.picture.data.url;
		FBAGE = response.age_range.min;
		FBGENDER = response.gender;
	});
}
function FBLogin(){
	FB.login(function(response) {
		statusChangeCallback(response);
	},{scope:'public_profile,publish_actions,email'});
}
function FBLogout(){
	FB.logout(function(){
		location.reload();
	});
}
