//var ssoURL = "http://202.142.212.166/herevgo/sign-in.php";
//http://202.142.212.166/herevgo/member/sign-in.php
var ssoServer = "http://member.herevgo.com/sign-in.php";
var ssoClient = "202.142.212.86";
var phpFile = "http://www.zhezafighter.com/_api/login/sign-in-sample.php";
var statusFile = "http://www.zhezafighter.com/_api/login/check-status.php";
var loginForm = "formlogin";

$(document).ready(function(){
	__init__();	
});

function __init__(){	
	/*
	$('form[name="'+loginForm+'"]').submit(function(e){		
		signIn();		
		e.preventDefault();
	});	
	$('#sign-out-button').click(function(e){		
		signOut();		
		e.preventDefault();
	});
	*/
}

function ssoSync(){
	$.ajax({
		url: ssoServer,
		type: 'GET',
		data: 'm=status',
		dataType: 'jsonp',
		dataCharset: 'jsonp',
		success: function(data){
			if( data[0].status == 200 ) {
				$.post(
					phpFile, {
						id: data[0].id,
						email: data[0].email,  
						username: data[0].username, 
						displayname: data[0].displayname,
						credit: data[0].credit,
						key: data[0].key, 
						m: "save-status"
					}, function(d){
						if( d != null ) {
							if( d.talert ) alert(d.talert);
							if(d.action == 'reload') { 
								window.location.reload();
							}
						}
					},"json"
				);
			} else {
				$.post(
					phpFile,{
						m: 'destroy-status'
					}, function(d){
						if( d.action == 'reload' ) window.location.reload();
					},"json"
				);
			}
		}
	});
	
}

function loadingStatus(){
	$('#login-status').html('กำลังตรวจสอบสถานะ ...');
}

function signIn(){
	var postData = $('form[name="'+loginForm+'"]').serialize();
	
	$.ajax({		
		url: ssoServer,
		type: 'GET',
		data: postData,
		dataType: 'jsonp',
		dataCharset: 'jsonp',
		success:function(data){
			if(data[0].status == 200){
				$.post(phpFile, {
					id: data[0].id,
					email: data[0].email,  
					username: data[0].username, 
					displayname: data[0].displayname,
					credit: data[0].credit,
					key: data[0].key, 
					m: "save-status" 
				}, function(d){
					if( d != null ) {
						if( d.talert ) alert(d.talert);
						if(d.action == 'reload') { 
							window.location.reload();
						}
					}
				},"json");
				
			}else{
				alert(data[0].description);
			}
			
		}
	});
}

function signOut() {
	LoutOut( false );
}

function LoutOut( flash ) {
	var logout = true;
	if( flash ) {} 
	else {
		logout = confirm("คุณต้องการ Logout ใช่หรือไม่?" );
	}
	
	if( logout ) {
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'm=cross-sign-out',
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){
					$.post(
						phpFile,{
							m: 'destroy-status'
						}, function(d){
							if( !flash ) window.location.reload();
							else window.location.href = "http://www.zhezafighter.com";
							/*loadingStatus();
							$.get(
								statusFile,{
									
								},function(html){
									$('#login-status').html(html);
								},"html"
							);*/
						},"json"
					);
				}
			}
		});	
	}
}


