/*** username ***/

$(function () {
	
	// create the loader
	
	var loader = $('<img/>').attr('src', STATIC_BASE+'/img/ajax-loader.gif');
	$('label#username-wrapper').append(loader);
	loader.hide();
	
	// create the OK
	
	var ok = $('<img/>').attr('src', STATIC_BASE+'/img/ajax-input-OK.gif');
	$('label#username-wrapper').append(ok);
	ok.hide();
	
	// create the FAIL
	
	var fail = $('<img/>').attr('src', STATIC_BASE+'/img/ajax-input-FAIL.gif');
	$('label#username-wrapper').append(fail);
	fail.hide();

	// functions
	
	function run() {
		if ($('input#username').attr('value') == '') {
			ok.hide();
			fail.hide();
			loader.hide();
			return;
		}
		ok.hide();
		fail.hide();
		loader.show();
		var url = DYNAMIC_BASE+'/ajax/user/check-username';
		$.getJSON(url, { username: $('input#username').attr('value') }, function (d) {
			loader.hide();
			if (d == true) {
				ok.show();
			}
			else {
				fail.show();
			}			
		});
	}
	
	var timeout;
	$('input#username').keyup(function (e) {
		if (timeout) {
			clearTimeout(timeout);			
		}
		timeout = setTimeout(run, 500);
	}).blur(function (e) {
		if (timeout) {
			clearTimeout(timeout);			
		}
		timeout = setTimeout(run, 500);
	});
});

/*** submission ***/

$(function () {
	$('#register-submit').click(function (e) {
		stop(e);
		$(this).attr('disabled', 'disabled').attr('value', SENDING);
		$('form.register').submit();
	});
});