$(document).ready(function(){
    $('#search').focus(function() {
        if (this.value == this.defaultValue) this.value = '';
        else this.select();
    });
    $('.blr').blur(function() {
        if ( this.value == '' )this.value = this.defaultValue;
    });
    $('.blr').focus(function() {
        if (this.value == this.defaultValue) this.value = '';
        else this.select();
    });

	$('#add_comment').click(function() {
		if ( $('input[name="captcha"]').val() == '' || $('input[name="captcha"]').val() == 'Код с картинки' ) { 
			alert('Введите код с картинки');
			return false;
		}
		else if ( $('input[name="uname"]').val() == '' || $('input[name="uname"]').val() == 'Ваше имя' ) { 
			alert('Введите ваше имя');
			return false;
		}
		else if ( $('textarea[name="text"]').val() == '' ) { 
			alert('Введите комментарий');
			return false;
		}
	});
	$('#add_quote').click(function() {
		if ( $('input[name="captcha"]').val() == '' || $('input[name="captcha"]').val() == 'Код с картинки' ) { 
			alert('Введите код с картинки');
			return false;
		}
		else if ( $('textarea[name="text"]').val() == '' ) { 
			alert('Введите цитату');
			return false;
		}
	});

	// Ставим плюсик
    $(".vote-for").click(function () {
		id = $(this).attr('rev');
		rating = $(this).next();
		if ( $.cookie('first') == '' ) $.cookie('first', 'one', {expires: 365*2, path: "/"});
		//if ( $.cookie('first') != '' ) {
			if ( $.cookie('for' + id) == '' ) {
				$.cookie('for' + id, '1', {expires: 365*2, path: "/"});
				$.ajax({
					type: 'POST',
					data: 'hidden=send&rating=1&id=' + id,
					url: BASE_URL + 'ajax/vote/',
					beforeSend: function(){},
					success: function(answer) {rating.text(parseInt(rating.text())+1)},
					error: function() {alert('При соединении с сервером возникли проблемы');}
				});
			} else alert('Вы уже голосовали за эту цитату');
		//}
		return false;
	});

	// Нещадно минусуем
    $(".vote-against").click(function () {
		id = $(this).attr('rev');
		rating = $(this).prev();
		if ( $.cookie('first') == '' ) $.cookie('first', 'one', {expires: 365*2, path: "/"});
		//if ( $.cookie('first') != '' ) {
			if ( $.cookie('for' + id) == '' ) {
				$.cookie('for' + id, '1', {expires: 365*2, path: "/"});
				$.ajax({
					type: 'POST',
					data: 'hidden=send&rating=-1&id=' + id,
					url: BASE_URL + 'ajax/vote/',
					beforeSend: function(){},
					success: function(answer) {rating.text(parseInt(rating.text())-1)},
					error: function() {alert('При соединении с сервером возникли проблемы');}
				});
			} else alert('Вы уже голосовали за эту цитату');
		//}
		return false;
	});

    /* $(".scroller").click(function () {
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;
		$("html").animate({ scrollTop: destination}, 1100 );
		return false;
	}); */

    // Удаление цитаты
    $('.delquote').click(function(){
        thiss = $(this);
        var id = thiss.attr('href');
        $.ajax({
            type: 'POST',
            data: 'hidden=send&id=' + id,
            url: BASE_URL + 'ajax/delquote/',
            beforeSend: function(){},
            success: function(answer) {$('.delq' + id).animate({backgroundColor: 'red', color: 'black'}, 'fast').animate({ opacity: "hide" }, "slow"); thiss.parent().parent().parent().parent().parent().animate({backgroundColor: 'red', color: 'black'}, 'fast').animate({ opacity: "hide" }, "slow")},
            error: function() {alert('При удалении цитаты появилась проблема');}
        });
        return false; 
    });

});
