<?
//========================================
// dehead(config/function/lib) Include
//========================================
/*include $_SERVER['DOCUMENT_ROOT']."/front/include/dehead.php";

//========================================
// Meta Tag
//========================================
$metaTitle = $metaInfo['n_band']['title'];
$metaDescription = $metaInfo['n_band']['description'];
$metaKeyword = $metaInfo['n_band']['keyword'];
$metaImage = $metaInfo['n_band']['image'];

//========================================
// header Include
//========================================
include PATH_INCLUDE."header.php";*/
session_start();
?>
<meta charset="UTF-8">
<style>
#container2{
	width:720px;
	margin:auto;
	background-color:#aaa;
}
@media screen and (max-width:640px){
	#container2{
		width:100%;
	}
}
</style>
<div id="container">
	<div id="container2">
	<h2>???</h2>
	<a href="javascript:;" class="fb_login">로그인</a> 
	<a href="javascript:;" class="fb_logout">로그아웃</a>
	<a href="javascript:;" class="fb_get">세션확인</a>
	</div>
	
	<fb:login-button scope="public_profile,email" onlogin="checkLoginState();"></fb:login-button>
</div>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
<? if(!$_SESSION[name]){?>
window.fbAsyncInit = function() {
	FB.init({
	appId      : '280686338998871',
	cookie		: true,
	xfbml      : true,
	version    : 'v2.8'
	});
	checkLoginState();
};

$('.fb_get').click(function(){
	FB.getLoginStatus(function(res){
		console.log(res);
		alert(res.authResponse);
	});
});

(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'));


var checkLoginState = function(){
	FB.getLoginStatus(function(response){
		console.log('check');
		console.log(response);
		getStateChange(response);
	});
}
var getStateChange = function(response){
	if (response.status === 'connected') {
		
		alert('로그인성공');
		console.log('login ok');
		console.log(response);
		getAPI();
	}else if (response.status === 'not_authorized') {
		
		alert('안댐');
		console.log(response);
	} else {
		
		alert('아몰랑');
		console.log(response);
	}
}

var getAPI = function(){
	FB.api('/me',
		{locale:'ko_KR', fields: 'name,email,picture'},
		function(response){
			alert(response.name);
			console.log('FB.api');
			console.log(JSON.stringify(response));
			
			//var uid = response.authResponse.userID;
			var accessToken = FB.getAuthResponse()['accessToken'];//response.authResponse.accessToken;
			alert('dd');
			$.ajax({
				url: './event2_proc.php',
				type: 'post',
				data: {'id':response.email, 'name':response.name,'pic':response.picture.url},
				success: function(res){
					location.reload();
				},error: function(res){
					alert(res.status);
				}
			});
			alert('aa');
		}
	);
}

$('.fb_login').click(function(){
	FB.login(function(response) {
		getStateChange(response);
		<?/* if(2==1){?>
		if (response.status === 'connected') {
			
			alert('로그인성공');
			console.log('login ok');
			console.log(response);
			
			
			var uid = response.authResponse.userID;
			var accessToken = FB.getAuthResponse()['accessToken'];
			
			
			alert('토큰발행'+uid+' '+accessToken);
			FB.api('/me',
				{locale:'ko_KR', fields: 'name,email,picture'},
				function(response){
					alert(response.name);
					console.log('FB.api');
					console.log(JSON.stringify(response));
				}
			);
			
		} else if (response.status === 'not_authorized') {
			alert('안댐');
			console.log(response);
		} else {
			alert('아몰랑');
			console.log(response);
		}
		<?}*/?>
	},{scope:'public_profile,email'});
	
	console.log('loginEnd');
});

$('.fb_logout').click(function(){
	FB.logout(function(response){
		alert('로그아웃되었습니다.');
		location.reload();
	});
});
<?}else{?>
alert('<?=$_SESSION[id]?>');
<?}?>
</script>



<?

//========================================
// quick bar Include
//========================================
//include PATH_INCLUDE."quick.php";

//========================================
// footer Include
//========================================
//include PATH_INCLUDE."footer.php";
?>