$(document).ready(function() {
    $('#reg_link > a').click(
        function() {
            toggler.exec('form_registration');
            return false;
        }
    );
    
    // авторизация
    function beforeSubmit() {
			$('.error').hide();
            $('#enter_btn').hide();
            $('#login_loading').show();
		}
		
    function errorSubmit(data) {
        $("#error").html(data.responseText).fadeIn();
        $('#enter_btn').show();
        $('#login_loading').hide();
    }
    
    function successSubmit(data) {
        $('#enter_btn').show();
        $('#login_loading').hide();
        if (data.status=='success') {
            document.location.href = data.href;
        } else {
            for (i=0; i<data.errors.length; i++) {
                $('#error_'+data.errors[i]).fadeIn();
            }
        }
    }
    
    $("#login_form").submit(function() {
        $(this).prepend('<input type="hidden" name="ajax_flag" value="1" />');
        
        var options = {
            beforeSubmit:	beforeSubmit,
            success:		successSubmit,
            error:			errorSubmit,
            timeout:		3000,
            dataType:		'json'
        }		

        $(this).ajaxSubmit(options);
        return false;
    });
    
    $('#openid_form').submit(function() {
        var v = $('input[name=openid_account]').val();
        if (!v) {
            $('#openid_account_error').show();
            return false;
        }
    });
    
    $('#lj_form').submit(function() {
        var v = $('input[name=lj_account]').val();
        if (!v) {
            $('#lj_account_error').show();
            return false;
        }
    });
});

toggler = {
    prev: '',
    expect: 'form_registration',
    
    exec: function(id) {
        var obj = $('#'+id);
        if (toggler.prev && toggler.prev != toggler.expect) {
            $('#'+toggler.prev).animate({height: "hide"});
        }
        if (obj.css('display').toLowerCase() == 'none') {
            obj.animate({height: "show"});
        } else {
            obj.animate({height: "hide"});
        }
        toggler.prev = id;
        return false;
    }
}
    
report_vote = function(obj, mode, interesting) {
    var $obj = $(obj);
    var link = $obj.attr('href');
    $.getJSON(link, function(data) {
        if (data.can_go) {
            var text = 'Мне интересно';
        } else {
            var text = 'Я передумал';
        }
        if (mode == 'inner') {
            $obj.html(text).parent().prev().find('span').html(data.count);
        } else {
			if (interesting && interesting !== undefined) {
				var parent = $obj.parent();
				parent.prev().prev().remove();
				parent.prev().remove();
				parent.next().remove();
				parent.remove();
			} else {
				$obj.html(text).next().find('span').html(data.count);
			}
        }
    });
    return false;
}
