var	 ajax_kabupaten_url = '';
var  ajax_login_provider = '';

var setKabupatenAjaxURL = function(path){
	ajax_kabupaten_url = path;
}

var setUserProviderLoginAjaxURL = function(path){
	ajax_login_provider = path;
}

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

// Load the SDK asynchronously
(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 = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
/*INIT FB*/

function fb_login(){
    FB.login(function(response){
        console.log('statusChangeCallback');
        console.log(response);
        if (response.status === 'connected') {
			$.ajax({
				url : ajax_login_provider,
				dataType : 'json',
				type : 'post',
				data : {
					provider:"facebook",
					provider_id:response.authResponse.userID,
					provider_token:response.authResponse.accessToken
				},
				success : function(resp){
					if (resp.success){
						window.location.href = resp.message;
						//show_message('success', resp.message, '');
					}else{
						show_message('danger', resp.message, '');
					}
				},
				error : function (xhr, status, error){
					show_message('danger', error, '');
				}
			})
        } else {
        
        }            
    },
        {scope:'public_profile,email'}
    )
}

/*INIT GOOGLE*/
gapi.load('auth2', function(){
	auth2 = gapi.auth2.init({
		client_id: '42655519165-b4dgg33bnbql21gfij2jiepgad6nph6m.apps.googleusercontent.com',
		cookiepolicy: 'single_host_origin',
	});
	attachSignin(document.getElementById('google_sync'));
});

function attachSignin(element) {
	auth2.attachClickHandler(element, {},
		function(googleUser) {
			google_sync(googleUser);
		}, function(error) {
			show_message('danger', 'Tidak dapat terhubung dengan Google, mohon dicoba kembali.', '');
		}
	);
}
/*INIT GOOGLE*/

function google_sync(googleUser){
	var profile = googleUser.getBasicProfile();
	var id_token = googleUser.getAuthResponse().id_token;
	var	email = profile.getEmail();

	
	$.ajax({
		url : ajax_login_provider,
		dataType : 'json',
		type : 'post',
		data : {
			provider:"google",
			provider_id:email,
			provider_token:id_token
		},
		success : function(resp){
			if (resp.success){
				window.location.href = resp.message;
			}else{
				show_message('danger', resp.message, '');
			}
		},
		error : function (xhr, status, error){
			show_message('danger', error, '');
		}
	})

}

$(window).ready(function(e){



	var state = $("#form_title").attr('data-state');
	

	if (state=='login'){
		$("#login_form").show();
		$("#registration_form").hide();
		$("#forgot_password_form").hide();
		$("#form_title").html('Login');
	}else if (state=='forgot'){
		$("#registration_form").hide();
		$("#login_form").hide();
		$("#forgot_password_form").show();
		$("#form_title").html('Reset Password');
	}else if (state=='join'){
		$("#registration_form").show();
		$("#login_form").hide();
		$("#forgot_password_form").hide();
		$("#form_title").html('Join');
	}


	$(".forget_password_trigger_button").bind('click',function(e){
		$(".current_form").hide();
		$("#forgot_password_form").show();
		$("#form_title").html('Reset Password');
	})

	$(".register_trigger_button").bind('click',function(e){
		$(".current_form").hide();
		$("#registration_form").show();
		$("#form_title").html('Join');
	})

	$(".login_trigger_button").bind('click',function(e){
		$(".current_form").hide();
		$("#login_form").show();
		$("#form_title").html('Login');
	})

	$('select[name="user_aprov_id"]').bind('change',function(e){
		$("select[name=user_akab_id]").empty();
		$("select[name=user_akab_id]").hide();
		var user_aprov_id = $(this).val();
		if(user_aprov_id==''){
			return false;
		}else{
			$.ajax({
				url : ajax_kabupaten_url,
				dataType : 'json',
				type : 'post',
				data : {user_aprov_id:user_aprov_id},
				success : function(resp){
					if (resp.success){
						$("select[name=user_akab_id]").append(resp.message);
						$("select[name=user_akab_id]").show();
					}else{
						show_message('danger', resp.message, '');
					}
				},
				error : function (xhr, status, error){
					show_message('danger', error, '');
				}
			})
		}		 	
	});

	$("#login_form").bind('submit',function(e){
		e.preventDefault();
		$.ajax({		
			url : $("#login_form").attr('action'),
			type : $("#login_form").attr('method'),
			dataType : 'json',
			data : $("#login_form").serialize(),
			success : function(resp){
				if (resp.success){
					window.location.href = resp.message;
				}else{
					show_message('danger', resp.message, '');
				}
			},
            error : function (xhr, status, error){
		        show_message('danger', error, '');
            }
		})
	})

	$("#registration_form").bind('submit',function(e){
		e.preventDefault();
		$.ajax({		
			url : $("#registration_form").attr('action'),
			type : $("#registration_form").attr('method'),
			dataType : 'json',
			data : $("#registration_form").serialize(),
			success : function(resp){
				if (resp.success){
					show_message('success', resp.message, '');
					$('#registration_form')[0].reset();
				}else{
					show_message('danger', resp.message, '');
				}
			},
            error : function (xhr, status, error){
		        show_message('danger', error, '');
            }
		})
	})

	$("#forgot_password_form").bind('submit',function(e){
		e.preventDefault();
		$.ajax({		
			url : $("#forgot_password_form").attr('action'),
			type : $("#forgot_password_form").attr('method'),
			dataType : 'json',
			data : $("#forgot_password_form").serialize(),
			success : function(resp){
				if (resp.success){
					show_message('success', resp.message, '');
					$('#forgot_password_form')[0].reset();
				}else{
					show_message('danger', resp.message, '');
				}
			},
            error : function (xhr, status, error){
		        show_message('danger', error, '');
            }
		})
	})	

	$("#reset_password_form").bind('submit',function(e){
		e.preventDefault();
		$.ajax({		
			url : $("#reset_password_form").attr('action'),
			type : $("#reset_password_form").attr('method'),
			dataType : 'json',
			data : $("#reset_password_form").serialize(),
			success : function(resp){
				if (resp.success){
					show_message('success', resp.message, '');
					$('#reset_password_form')[0].reset();
					$("#reset_password_form").hide();
				}else{
					show_message('danger', resp.message, '');
				}
			},
            error : function (xhr, status, error){
		        show_message('danger', error, '');
            }
		})
	})	


})