var ssoServer = "http://member.herevgo.com/sign-in.php";
var defineUrlPath = "http://www.skoolbuz.com/_module/";
var phpFile = defineUrlPath + "sign-on/sign-on_function.php";
var ssoClient = document.location.host;

$.confglob = {
    fullSitePath : "http://www.skoolbuz.com/"
};


function ssoSync(){

	$.ajax({
		url: ssoServer,
		type: 'GET',
		data: 'm=status&client='+ssoClient,
		dataType: 'jsonp',
		dataCharset: 'jsonp',
		success: function(data){
			$.post(
				phpFile, {
					act: "store",
					id: data[0].id,
					email: data[0].email,  
					username: data[0].username, 
					key: data[0].key, 
					m: "save-status"
				}, function(d){
					
					if(d.flag == "true"){
						
						window.location.reload();		
						
					}else if(d.flag == "regis"){
						
						self.location.href = defineUrlPath + "profile/signup_complete.php";
						
					}else{

                                        }

				},"json"
			);
			
		}
	});
	
}


function logoutSingleSync(){

    $.ajax({
		      type: "GET",
                      data: '',
		      url: "http://www.zheza.com/datacenter/logout.php",
                      dataType: 'jsonp',
		      dataCharset: 'jsonp',
		      success: function() {}
    });//End Ajax
}


function loginByFB(){
	
											preloadShow();
											
                                            window.fbAsyncInit = function() {
                                                    FB.init({
                                                        appId : '105134999568422',
                                                        status : true, // check login status
                                                        cookie : true, // enable cookies to allow the server to access the session
                                                        xfbml : true // parse XFBML
                                                    });

                                                    // whenever the user logs in, we refresh the page
                                                    FB.Event.subscribe('auth.login', function() {
                                                        window.location.reload();
                                                    });

                                                    FB.login(function(response) {

                                                        if(response.status=='connected'){
                                                            //window.location.reload();                                                            
                                                            //self.location.href = "http://www.skoolbuz.com/";

                                                            var fbsend = 'act=fblogin';

                                                            $.ajax({
                                                              type: "POST",
                                                              url: defineUrlPath + "facebook-api/facebook-sign-on-function.php",
                                                              data: fbsend,
                                                              success: function(ret) {

                                                              }
                                                            });//End Ajax

                                                        }

                                                    });
                                                };

                                                (function() {
                                                    var e = document.createElement('script');
                                                    e.src = document.location.protocol + '//connect.facebook.net/th_TH/all.js';
                                                    e.async = true;
                                                    document.getElementById('fb-root').appendChild(e);
                                                }());

}

function fbLogin2SKB() {   																								                                            												
												
												window.fbAsyncInit = function() {
                                                    FB.init({
                                                        appId : '105134999568422',
                                                        status : true, // check login status
                                                        cookie : true, // enable cookies to allow the server to access the session
                                                        xfbml : true // parse XFBML
                                                    });

                                                    // whenever the user logs in, we refresh the page
                                                    FB.Event.subscribe('auth.login', function(response) {
                                                        
														if(response.status=='connected'){
															
															window.location.reload();
														}
														
                                                    });

                                                    FB.login(function(response) {
														
                                                        if(response.status=='connected'){

                                                            var fbsend = 'act=fblogin';

                                                            $.ajax({
                                                              type: "POST",
                                                              url: defineUrlPath + "facebook-api/facebook-utilities-function.php",
                                                              data: fbsend,
                                                              success: function(ret) {
																		
                                                                       var obj = $.evalJSON(ret);

                                                                       if(obj.flag == true && obj.status == 1){
																		   		preloadHide();
                                                                             	window.location.reload();
                                                                       }else if(obj.flag == false && obj.status == 2){
																		   	var nElement = $("div#footerSlideContainer").length;
																			
																			if(nElement > 0){																																							
																					callFBConfirmBox();																				
																			}else{																				
																					createFBconfirmbox();
																					callFBConfirmBox();																					
																			}
																																				
																	   }else{
                                                                            return false;
                                                                       }

                                                              }
                                                            });//End Ajax

                                                        }

                                                    }, {scope:'email,user_birthday,user_status,user_photos,read_stream,publish_stream'});
                                                };

                                                (function() {
                                                    var e = document.createElement('script');
                                                    e.src = document.location.protocol + '//connect.facebook.net/th_TH/all.js';
                                                    e.async = true;
                                                    document.getElementById('fb-root').appendChild(e);
                                                }());
}


function callFBConfirmBox(){
	
	
	
												//Start Clear Error Message
												$('.fb_cmem_password_error').html("");																
												//End Clear Error Message
																								
												$("#footerSlideContainer").show();
												$('#footerSlideContent').animate({ height: '236' });
												$('.btnClose').css('backgroundPosition', 'bottom left');
																	
												openfbox = true;
														
												
												return openfbox;

}


function createFBconfirmbox(){
	
											var htmlAppBox = '';
											
											htmlAppBox += '';
											htmlAppBox += '<form id="frmFBMember" name="frmFBMember" method="post"><div id="footerSlideContent"><div id="footerSlideText">';
											htmlAppBox += '<div class="boxConfirmFb"><div class="btnClose"><a href="javascript: void(0)"></a></div>';
											htmlAppBox += '<div class="mainMenuLogin" id="mainMenuLoginSwitch">';
											htmlAppBox += '<div>';
											htmlAppBox += '<span><input name="email" id="fb_cmem_username" type="text" value="กรอกอีเมล์สมาชิกสคูลบัส"/></span>';
											htmlAppBox += '<span><input name="password" id="fb_cmem_password" type="password" value="HelloSkoolbuz"/></span>';
											htmlAppBox += '<span class="fb_cmem_password_error errorRed" for="fb_cmem_password" id="fb_cmem_password_error" style="margin-left: 1px; margin-top: -2px;"></span>';
											htmlAppBox += '<div class="btnOk"><a href="javascript: void(0)"></a></div>';
											htmlAppBox += '<div class="clear"></div>';
											htmlAppBox += '<div id="falseSignin" style="display: none;"><span class="txtError" style="color: #FF0000;padding-top: 7px;"><strong>อีเมล หรือ รหัสผ่าน ไม่ถูกต้อง</strong></span></div>';
											htmlAppBox += '</div>';			
											htmlAppBox += '</div>';
											htmlAppBox += '<div class="clear"></div>';
                                        	htmlAppBox += '<div id="loadSignin" style="display: none;"><img src="' + $.confglob.fullSitePath + 'web_images/preload/ajax-loader.gif" style="padding-top: 80px; margin-left: -43px; text-align: left;"/> <br /><span style="padding-top: 80px; margin-left: -25px; text-align: left; color: #FFF;">กำลังดำเนินการตรวจสอบบัญชีผู้ใช้</span></div>'
                                        	htmlAppBox += '<div id="successSignin" style="display: none;"><img src="' + $.confglob.fullSitePath + 'web_images/preload/ajax-loader.gif" style="padding-top: 80px; margin-left: -43px; text-align: left;" /> <br /><span style="padding-top: 80px; margin-left: -63px; text-align: left; color: #FFF;">ลงทะเบียนเข้าสู่ระบบผ่าน Facebook เสร็จสมบูรณ์</span></div>';	
											htmlAppBox += '<div id="alreadyAccount" style="display: none;"><img src="' + $.confglob.fullSitePath + 'web_images/preload/ajax-loader.gif" style="padding-top: 80px; margin-left: -43px; text-align: left;" /> <br /><span style="padding-top: 80px; margin-left: -63px; text-align: left; color: #FF0000;">Facebook Account นี้ได้ถูกใช้ลงทะเบียนในระบบแล้ว</span></div>';											
											htmlAppBox += '</div></div></div></form>';
											
											$(".appBoxInfo").attr("id","footerSlideContainer");
											$("#footerSlideContainer").html(htmlAppBox);
											
											
											return true;
}



//Sign-On Engine



$(document).ready(function(){
	

  	var fbsend = 'act=fbBtChk';

  	$.ajax({
      		type: "POST",
          	url: defineUrlPath + "facebook-api/facebook-utilities-function.php",
      		data: fbsend,
       		success: function(ret) {
       		var obj = $.evalJSON(ret);
				if(obj.flag == true){
						$(".connectFb").hide();
						$(".connectFb").remove();
				}
           	}
 	});//End Ajax


	$('a.single-sing-on-logout').click(function(e){
                
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){                                        

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                           	logoutSingleSync();
							self.location.href = defineUrlPath + "sign-on/index.php?action=logout&fblogout=true";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});

   	$('a.member-awards-logout').click(function(e){
              
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                    		logoutSingleSync();
							self.location.href = $.confglob.fullSitePath + "skoolbuz-teacher-awards2011/?action=logout";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});
	
	
	$('a.member-mvcontest-logout').click(function(e){
              
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                    		logoutSingleSync();
							self.location.href = $.confglob.fullSitePath + "mvContest/?action=logout";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});
	
	$('a.member-awards2011-logout').click(function(e){
              
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                    		logoutSingleSync();
							self.location.href = $.confglob.fullSitePath + "skb2011/?action=logout";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});
	
	$('a.member-moregang-logout').click(function(e){
              
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                           	logoutSingleSync();
							self.location.href = $.confglob.fullSitePath + "moregang-signon.php?action=logout";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});
	
	$('a.member-vecclub-logout').click(function(e){
              
		$.ajax({
			url: ssoServer,
			type: 'GET',
			data: 'act=logout&m=cross-sign-out&client'+ssoClient,
			dataType: 'jsonp',
			dataCharset: 'jsonp',
			success: function(data){
				if(data[0].status == 200){

					$.post(
						phpFile,{
							act: 'store',
							m: 'destroy-status'
						}, function(d){
                           	logoutSingleSync();
							self.location.href = $.confglob.fullSitePath + "vecclub-signon.php?action=logout";
						},"json"
					);
				}
			}
		});
		e.preventDefault();
	});
	
	//New Login Social API

	jQuery.fn.inputtoggle = function(){
			$(this).each(function(index){
					var myvalue = $(this).attr("value");

					$(this).focusin(function(){
							if($(this).val() == myvalue) $(this).val("");
					});
					$(this).focusout(function(){
							if($(this).val() === "") $(this).val(myvalue);
					});
			});
	};

	$("#email").inputtoggle();
	$("#password").inputtoggle();
	
});


runOnLoad(function(){
	
	ssoSync();
	
});

//End Sign-On Engine
