function savecomment(p_form_name, p_obj_suffix)
{
    var text_obj = 'comment_text' + p_obj_suffix;
        
    if ($('#' + text_obj).length > 0 && CKEDITOR.instances[text_obj])
    {
        $('#' + text_obj).val(CKEDITOR.instances[text_obj].getData());
    }
	
    $(p_form_name).ajaxSubmit(function(json) { 
        
        if (json.error !== undefined)
        {
			alert(json.error);
        }
        else
        {
            if (json.edit)
            {
                $('#commentform' + json.id).html(json.content);
            }
            else
            {							
                var parent      = $('#commentnodes' + json.parent_id);
                var last        = parent.children().last();
                
                if (last && (last.attr('id') == 'comment0' || last.attr('id') == 'nocomments'))
                {
                    last.remove();
                }

				var comment = $('<div></div>').attr('id', 'comment' + json.id).append(json.content);
				parent.append(comment);
				
				if (json.level > 0 && json.level < 7)
				{
					$('#comment' + json.id).addClass('level1');
				}				
                
                if ($('#' + text_obj) && CKEDITOR.instances[text_obj])
                {
                    $('#' + text_obj).val('');
                    CKEDITOR.instances[text_obj].setData('');
                }                
            }                            
        }         
    }); 
    
}

function editcomment(p_comment_id)
{
    get_comment_form('edit', p_comment_id);
}

function viewcomment(p_comment_id)
{
    get_comment_form('view', p_comment_id);
}

function deletecomment(p_comment_id)
{

	$.ajax({
		url: '/comment/deletecomment/',
		data : {
			comment_id 	: p_comment_id
		},
		success: function(json){
            if (json.error !== undefined)
            {
                alert(json.error);
            }
            else
            {
                $('#commentform' + json.id).html(json.content);                                
            }
		}
	}); 	
	 
}

function get_comment_form(p_type, p_comment_id)
{    
	$.ajax({
		url: '/comment/getcommentform/',
		data : {
			comment_id 	: p_comment_id,
			type		: p_type
		},
		success: function(json){
            if (json.error !== undefined)
            {
                alert(json.error);
            }
            else
            {
                $('#commentform' + json.id).html(json.content);
                
                if (p_type == 'edit')
                {
                    create_ckeditor_instance('comment_text' + json.parent_id + json.id);
                }                                
            }
		}
	});    
    
}

function answercomment(p_parent_id)
{
	$.ajax({
		url: '/comment/getcommentform/',
		data : {
			parent_id 	: p_parent_id,
			type		: 'edit'
		},
		success: function(json){
            var parent  = $('#commentnodes' + json.parent_id);
            var last    = parent.children().last();
            if (last == null || last.attr('id') != 'comment0')
            {
				
				var comment = $('<div></div>').attr('id', 'comment' + json.id).append(json.content);
				
				parent.append(comment);
				
				if (json.level > 0 && json.level < 7)
				{
					$('#comment' + json.id).addClass('level1');
				}				
				
                create_ckeditor_instance('comment_text' + json.parent_id + json.id);
            }
		}
	});  	
	
}

function cancelanswer(p_parent_id)
{
    var parent  = $('#commentnodes' + p_parent_id);
    //var last    = parent.getLast();
    var last    = parent.children().last();
    if (last && last.attr('id') == 'comment0')
    {
        last.remove();
    }
}

function create_ckeditor_instance(obj_name)
{
    if ($('#' + obj_name).length > 0)
    {
        var instance =  CKEDITOR.instances[obj_name];
        if (instance)   CKEDITOR.remove(instance); 
        
        CKEDITOR.replace(obj_name, { height : '100px', width : '100%', language : 'ru', customConfig : '/classes/services/ckeditor/cfg_text.js'});          
    }
}

//jquery
function load_datepicker()
{
	 $.datepicker.setDefaults(
			 $.extend($.datepicker.regional["ru"])
	 );

	 $("#date").datepicker({
		 showOn: 			'button',
		 buttonImage:		'/img/icons/calendar.gif',
		 buttonImageOnly:	true
	 });	
												
}

$(function(){
	create_ckeditor_instance('comment_text');
	load_datepicker();
})


