$(function(){			
	$("#loginBtn").click(function(){
		toggleLoginBox();
		toggleSelectedButton();
		setFocus();
	})
	.one("click", function(){
		$("#email").blur();
	});
	
	$("#switch-to-gapps-login").click(function(){
		//step 2 needs to be opened
		toggleShowStep1(true);
		toggleShowStep2(false);
		toggleShowStep3(true);
		toggleShowStep4(true);
		setFocus();	
		return false;
	});
	$("#switch-to-gapps-login2").click(function(){
		$("#switch-to-gapps-login").click();
	});
	$("#switch-to-standard-login").click(function(){
		//step 1 needs to be opened
		toggleShowStep1(false);
		toggleShowStep2(true);
		toggleShowStep3(true);
		toggleShowStep4(true);
		setFocus();
		return false;
	}); /*
	$("#forgot-password").click(function(){	
		//step 4 needs to be opened
		toggleShowStep1(true);
		toggleShowStep2(true);
		toggleShowStep3(true);
		toggleShowStep4(false);
		setFocus();
	});
	*/
	
	var checkEmailRe = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	$("#email")
	.blur(function(){
		var val = this.value;
		if (val && checkEmailRe.test(val))
		{
			$.getScript("https://app.theflowr.com/api/find/email/" + encodeURIComponent(val) + "/callback/email_cb");
		}
	});
	
	$("#login2_username")
	.blur(function(){

	});
	
	$("#loginBox-step-1 form").submit(function(){
		var clients = flowr_clients,
			len     = clients.length,
			ul, c;
		
		if (len < 1)
		{
			alert("Sorry, we could not find your email.");
		}
		else if (len > 1)
		{
			ul = $("#loginBox-step-3 ul");
			ul.empty();
			for (var i=0; i<len; i++)
			{
				c = clients[i];
				$("<li>")
					.data("client", c)
					.append($('<a href="#"/>').text(c.name))
					.appendTo(ul);
			}
			toggleShowStep1(true);
			toggleShowStep3();
		}
		return len == 1;
	});
	
	$("#loginBox-step-3").delegate("li", "click", function(){
		var client = $(this).data("client");
		setLoginAction(client);
		flowr_clients = [client];
		$("#loginBox-step-1 form").submit();
		return false;
	});
});

flowr_clients = [];
function email_cb(data)
{
	if (data.rs == 1)
	{
		flowr_clients = data.data && data.data.clients || [];
		if (flowr_clients.length == 1)
		{
			setLoginAction(flowr_clients[0]);
		}
	}
}

function setLoginAction(client)
{
	$("#loginBox-step-1 form").attr("action", client.url + "/login");
}

function valideteGForm(){
	
		var val = $("#login2_username").val();
		var checkEmailRe = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var checkDomainRe = /^(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				
		if (val && checkEmailRe.test(val)) {
			$('#login2_username').removeClass("error");
			var pos = strpos(val, "@");
			var domain = val.substr(pos + 1);
			setGoogleLoginAction(domain);
		}
		else if (val && checkDomainRe.test(val)) {
			$('#login2_username').removeClass("error");
			setGoogleLoginAction(val);
		} else {
			$('#login2_username').addClass("error");		
			alert("This is not valid email or domain name.");	
		}	
	
	if($("#login2_username").hasClass("error")){
		return false;
	}
}

function setGoogleLoginAction(domain)
{
	$("#loginBox-step-2 form").attr("action", "http://app.theflowr.com/api/auth/from/google/domain/" + domain);	

}

function toggleSelectedButton(){
	if($("#loginBtn").hasClass("loginBtn_selected")){
		$("#loginBtn").removeClass("loginBtn_selected");
	} else {
		$("#loginBtn").addClass("loginBtn_selected");
	}
}

function toggleLoginBox(){
	if($("#loginBox").is(":visible")){
		$("#loginBox").hide();
	} else {
		$("#loginBox").show();
	}		
}

function toggleShowStep1(close){
	if(close || $("#loginBox-step-1").is(":visible")){
		$("#loginBox-step-1").hide();
	} else {
		$("#loginBox-step-1").show();
		//$("#email").focus();
	}
}

function toggleShowStep2(close){
	if(close || $("#loginBox-step-2").is(":visible")){
		$("#loginBox-step-2").hide();
	} else {
		$("#loginBox-step-2").show();
		//$("#login2_username").focus();
	}
}

function toggleShowStep3(close){
	if(close || $("#loginBox-step-3").is(":visible")){
		$("#loginBox-step-3").hide();
	} else {
		$("#loginBox-step-3").show();
	}
}

function toggleShowStep4(close){
	if(close || $("#loginBox-step-4").is(":visible")){
		$("#loginBox-step-4").hide();
	} else {
		$("#loginBox-step-4").show();
		//$("#login4_username").focus();
	}
}

function setFocus(){
	$("#loginBox > div:visible input:first").focus();
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}


