(function($){
    $.fn.comments = function(params){

		var s = $.extend({
			ajax_file: '/BackEnd/site/comments/bootstrap.php',
            ajax_update:  false,
            send_but_class: 'send_comment',
            reply_but_class: 'reply_comment',
            mform_id_pref:'comments_form_',
            del_but_id:'delete',
            resp_but_id:'response'
		},
		params);

        var conteiner = $(this);

        function cObj(wrap){
            this.cwrap = wrap;
        }

        cObj.prototype.insert = function(data){
            var item = $('<div><a href="javascript:void(0)"><img src="'+data.avatar+'" alt="" /></a><a href="javascript:void(0)"><span class="name">'+data.name+'</span></a><div class="comments_info"><div>'+data.date+'</div></div><p>'+data.message+'</p></div><div class="clear"></div><div class="dividing_line"></div><div class="clear"></div>');
            this.cwrap.prepend(item);
        }

        cObj.prototype.insert_reply = function(el, data){
            var item = $('<div style="width:480px; float:right;"><a href="javascript:void(0)"><img src="'+data.avatar+'" alt="" /></a><a href="javascript:void(0)"><span class="name">'+data.name+'</span></a><div class="comments_info"><div>'+data.date+'</div></div><p>'+data.message+'</p></div><div class="clear"></div><div class="dividing_line"></div><div class="clear"></div>');
            item.insertBefore(el);
            el.parent().find('.to_remove').remove();
        }


        //
		//define FormData (object to collecting input data from form)
		//
		function FormData(wrapper){
			this.conteiner = wrapper;
		};

		FormData.prototype.get = function(){
			var post_data = new Object;
			this.conteiner.find(":input").each(function(){
				if($(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio'){
					if($(this).attr('checked')){
						post_data[$(this).attr('name')] = $(this).val();
					};
				}else{
					post_data[$(this).attr('name')] = $(this).val();
				};
			});
			return post_data;
		};

        //
        // bus logic
        //
        var comments = new cObj(conteiner);

        var reply_form;
        $('.reply_news').toggle(
           function(){
           reply_form = $('<div class="to_remove"><textarea name="reply_text" class="reply_message" cols="" rows="">Ваш комментарий:</textarea><input name="reply_mid" type="hidden" value="'+$(this).attr('id')+'"/><input name="page_id" type="hidden" value="'+$(this).attr('par')+'"/><a href="javascript:void(0)" class="save_link reply_comment" id="'+$(this).attr('id')+'"><span class="l"><span class="r"><span class="c">Ответить</span></span></span></a></div><div class="clear"></div><div class="dividing_line to_remove"></div>');
           $(this).parent().append(reply_form);

           $('.'+ s.reply_but_class).click(function(){
               var el = $(this).parent();
               var post_data = new FormData($(this).parent()).get();
               $.post(s.ajax_file, post_data, function(response){
                    comments.insert_reply(el, response);
                    $(this).parent().find('.to_remove').remove();
               }, 'json');
           });

           },
           function(){
             $(this).parent().find('.to_remove').remove();
           }
        );

        $('.'+ s.send_but_class).click(function(){
            var post_data = new FormData($('#'+s.mform_id_pref+$('.'+ s.send_but_class).attr('id'))).get();
            $.post(s.ajax_file, post_data, function(response){
                comments.insert(response);
            }, 'json');
        });

    }

    $(document).ready(function(){
		$("#comments").comments();
	});

}(jQuery))
