 $(document).ready(function(){
	var listname_field = $("#iListTitle");
	listname_field.after('<div id="iListURLContainer">Adresa va fi: ' +
			'<span id="iListURL"></span></div>' +
			'<div id="iListURLErrorContainer" class="error-message">' +
			'<span id="iListURLError"></span></div>'); 

	// these are duplicates of the errors that we are going to detect too
	$('.error-message.name').not('.serverside').hide();
	
	listname_field.keyup(update_list_status).keyup();
	
	$('#owner_name').hide();
  });
  
var first_update = true;
function update_list_status(){
	var value = this.value;
	
	// the first time - we leave the updates from the server to be visible
	// all other times -> hide all and start from 0
	if (!first_update) {
		$('.error-message.name').hide();
	}
	
	if (name_validation_serverside_error()) {
		// there is an error from the server
   		$('#iListURLContainer').hide();
	}else {
		$('#iListURLContainer').show();
		var new_value = value.replace(/\s+/g, '-');
	   	$("#iListURL").text("http://lista.lu/" + $('#owner_name').text() + '/de/' + new_value);
	}

	if (first_update) {
		first_update = false;
	}
}

function name_validation_serverside_error() {
 	return (first_update && $('.error-message.name.serverside').length>0);
}

$(document).ready(function(){
	$('form.new_list').submit(function(){
		var textarea = $('.list_input', $(this)); 
		var text = textarea.val();
		var nr_enter = text.split("\n").length - 1;
		var nr_double_enter = text.split("\n\n").length - 1;
		if (nr_enter > 0 && nr_double_enter == 0) {
			var change = confirm('Ai scris mai multe linii dar nu sunt separate cu cate un rand liber, ' +
					'ca sa fie elemente diferite in lista.\nVrei sa le separi?');
			if (change) {
				textarea.val(text.replace(/\n/g, '\n\n'));
			}
		}
		return true;
	});
	
	$('a[href=#help_toggle]').click(hide_help);		
});


function hide_help() {
	$('div.edit_help.mini').fadeOut('normal',help_hidden);
}

function help_hidden() {
	$('.list_edit .input').width('98%');
	$('#iListContents').width('98%');
	$('a[href=#help_toggle]').html('arata').unbind('click',hide_help).click(show_help);
}

function show_help() {
	$('.list_edit .input').width('320px');
	$('#iListContents').width('320px');
	$('div.edit_help.mini').fadeIn('normal',help_shown);
} 

function help_shown() {
	$('a[href=#help_toggle]').html('ascunde').unbind('click',show_help).click(hide_help);	
}

