(function($) {
	$.fn.commentify = function() {
//		this.mousemove(this.commentify.mousemove);
		this.click(this.commentify.click);
		this.submit(this.commentify.submit);
		this.commentify.last = $();
	};
	$.extend($.fn.commentify, {
		last:	null,
		click: function(e) {
			var a = $(e.target).parents('a').first().add(e.target).filter('a');
			$.each(['create', 'complain', 'update', 'delete', 'cancel', 'edit', 'complained', 'togglecomplained', 'uncomplain', 'createFirstLevel', 'showNewCommentaries'], function(i, value) {
				if (!a.hasClass(value) || !$.secure.allow(e)) {
					return;
				}
				$.fn.commentify[value].call(a.parents('.comments-show-item').first(), e);
				e.preventDefault();e.stopPropagation();
				return false;
			});
		},
		mousemove: function(e) {
			c = $(this).commentify;
			n = $(e.target);
			if (n.get(0) == c.last.get(0)) {
				return;
			}
			c.last && c.last.children('.comments-actions-container').hide();
			c.last = n;
			c.last.children('.comments-actions-container').show();
		},
		showNewCommentaries: function(e) {
			$.scrollTo($($.find('.unviewed')).first(), 1000);
			$('#commentaries').addClass('comments-show-block-unviewed');
		},
		createFirstLevel: function(e) {
			form = $('#createFirstLevel');
			$.scrollTo(form, 1000);
			form.find('textarea').focus();
		},
		create: function(e) {
			this.commentify.cancel.call(this, e);
			var form = this.find('form[class=create_form]').first();
			form.toggle();
			form.is(':visible') && form.find('textarea').focus();
		},
		edit: function(e) {
			this.commentify.cancel.call(this, e);
			if (this.find('div[class~=complained]').size()) {
				$.fn.commentify.togglecomplained.call(this, e, $.fn.commentify.edit);
				return;
			}
			var form = this.find('form[class=edit_form]').first();
			form.find('textarea').first().html($.trim(this.find('.comments-show-item-text').first().html()));
			form.toggle();
			form.is(':visible') && form.find('textarea').focus();
		},
		'delete': function() {
			el = $(this);
			$.ajax({
			      url: $(this).find('a[class=delete]').first().attr('href'),
			      type: 'GET',
			      success: $.proxy($.fn.commentify.deleteSuccess, el),
			      error: $.fn.commentify.deleteError
			});
		},
		deleteSuccess: function(data) {
			el.effect("blind", {}, 1000).remove();
			span = $(".comments-show-head > span");
			span.html(data);
		},
		deleteError: function() {
			
		},
		cancel: function(e) {
			var form;
			if(e){
				if($(e.target).hasClass('create')){
					form = this.find('.edit_form');
				} else if($(e.target).hasClass('edit')){
					form = this.find('.create_form');
				} else {
					form = this.find('form'); 
				}
			} else {
				form = this.find('form');
			}
			
			form.hide();
			form.find('.error_list').remove();
			form.get(0).reset();
		},
		submit: function(e) {
			f = $(e.target).closest('form');
			$.ajax({
		      url: f.attr('action'),
		      type: 'POST',
		      data: f.serialize(),
		      success: f.attr('class') == 'edit_form' ? $.proxy($.fn.commentify.submitEditSuccess, f) : $.proxy($.fn.commentify.submitCreateSuccess, f),
		      error: $.fn.commentify.submitError,
		      beforeSend: function() {
				$(this).find('input, select, textarea, button').attr('disabled', 'disabled');
		      },
		      complete: function() {
				$(this).find('input, select, textarea, button').removeAttr('disabled');
		      }
		    });
			return false;
		},
		submitEditSuccess: function(data, status, request) {
			if (request.status==201) {
				var item = f.parent(); 
				item.children('.comments-show-item-hover').find('.comments-show-item-text').first().html(data);
				item.children('.comments-show-item-hover').first().effect("highlight", {}, 3000);
				item.commentify.cancel.call(item);
			} else {
				f.html(data);
			}
		},
		submitCreateSuccess: function(data, status, request) {
			if (request.status==201) {
				var item = f.parent();
				
				if(f.parent().is('#createFirstLevel'))item = f.parent().siblings('.comments-show-item-container').first().parent();
				item.commentify.append.call(item, data);
				
				if(!f.parent().is('#createFirstLevel')) { 
					item.commentify.cancel.call(item);
				} else {
					f.find('.error_list').remove();
					f.get(0).reset();
				}
			} else {
				f.html(data);
			}
		},
		submitError: function(request, type, e) {
			
		},
		append: function(data) {
			var c = this.children('.comments-show-item-container');
			c.append(data);
			setTimeout($.proxy(function() {
				var el = c.children().last();
				$.scrollTo(el, 1000);
				el.effect("highlight", {}, 3000);
			}, this), 1);
			span = $('.comments-show-head').find('#commentaries-count').first();
			span.html(+ $.trim(span.html()) + 1);
		},
		replace: function(data) {
			var c = this.children('.comments-show-item-container');
			c.append(data);
			setTimeout($.proxy(function() {
				var el = c.children().last();
				$.scrollTo(el, 1000);
				el.effect("highlight", {}, 3000);
			}, this), 1);
			span = $('.comments-show-head').find('#commentaries-count').first();
			span.html(+ $.trim(span.html()) + 1);
		},
		complain: function() {
			el = $(this);
			$.ajax({
			      url: el.find('a[class=complain]').first().attr('href'),
			      type: 'GET',
			      success: $.proxy($.fn.commentify.complainSuccess, el),
			      error: $.fn.commentify.complainError
			});
		},
		complainSuccess: function(data, status, request) {
			if (request.status==201) {
				el.replaceWith(data);
			}
		},
		complainError: function() {
			
		},
		togglecomplained: function(e, f) {
			el = $(this);
			$.ajax({
			      url: el.find('a[class~=togglecomplained]').first().attr('href'),
			      type: 'GET',
			      success: $.proxy(function(data, status, request) {
			    	  	this.commentify.togglecomplainedSuccess.call(this, data, status, request);
//			    	  	f && setTimeout($.proxy(f, this), 100, e);
			      }, this),
			      error: $.fn.commentify.togglecomplainedError
			});
		},
		togglecomplainedSuccess: function(data, status, request, f) {
			if (request.status==201) {
				el.replaceWith(data);
			}
		}, 
		togglecomplainedError: function() {
			
		},
		uncomplain: function() {
			el = $(this);
			$.ajax({
			      url: el.find('a[class=uncomplain]').first().attr('href'),
			      type: 'GET',
			      success: $.proxy($.fn.commentify.uncomplainSuccess, el),
			      error: $.fn.commentify.uncomplainError
			});
		}, 
		uncomplainSuccess: function(data, status, request) {
			if (request.status==201) {
				el.replaceWith(data);
			}
		}, 
		uncomplainError: function() {
			
		}
	});
})(jQuery);

jQuery(function($) {
	$('.comments-show-block').commentify();
});

