var sa = {};

sa.frontMongoDB =1 ;
sa.frontMySQL = 1;
sa.backMongoDB = 1;

sa.quiet = 1;

sa.init = function()
{
	
	if(sa.quiet == 1)
	{
		$("#login_loading").hide();
		$("#login_form").show();
	}
	
	sa.checkFrontMongoDB();
	sa.enterlogin();

	$("#customerEmail").focus(function(){
		$(this).css('background-color', '#e9e9e9');
	});
	
	$("#customerPassword").focus(function(){
		$(this).css('background-color', '#e9e9e9');
	});
	/*
	$("#formula").submit(function(){
		var ok = 1;
		
		if($("#customerEmail").val().length < 5) {
			$("#customerEmail").css('background-color', '#ffb9b9');
			$("#customerEmail").css("position", "relative");
			$("#customerEmail").animate({ left: 15 }, 30)
							.animate({ left: 0 }, 30)
							.animate({ left: -15 }, 30)
							.animate({ left: 0 }, 30);
							
			ok = 0;
		}
		
		if($("#customerPassword").val().length < 3) {
			$("#customerPassword").css('background-color', '#ffb9b9');
			$("#customerPassword").css("position", "relative");
			$("#customerPassword").animate({ left: 15 },20)
							.animate({ left: 0 },20)
							.animate({ left: -15 },20)
							.animate({ left: 0 },20);
							
			ok = 0;
		}
		
		
		if(ok == 1)
		{
			var pass = $("#customerPassword").val();
			var user = $("#customerEmail").val();
			$("#login_login").show();
			$("#login_form").hide();
			var done = 0;
			
			$.ajax({
			  url: '/socialagent_central/trunk/public/auth/login',
			  type: 'POST',
			  data: "customerEmail="+user+"&customerPassword="+pass,
			  complete: function(XMLHttpRequest, textStatus) {
			  	done = 1;
				return true;
			  },
			  error: function() {
			  	done = 0;
				return false;
			  },
			  success: function(){
			  	//d
			  }
			});
			
			if(done == 0) {
				//$("#login_login").hide();
				//$("#login_form").show();
				//return false;
			} else {
				//return true;
			}
			
			//$.post("/socialagent_central/trunk/public/auth/login", { customerEmail: user, customerPassword: pass },function(data) {
				//alert('Load was performed.'+data);
		//	});
			//return false;
			//return true;
		}
		else
		{
			return false;
		}
		
	});
	*/
	$("#login_btn").click(function(){
		$("#formula").submit();
		return false;
	});
}

sa.enterlogin = function()
{
    $("form input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('form').submit();
            return false;
        } else {
            return true;
        }
    });
}

sa.checkFrontMongoDB = function()
{
	if(sa.quiet == 0)
	{
		$("#status_box").html("Kör <strong>Frontend Test #1</strong>");
	}
	else
	{
		$(".system_check").html("Kör test #1");
	}
	
	$.get('login_verify.php', function(data) {
		if(data == "1") {
			sa.frontMongoDB = 1;
			sa.checkFrontMySQL();
		} else {
			sa.evaluateChecking();
		}
	});
	
	$(".tabs ul li").click(function(){
		$(".tabs ul li").toggleClass("active");
		return false;
	});
}

sa.checkFrontMySQL = function()
{
	if(sa.quiet == 0)
	{
		$("#status_box").html("Kör <strong>Frontend Test #2</strong>");
	}
	else
	{
		$(".system_check").html("Kör test #2");
	}
	
	$.get('login_verify.php', function(data) {
		if(data == "1") {
			sa.frontMySQL = 1;
			sa.checkBackMongoDB()
		} else {
			sa.evaluateChecking();
		}
	});
}

sa.checkBackMongoDB = function()
{	
	if(sa.quiet == 0)
	{
		$("#status_box").html("Kör <strong>Backend Test #1</strong>");
	}
	else
	{
		$(".system_check").html("Kör test #3");
	}
	
	$.get('login_verify.php', function(data) {
		if(data == "1") {
			sa.backMongoDB = 1;
			sa.evaluateChecking();
		} else {
			sa.evaluateChecking();
		}
	});
}

sa.evaluateChecking = function()
{
	$("#login_loading").hide();
	if(sa.frontMongoDB == 1 && sa.frontMySQL == 1 && sa.backMongoDB == 1)
	{
		$("#login_form").fadeIn();
		$(".system_check").addClass("system_active");
		$(".system_check").html("Aktivt");
	}
	else
	{
		if(sa.quiet == 0)
		{
			$("#login_error").fadeIn();
		}
		
		$(".system_check").addClass("system_down");
		$(".system_check").html("Nere");
	}
	
	$(".system_check").removeClass("system_check");
}

window.onload = sa.init;
