jQuery('document').ready(function(){


//document.location.href='https://www.facebook.com/dialog/oauth?client_id='+fbClientID+'&redirect_uri='+ document.location.href +' &scope=email,public_profile&display=popup';

var dapfbloginkey = jQuery('input[name="dapfbloginkey"]').val(); 
dap_fb_init(dapfbloginkey);
jQuery('#login_template_wrapper .side_template_fb_btns').unbind('click').bind('click',function(){
	    
	    window.dap_global_login_parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
	    var dapfbloginkey = jQuery('input[name="dapfbloginkey"]').val(); 
	    
	    if(dapfbloginkey == ''){
			var msg = "Sorry, it appears that the Facebook API key is missing in the DAP config.";
			dap_member_set_error_message(msg);
			return false;
		}
	    
	    var loading_html = jQuery(this).html();
	    jQuery('.dap_fblogin_text').html(loading_html);
	    var dap_loader_html = jQuery('.dap_loader_html').html(); 
	    jQuery(this).html(dap_loader_html);
		var ajaxurl = admin_ajaxurl;
		jQuery('#login_template_wrapper .signin_btn').removeClass('fbloginaliasactive'); 
		jQuery('#dap_member_login_frontend_shortcode input[name="dap_fblogin_email"]').val('');
		
		daploginFB('test',1); 
		
	});
});	

function dap_fb_init(dapfbloginkey){
	  window.fbAsyncInit = function() {
	FB.init({
	  appId      : dapfbloginkey,
	  status     : true, // check login status
	  cookie     : true, // enable cookies to allow the server to access the session
	  oauth      : true, // enable OAuth 2.0
	  xfbml      : true,  // parse XFBML  
	  version    : 'v2.5'
	}); 
	
  };
  (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/en_US/sdk.js#xfbml=1&appId=<?php echo $fb_api_key; ?>&version=v2.5";
		fjs.parentNode.insertBefore(js, fjs);
	  }(document, 'script', 'facebook-jssdk'));
}


function daploginFB(redirect_url,btnclicked) {
	var ajaxurl = admin_ajaxurl;
	var showloadergif = 'Y';
    FB.login(function(response) {
	  //User logged in!
	if (response.authResponse) {
		
		var emails;
		FB.api('/me?fields=email', function(response) {
        emails =  response.email ;
        console.log("emails:"+emails);
        dapCheckFBAccuontIsConnectedByEmail(emails);
     });
     }else {
		jQuery('#login_template_wrapper .side_template_fb_btns').html(jQuery('.dap_fblogin_text').html());
	}
  }, {scope:"email,public_profile"});

}


function dapCheckFBAccuontIsConnectedByEmail(email){
	       
		if(email == ''){
			return false;
		}
		action_id = jQuery('input[name="action_id"]').val(); 
		//dap_member_show_loader();
		jQuery.post(admin_ajaxurl, {
			action: 'dapFbConnectionAjax',
			email: email,
			action_id: action_id,
		}, function(returnval) {
			  dap_member_hide_loader();
			  returnval = JSON.parse(returnval);
			  console.log(returnval);
			  if(returnval.error){
				  dapFBAccountAddEmailAlias(email);
				  return false;
			  }
			
			  //dap_member_show_loader();
			  $form = jQuery("<form name='authenticate' action='"+returnval.form_action+"' method='POST' id='dapfbloginform'></form>");	
			  $form.append('<input type="hidden" name="email" value='+returnval.email+' >');	
			  $form.append('<input type="hidden" name="password" value='+returnval.pass+' >');	
			  $form.append('<input type="hidden" name="request" value='+returnval.post_login_url+' >');		
			  $form.append('<input type="hidden" name="dslredirect" value='+returnval.cur_login+' >');	
			  $form.append('<input type="hidden" name="action" value="page_template_submit" >');	
			  $form.append('<input type="hidden" name="submitted" value='+returnval.submit+'>');	
			  jQuery('body').append($form);			
			  jQueryDM('form#dapfbloginform').submit(); 
		  
	});// ajax ends here
	
}


function dapFBAccountAddEmailAlias(email = ''){
	
	if(email == ''){
		return false;
	}
	
	var facebooklinkmsg = jQuery('.facebooklinkmsg').html();
	
	
	
	swal.fire({
      title: "",
      html: facebooklinkmsg,
     // icon: "warning",
      customClass: {container: 'swal_dap_fb_login_wrapper'},
	  showCancelButton: true,
	  confirmButtonColor: '#5bb25e',
      //cancelButtonColor: '#efefef',
      confirmButtonText: 'Yes',
      cancelButtonText: 'No',
      dangerMode: true,
   }).then((result) => {
	if (result.value) {
		 jQuery('#login_template_wrapper .login-withfb-section').hide(); 
		 jQuery('#login_template_wrapper .signin_btn').addClass('fbloginaliasactive'); 
		 jQuery('#dap_member_login_frontend_shortcode input[name="dap_fblogin_email"]').val(email);
		 
		 jQuery('.dap-social-login-heading').after('<span class="dap-social-fb-login-heading" >'+jQuery('input[name="facebooklinkheadingmsg"]').val()+'<span>');
	
      }
      
	  jQuery('#login_template_wrapper .side_template_fb_btns').html(jQuery('.dap_fblogin_text').html());
      
    })
}



