function switchAdd(obj) {
	if ($('#addForm').css('display') == 'block') {
		$(obj).text('Добавить');
	} else {
		$(obj).text('Отменить');
	}
	$('#addForm').slideToggle();
	return false;
}

function textChange()
{
	var a = $('#new_note').val().length;
	if (a > 75) $('#new_note').val($('#new_note').val().substring(0, 75));
	var a = $('#new_note').val().length;
	$('#limit').text(75 - a);
}


function vote(grade, obj, note_id) {
	
	if (!registered) {
		alert('Авторизуйтесь пожалуйста');
		return false;
	}
	
	$('.ctrl').attr('disabled', 'disabled');
	
	if (!note_id || note_id.length == 0) var note_id = $('#note_id').val();

	if (note_id.length > 0)
		$.get('/ajax/vote_for_note/', {'id':note_id, 'grade':grade}, function(resp){ 
			if (resp == 'added') loadNote();
			else if (resp == 'updated') {
				if (grade == 1)
				{
					$('#' + note_id + 'plus').attr('src', '/images2/wantbuy_active.gif');
					$('#' + note_id + 'minus').attr('src', '/images2/dontwantbuy.gif');
				}
				else
				{
					$('#' + note_id + 'plus').attr('src', '/images2/wantbuy.gif');
					$('#' + note_id + 'minus').attr('src', '/images2/dontwantbuy_active.gif');
				}
			}
			else 
				alert(resp);
			$('.ctrl').removeAttr('disabled');
		});
}

function addNote() {
	var note = $('#new_note').val();
	if (note.length > 0 && note.length <= 75) {
		$('.ctrl').attr('disabled', 'disabled');
	
		$.post('/ajax/note_add', {'note' : note}, function(resp) {
			alert(resp);
			$('#new_note').val('');
		});
	
		$('.ctrl').removeAttr('disabled');
		
	} else if (note.length > 75) {
		alert('Максимально можно использовать 75 символов');
	} else {
		alert('Нельзя отправить пустое сообщение');
	}
	
	return false;
}

function loadNote() {
	$('#loader').show();
	$('#note').hide();
	$.getJSON('/ajax/note_view', function(data) {
		$('#loader').hide();
		if (data.text.length > 0)
		{
			$('#note_author').text(data.user_login).attr('href', 'http://www.maryjane.ru/profile/' + data.user_id);
			$('#notees_link').attr('href', '/notees/' + data.user_id + '/' + data.id);
			$('#note_text').text(data.text);
			$('#note_id').val(data.id);
			$('#note').show();
		}
		else
		{
			$('#note_message').text('Нет новых слоганов').show();
			$('#note_id').val('');
		}
	});
}