<?
//========================================
// 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";*/
?>
<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>
//페이스북
/*function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response)
}*/
/* 매뉴얼대로 
function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
	
	if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      //document.getElementById('status').innerHTML = 'Please log ' +
        //'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      //document.getElementById('status').innerHTML = 'Please log ' +
      //  'into Facebook.';
    }
}
function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
    //  document.getElementById('status').innerHTML =
    // 'Thanks for logging in, ' + response.name + '!';
	});
}
/*매뉴얼끝*/
/*
function checkLoginState() {
  FB.getLoginStatus(function(response) {
	  console.log('chkloginstate');
	  console.log(response);
	//  statusChangeCallback(response);
  });
}
*/
window.fbAsyncInit = function() {
	FB.init({
	appId      : '280686338998871',
	cookie		: true,
	xfbml      : true,
	version    : 'v2.8'
	});
	//FB.AppEvents.logPageView();
	/*
	FB.getLoginStatus(function(response) {
		console.log('normal');
		console.log(response);
		if(response.status == 'unknown'){
			alert('비로그인 상태!');
		}else{
			alert('로그인상태!');
		}
	},true);*/
	//checkLoginState();
	FB.getLoginStatus(function(response){
		statusChangeCallback(response);
	});
};

$('.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 b_getLoginStatus = function(response){
	FB.getLoginStatus(function(response) {
		console.log('getLoginStatus');
		console.log(response);
		if(response.status == 'unknown'){
			console.log('비로그인 상태!');
		}else{
			console.log('로그인상태!');
		}
	});
}
*/


  function statusChangeCallback(response) {

    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.

        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;

      getAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
        //notLogin();
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
        //notLogin();
    }
  }
  
  


var checkLoginState = function(){
	FB.getLoginStatus(function(response){
		console.log('check');
		console.log(response);
		getStateChange(response);
	});
}
var getStateChange = function(response){
	if (response.status === 'connected') {
		// Logged into your app and Facebook.
		alert('로그인성공');
		console.log('login ok');
		console.log(response);
		getAPI();
	}else if (response.status === 'not_authorized') {
		// The person is logged into Facebook, but not your app.
		alert('안댐');
		console.log(response);
	} else {
		// The person is not logged into Facebook, so we're not sure if
		// they are logged into this app or not.
		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;
		}
	);
}

$('.fb_login').click(function(){
	FB.login(function(response) {
		getStateChange(response);
		<? if(2==1){?>
		if (response.status === 'connected') {
			
			// Logged into your app and Facebook.
			alert('로그인성공');
			console.log('login ok');
			console.log(response);
			
			
			var uid = response.authResponse.userID;
			var accessToken = FB.getAuthResponse()['accessToken'];//response.authResponse.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));
				}
			);
			
			/*
			FB.getLoginStatus(function(response) {
				console.log('getstat');
				console.log(response);
				if(response.status === 'connected'){
					console.log(response.authResponse.accessToken);
					var uid = response.authResponse.userID;
					var accessToken = response.authResponse.accessToken;
					alert('토큰발행'+uid+' '+accessToken);
					FB.api('/me',
						{locale:'ko_KR', fields: 'name,email,picture'},
						function(response){
							alert(response.name);
							console.log(response);
						}
					);
					//location.reload();
				}				
			},true);
			*/
			
			
			
			
			
			/*FB.api('/me',
				{locale:'ko_KR', fields: 'name,email,picture'},
				function(response){
					console.log(response);
				}
			);*/
		} else if (response.status === 'not_authorized') {
			// The person is logged into Facebook, but not your app.
			alert('안댐');
			console.log(response);
		} else {
			// The person is not logged into Facebook, so we're not sure if
			// they are logged into this app or not.
			alert('아몰랑');
			console.log(response);
		}
		//location.reload();
		<?}?>
	},{scope:'public_profile,email'});
	
	console.log('loginEnd');
});

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



<?

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

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