function applyreplyform(replyLinks){
	for (var i = 0; i < replyLinks.length; i++) {
		var link = replyLinks[i];
		if (link.className == 'tv-reply' || link.className == 'tv-badreply' || link.className == 'tv-reply-not-full') {
			link.onclick = function(self) {
				var replyForm = document.getElementById('writecomment');
				replyForm.parentNode.removeChild(replyForm);
				document.getElementById('m'+this.id).appendChild(replyForm);
				replyForm.style.display = 'block';
				document.getElementById('msgpid').value = this.id;
				if (link.className == 'tv-reply'){
					document.getElementById('msgtext').value = '';
				}
				return null;
			}
			
		}
		
		if (link.className == 'tv-badreply') {
			link.onclick();
		}

		if ($(link).is('.karma-plus, .karma-minus')) {
			karmaColor(link.parentNode.getElementsByTagName('a')[0]);
			$(link).click(function(e) {
				if($(this).is('.club-inactive-button')){
					return false;
				}
				var karmaNumber = this.parentNode.getElementsByTagName('strong')[0];
				var otherParams = '';
				if ($(this).is('.karma-minus')) {
					var vote = -1;

					if (this.id.search('post') != -1) {
						var rateId = this.id.replace('postminus','');
						var scriptName = 'club';
					} else if (this.id.search('karma') != -1) {
						var rateId = this.id.replace('karmaminus','');
						var scriptName = 'usersfship';
						karmaNumber = this.parentNode.getElementsByTagName('strong')[0];
						otherParams = '&domain=karma';
					} else {
						var rateId = this.id.replace('minus','');
						var scriptName = 'confmessage';
					}
					var voice = 'f';
				} else {
					var vote = 1;
					if (this.id.search('post') != -1) {
						var rateId = this.id.replace('postplus','');
						var scriptName = 'club';
					} else if (this.id.search('karma') != -1) {
						var rateId = this.id.replace('karmaplus','');
						var scriptName = 'usersfship';
						karmaNumber = this.parentNode.getElementsByTagName('strong')[0];
						otherParams = '&domain=karma';
					} else {
						var rateId = this.id.replace('plus','');
						var scriptName = 'confmessage';
					}
					var voice = 't';
				}
				
				var url = "/"+scriptName+".action?action=rate&id="+rateId+"&voice="+voice+otherParams;
				$.ajax({
					type:      'GET',
					cache:     false,
					url:       url
				});
				
				karmaNumber.innerHTML = karmaNumber.innerHTML * 1 + vote;
				if ($(this).parent().hasClass('club-user-rating')) {
					$(this).addClass('club-inactive-button');
					$(this).siblings('a:last').removeClass('club-inactive-button');
				} else {
					$(this).hide();
					$(this).siblings('a:last').hide();
				}
				karmaColor(karmaNumber);
				return null;
			})
		}
	}
}

function showreply() {
	if (document.getElementById('page-comments')) {
		var replyLinks = document.getElementById('page-comments').getElementsByTagName('a');
		applyreplyform(replyLinks);
	}
	return null;
}

function showclubreply() {
	var replyLinks = $("div.club-topic-info a");
	applyreplyform(replyLinks);
	return null;
}

function showuserreply() {
	if (document.getElementById('club-user-stats')) {
		var replyLinks = document.getElementById('club-user-stats').getElementsByTagName('a');
		applyreplyform(replyLinks);
	}
	return null;
}

function karmaColor(karmaNumber) {
	if ((karmaNumber.innerHTML)*1 > 0) {
		karmaNumber.className = 'karma-number good';
	}
	if (karmaNumber.innerHTML == '0') {
		karmaNumber.className = 'karma-number neutral';
	}
	if ((karmaNumber.innerHTML)*1 < 0) {
		karmaNumber.className = 'karma-number bad';
	}
	return null;
}

$(document).ready(function() {
	if ('#write' == window.location.hash) {
		document.getElementById('writecomment').style.display = 'block';
	}
});
