$(document).ready(function() {
	////////////
	// Welcome
	////////////
	
	// Login
	$('#login_form').submit(function(e) {
		e.preventDefault();
		
		$('#message_box').attr('class', 'no_message rctr rcbl');
		$('#message_box p').html('One second.. <img src="/images/ajax-loader-gray.gif"');
		
		if ($('#username').attr('value').length < 3) {
			$('#message_box').attr('class', 'bad_message rctr rcbl');
			$('#message_box p').html('Username too short.');
			$('#username').focus().select();
			return false;
		}
		
		if ($('#password').attr('value').length < 3) {
			$('#message_box').attr('class', 'bad_message rctr rcbl');
			$('#message_box p').html('Password too short.');
			$('#password').focus().select();
			return false;
		}
		
		if ($('#username').attr('value').length > 30) {
			$('#message_box').attr('class', 'bad_message rctr rcbl');
			$('#message_box p').html('Username too long.');
			$('#username').focus().select();
			return false;
		}
		
		if ($('#password').attr('value').length > 30) {
			$('#message_box').attr('class', 'bad_message rctr rcbl');
			$('#message_box p').html('Password too long.');
			$('#password').focus().select();
			return false;
		}
		
		$.post('/ajax/login/',{
		 username: $('#username').attr('value'), password: $('#password').attr('value')},
		 function(data){
		    if (data.success) {
				location.href = "/notes/";
			} else {
				$('#message_box').attr('class', data.message_class + " rctr rcbl");
				$('#message_box p').html(data.message);
				
				if (data.register) {
					$('#password').attr('value', '');
				};
				
				$('#password').focus().select();
			};
		}, 'json');
		
	});
	
	
	///////////
	// Notes
	///////////
	
	var new_color = "#888";
	
	// Sets gray color, when unset.
	$('.title_input').each(function() {
		if(this.value == "Enter title here..") {
			this.style.color = new_color;
		}
	});
	
	$('.text_input').each(function() {
		if(this.value == "Enter bodytext here..") {
			this.style.color = new_color;
		}
	});
	
	// Clear title input
	$('.title_input').focus(function() {
		if (this.value == "Enter title here..") {
			this.value = "";
		}
		this.style.color = "";
	});
	$('.title_input').blur(function() {
		if (this.value == "") {
			this.value = "Enter title here..";
			this.style.color = new_color;
		}
	});
	
	// Clear text input
	$('.text_input').focus(function() {
		if (this.value == "Enter bodytext here..") {
			this.value = "";
		}
		this.style.color = "";
	});
	$('.text_input').blur(function() {
		if (this.value == "") {
			this.value = "Enter bodytext here..";
			this.style.color = new_color;
		}
	});
	
	// Set and save color.
	$('.note_title select').change(function(e) {
		e.preventDefault;
		var original_this = $(this);
		var id = $(this).parent().parent().children('.hidden_id')[0].value;
		var spinner = $(this).parent().children('.ajax_spinner')[0];
		
		// Show spinner
		spinner.style.display = "block";
		
		// Go to work.
		$.post('/ajax/change_color_note/',{
		 color: this.value, note_id: id},
		 function(data){
		    if (data.success) {
				// If color was changed, change it.
				original_this.parent().css('background-color', '#' + data.color);
			} else {
				location.href = '/' + data.redirect + '/';
			};
			
		// Hide spinner
		spinner.style.display = "none";
		}, 'json');
	});
});
