function comment_reply_to(comment_id, object_id)
{
    var comment_form = $('#comment_form_'+object_id);
    if(!comment_form.length) return;
    comment_form.get(0).reply_to.value = comment_id;
    $('#comment'+comment_id+' .control').after(comment_form);
    setTimeout('$("#comment_form_'+object_id+' .comment_field").focus()', 5);
}
function comments_subscribe(content_type_id, object_id)
{
    var add_link = $('#comments_subscribe_link_'+content_type_id+'_'+object_id);
    var delete_link = $('#comments_unsubscribe_link_'+content_type_id+'_'+object_id);
    add_link.css('opacity', '0.5');
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/ajax/comments/subscribe/',
        data: {content_type_id:content_type_id, object_id:object_id},
        success: function(result){
            add_link.css('opacity', '1');
            if(result.error){}
            else
            {
                add_link.hide();
                delete_link.show();
            }
        },
        error: function(){
            add_link.css('opacity', '1');
        }
    });
}
function comments_unsubscribe(content_type_id, object_id)
{
    var add_link = $('#comments_subscribe_link_'+content_type_id+'_'+object_id);
    var delete_link = $('#comments_unsubscribe_link_'+content_type_id+'_'+object_id);
    delete_link.css('opacity', '0.5');
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/ajax/comments/unsubscribe/',
        data: {content_type_id:content_type_id, object_id:object_id},
        success: function(result){
            delete_link.css('opacity', '1');
            if(result.error){}
            else
            {
                add_link.show();
                delete_link.hide();
            }
        },
        error: function(){
            delete_link.css('opacity', '1');
        }
    });
}

function comment_field_onfocus()
{
    $('.comment_field').focus(function(){
        $(this).css('width','100%').css('height','15em');
    });
}

function comment_preview(object_id)
{
    var form_id = '#comment_form_'+object_id;
    $(form_id+' .comment_preview').html('<div class="wait">'+lang.please_wait+'</div>').show();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/ajax/comments/preview/',
        data: {text: $(form_id+' .comment_field').val()},
        success: function(result){
            $($(form_id+' .comment_field').get(0)).focus();
            if(result.error){
                $(form_id+' .comment_preview').html('<div class="error">' + result.error + '</div>');
                return;
            }
            $(form_id+' .comment_preview').html(result.preview).show();
        },
        error: function(){
        }
    });
    return false;
}

function comment_post(object_id)
{
    var form_id = '#comment_form_'+object_id;
    $(form_id+' .comment_preview').html('<div class="wait">'+lang.please_wait+'</div>').show();
    data = $(form_id).formToArray();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/ajax/comments/post/',
        data: data,
        success: function(result){
            $($(form_id+' .comment_field').get(0)).focus();
            if(result.error){
                $(form_id+' .comment_preview').html('<div class="error">' + result.error + '</div>' +
                    (result.comment?result.comment:'')).show();
                return false;
            }
            var comment_list = $('#comment_list_' + object_id);
            if(!comment_list.length)
            {
                $(form_id).before('<ul class="comment_list" id="comment_list_' + 
                    object_id + '">' + result.comment + '</ul>');
            }
            else
            {
                if(result.reply_to)
                    $('#comment'+result.reply_to).after(result.comment);
                else
                    comment_list.append(result.comment);
                comment_list.after($(form_id));
            }
            $('.comment_preview').hide();
            $('.comment_field').blur().val('');
            $(form_id).get(0).reply_to.value='';
        },
        error: function(){
        }
    });
    return false;
}

$(document).ready(function(){
    comment_field_onfocus();
    $('#id_reply_to').val('');
});

