var domain = "jogacraque.com";

$(document).ready(function() {
	$("body").removeClass("invisible");
	
	gasajax("doc");
	
	var formFields = new Array();
	
	$("INPUT[type='text']").each( function () {
		id = $(this).attr('id');
		if (id != "j_password") {
			value = $(this).attr('value');
			formFields[id] = value;
		}
	});
	
	$('.landingpage INPUT').focus( function () {
		if ($(this).attr('value') == formFields[$(this).attr('id')]) {
			$(this).attr('value','');
		}
	});
	
	$('.landingpage INPUT').blur( function () {
		if ($(this).attr('value') == '') {
			$(this).attr('value',formFields[$(this).attr('id')])
		}
	});
	
	$("#j_password").focus(function(){
		if($(this).attr('type') != "password"){	
			$(this).replaceWith("<input type='password' id='j_password' name='j_password' class='input' />");
			$("#j_password").focus();
		}
	});
	
	$("#r_passwordlabel").focus(function() {
		$("#r_passwordlabel").remove();
		$("#r_password").removeClass("hidden");
		$("#r_password").focus();
	});

	$("#fbregistrationform").dialog({
		autoOpen: false,
		close:function() { $("#userRegistrationForm").removeClass("invisible"); },
		bgiframe: true,
		modal: true,
		title:"Cadastre-se com sua conta Facebook",
		width:370
	});

});

FB_userConnected = function() {
	$.ajax({
		cache:false,
		data:{"do":"something"},
		dataType:"text",
		success:function(response) {
			if (response == "true") {
				$("#doc").append("<form id='fbLogin' method='post' action='/fblogin.do' class='hidden' name='fb'></form>");
				$("#fbLogin").submit();
			} else {
				setRegForm("connected");
			}
		},
		type:"get",
		url:"/ajaxFacade/fbUserExists.do"
	});
}

setRegForm = function(status) {
	if (typeof status != "undefined") {
		if ( status == "connected") {

			$("#userRegistrationForm").addClass("invisible");
			$("#fbregistrationform").dialog("open");

			var api = FB.Facebook.apiClient;
			var uid = api.get_session().uid;
			var sequencer = new FB.BatchSequencer(); 
			var uinfo = api.users_getInfo(uid,["proxied_email"],sequencer);
			sequencer.execute(function() {
				var uemail = uinfo.result[0].proxied_email;
				$("#fbr_email").val(uemail);
				var upas = Math.floor(Math.random()*100000000+10000000);
				$("#fbr_password").val(upas);
			});
		}
	}
}

urlexample = function(value) {
	if (value != "") {
		$(".urlexamplee").text("http://"+value+"."+domain+"/");
	} else {
		$(".urlexamplee").text("");
	}
}

validateUserInfo = function(form,pre) {
	(typeof form != "undefined")?form = "#"+form:form = "#userRegistrationForm";
	if (typeof pre == "undefined") { pre = ""; }
	var doSubmit = true;
	$(form +" input").each(function() {
		if ( $(this).attr("value") == "" && $(this).attr('id') != pre+'inviteCode' && $(this).attr('id') != pre+'r_passwordlabel' ) {
			if ( $(this).attr('id') == pre+'r_password' ) {
				$("#"+pre+"r_passwordlabel").addClass("emptyField");
			}
			$(this).addClass("emptyField");
			doSubmit = false;
		} else {
			$(this).removeClass("emptyField");
		}
	});
	$(form +" select").each(function() {
		if ( $(this).attr("value") == " " ) {
			$(this).addClass("emptyField");
			doSubmit = false;
		} else {
			$(this).removeClass("emptyField");
		}
	});

	if ($("#"+pre+"screenName").attr("value").indexOf(".") > 0) {
		$("#"+pre+"screenName").addClass("emptyField");
	}
	if ($("#"+pre+"agreeTerm1").attr("checked") == false) {
		doSubmit = false;
		$(".terms").addClass("notchecked");
	} else {
		$(".terms").removeClass("notchecked");
	}

	return doSubmit;
}