// fonction de base 
//$(function () {
//  $(window).load(function () {
//    $(':input:visible:enabled:first').focus();
//  });
//})


$(document).ready(function() {

	$('input[type="text"],input[type="password"]').addClass("idleField");
		$('input[type="text"],input[type="password"]').focus(function() {
			$(this).removeClass("idleField").addClass("focusField");
		 if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"],input[type="password"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
		 if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});

	$.preloadCssImages();
});

function onMollifyStarted() {
	registerEventHandler(onMollifyEvent);
}

function onMollifyEvent(e) {
	if (e.type == 'SESSION_START') {
		var session = e.payload;
		if (session['authentication_required'] == true && session['authenticated'] == false) showLogin();
	} else if (e.type == 'SESSION_END') {
		showLogin();
	}
}

function showLogin() {
	$("#btn-login").click(onLogin);
	$("#login").show();
}

function onLogin() {
	var name = $("#name-field").val();
	var pw = $("#password-field").val();
	if (name.length == 0 || pw.length == 0) {
		// perhaps show some notification etc
		return;
	}
	doLogin(name, pw, refresh);
}

function refresh() {
	window.location = window.location;
}

function onLoginFailed() {
	alert("Login failed");
}

function onError(err) {
	// more like a debugging tool, this function can be removed
	//alert(err);
	$(".error").show();
	$(".lst-td input").css({
		"color": "red"
	});
}
