// JavaScript Document

$('#login_form').ajaxForm( {
	target:		'#login_acceptor',
	beforeSubmit: function() {
	if( $("#email").val() =="" || $("#password").val()==""){
		$("#logout_suc, #login_false").hide();
		$("#login_empty").fadeIn();
		return false; }
	$("#login_acceptor").html('<img src="images/loading.gif" alt="loading..." />');
	},
	error:		function() {
		$("#login_panel").prepend('<span class="err3">系統錯誤，無法登入！<br /></span>');
	}
	//success:	afterlogin   // This has done by the echo of login.php and it will be put in target option.
});

// logout ajax
function logout() {
	  $.ajax({
      url: 'php_functions/logout.php',
      type: 'POST',
	  cache: false,
      data: {
        logout: true
      },
	  dataType: 'html',
      error: function(xhr) {
		  $("#login_panel").prepend('<span class="err3">系統錯誤，無法登出！<br /></span>');
      },
      success: function(response) {
          $("#login_acceptor").html(response);
		  afterlogout();
      },
	  beforeSend: function(){
		  $("#login_acceptor").html('<img src="images/loading.gif" alt="loading..." />');
	  }
    });
}

// after login
function afterlogin() {
	$("#login_panel").hide();
	$("#mycard_panel").show();
	$("#mycard_login").load("php_functions/after_login.php"); 
	//$("#profile .c_b_content").load(location.href+" #profile>*","");
	load_stars();
}

// after logout
function afterlogout() {
	$("#login_empty, #login_false, #mycard_panel").hide();
	$("#login_panel, #logout_suc").fadeIn();
	$("#email, #password").attr("value", "");
	$("#mycard_login").html("");
	$("div#profile > div.c_b_content").html('<script type="text/javascript">location.href=("index.php");</script>'); // Clean Profile Page
	load_stars();
}

function load_stars(){
	if( typeof(star_options) != "undefined"){
	$.ajax({
			url: 'php_functions/rank.php', type: 'POST', cache: false,
			data: star_options,
			dataType: 'html',
			error: function(xhr) { $("#stars").html('<span class="err3">無法載入！</span>'); },
			success: function(response) { $("#stars").html(response); },
			beforeSend: function(){ $("#stars").html('<img src="images/loading.gif" alt="loading..." />'); }
		});
	}
}