function ShowParentComment ( id, obj )
{
    $( 'div[@class="comment_item answer_absolute"]' ).remove ();
    $( 'div[@class="close_p close_answ"]' ).remove ();
    
    var offset = $( obj ).offset ();
    
    var html = $( '#comment' + id ).html ();
        html = $( '<div class="comment_item answer_absolute" style="display: none; position: absolute; width: 500px; background-color: #fff; border: 3px #000 solid; padding: 7px 10px;">' + html + '</div>' );
        html
            .find ( 'div.comment_answer_link_c' )
            .remove ()
            .end ()
            .find ( 'img.this_is_answer' )
            .remove ();
            
    var top  = offset.top  - 30;
    var left = offset.left + 30;
        
    html.css ( { top: top + 'px', left: left + 'px' } );
    
    var close = $( '<div class="close_p close_answ"></div>' );
        close.css ( { top: ( top - 12 ) + 'px', left: ( left + 510 ) + 'px' } );
        close.bind ( 'click', function () {
            $( this ).remove ();
            $( 'div[@class="comment_item answer_absolute"]' ).remove ();
        } );
        
    $( 'body' ).append ( html );
    html.animate ( { opacity: 'show' }, 200 );
    $( 'body' ).append ( close );
}

function UDeleteComment ( id, type )
{
    if ( type == 'del_by_author' )
    {
        var del_by = 'author';
    } else {
        var del_by = 'rr';
    }
    
    $.get ( '/uaction/', { action: 'delete_comment', id: id, del_by: del_by }, function () {
        if ( type == 'del_by_author' )
        {
            var str = 'Комментарий удален автором этой темы';
        } else {
            var str = 'Комментарий удален представителем заведения';
        }
        
        
        $( '#comment' + id + ' div.comment_text div:eq(0)' ).html ( '<span class="deleted_comment">' + str + '</span>' );
    } );
}

function Complaint ( id )
{
    var html = $( '#complaint_form' ).html ();
    var nick = $( '#comment' + id ).find ( 'span.cunick a:eq(0)' ).text ();
    if ( !nick ) nick = $( '#comment' + id ).find ( 'span.cunick' ).text ();
    
    html = html.replace ( '[NICK]', nick );
    html = html.replace ( '[ID]', id );
    jQuery.facebox ( html );
}

function SendComplaint ( id )
{
    $.post ( '/uaction/', { action: 'complaint', id: id, text: $( '#facebox' ).find ( 'textarea' ).val (), path: window.location.pathname + window.location.search }, function () {
        jQuery.facebox ( 'Ваша жалоба отправлена.' );
    } );
}

function GetCommentAnswerForm ( id )
{
    var html = $( 'div.comment_form_etalon' ).html ();
        html = html.replace ( /CID/g, id );
    
    $( '#comment_answer_c_' + id )
        .find ( 'div.nav' )
        .hide ()
        .end ()
        .append ( html );
        
    var w = $( '#comment_answer_c_' + id )
                .find ( 'textarea' )
                .width ();
}

function SaveComment ( id )
{
	if ( id )
	{
		var fid = '_' + id;
	} else {
		var fid = '';
	}

	$( '#comment_bsubmit' + fid )
        .hide()
        .after ( '<img src="/images/ajax.gif" width="43" height="11" id="comment_asubmit' + fid + '" />' );
	
	var req = {
		url: window.location.href
	};

	client_request ( 'comments', 'save_mcomment', '', 'POST', req, 'AfterSaveComment', "'" + fid + "'", 'edit_comment_form' + fid );
}

function AfterSaveComment ( fid, data )
{
	if ( data.errors )
	{
	    $( '#comment_asubmit' + fid ).remove();
        $( '#comment_bsubmit' + fid ).show();
	    
		$( '#save_comment_error' + fid ).html ( '<div class="errors" style="margin-top: 15px;">' + data.errors + '</div>' );
	} else if ( data.content ) {
	    $( '#comment_asubmit' + fid ).remove();
        $( '#comment_bsubmit' + fid ).show();
        
	    if ( fid != '' )
	    {
	        $( '#edit_comment_form' + fid ).remove ();
    	    $( '#save_comment_error' + fid ).remove ();
    	    $( '#comment_answer_c' + fid ).find ( 'div.nav' ).show ();
	        
	        var id = fid.replace ( '_', '' );
	        
    	    if ( $( '#comments_children' + fid ).length == 0 )
    	    {
    	        $( '#comment' + id ).after ( '<div class="comments_children" id="comments_children_' + id + '"></div>' );
    	    }
    	    
    	    $( '#comments_children' + fid ).append ( data.content );
	    } else {
	        $( '#edit_comment_form' + fid ).find ( 'textarea' ).val ( '' );
    	    $( '#save_comment_error' + fid ).empty ();
    	    $( '#comments_container' ).append ( data.content );
	    }
	} else {
		window.location = '/slogin/?type=comment';
	}
}