﻿$(document).ready(function(){
	var init_comments = function() {
		$("#comments a.toggle-form, #add-comment a").click(function(){
			$("#comments form, #comment-form").hide();
			if ($(this).parent().attr("id") == 'add-comment'){
				$("#comment-form").show();
			} else {
				$(this).parent().parent().find("form:first").show();
			}
			return false;
		});
		
		$("li.comment form, #comment-form").submit(function(){
			var textarea = $(this).children('textarea:first');
			if (textarea.val().match(/^\s*$/)) {
				textarea.fadeTo("fast", 0.1, function(){ 
					textarea.fadeTo("fast", 1);
				});
				$('input[type=submit]', $(this)).attr('disabled', '');
				
				return false;
			}
			
			return true;
		});
		
		$("li.comment form input[type=submit], #comment-form input[type=submit]").click(function(){
			$(this).attr('disabled', 'disabled');
			$(this).parent().submit();
			
			return false;
		});
		
		$("li.comment form input[type=submit], #comment-form input[type=submit]").dblclick(function(){
			$(this).attr('disabled', 'disabled');
			$(this).parent().submit();
			
			return false;
		});

		/* hide bad comments */
		$(".comment-hidden").each(function(){
			var self = $(this);
			var className = 'comment-hidden';
			var showLink = $("a.show-comment-link", self);
			var hideLink = $("a.hide-comment-link", self);
			
			showLink.click(function(){
				self.removeClass(className);
				
				return false;
			});
			
			hideLink.click(function(){
				self.addClass(className);
				
				return false;
			});
		});
	}
	init_comments();
	
	// new comments
	(function($) {
		var app = {};
		app.options = {
			root: '#comments',
			list: '#comments ul.comment-list:first',
			link: '#load-comments',
			loader: '#comments img.loader',
			comments: '#comments ul.comment-list li.comment',
			first: '#comments ul.comment-list li.comment:first',
			num: '#num-comments'
		}
		app.cont = {};
		app.load = function(url, offset) {
			$.ajax({
				url: url,
				data: {offset: offset},
				beforeSend: function() {
					app.cont.loader.show();
					app.cont.link.addClass('lock');
				},
				error: function() {
					app.cont.loader.hide();
					alert('Произошла ошибка. Попробуйте еще раз.')
					app.cont.link.removeClass('lock');
				},
				success: function(data) {
					app.cont.loader.hide();
					app.cont.list.append(data);
					init_comments();
					window.kr = new window.KarmaRank('/rank/');
					app.cont.link.removeClass('lock');
					if ($(app.options.comments).length == app.cont.num.text()) {
						app.cont.link.hide()
					}
				}
			});
		}
		app.click_handler = function() {
			if ($(this).hasClass('lock'))
				return false;

			var url = $(this).attr('href');
			app.load(url, $(app.options.root + ' li.comment-level-0').length);

			return false;
		}
		app.run = function() {
			var o = app.options;
			app.cont.root = $(o.root);
			app.cont.list = $(o.list);
			app.cont.link = $(o.link);
			app.cont.loader = $(o.loader);
			app.cont.first = $(o.first);
			app.cont.num = $(o.num);

			if (app.cont.root && app.cont.link) {
				app.cont.link.click(app.click_handler);
			}
		}

		app.run();
	})(jQuery);
});

