var deleteComment = function(el, id) {
	this.apply = function(o) {
		if(o.responseText == 'success') {
			el.parentNode.parentNode.parentNode.style.backgroundColor = '#FAFAFA';
			el.parentNode.parentNode.parentNode.style.textDecoration = 'line-through';
		}
		else {
			alert('Не удалось удалить комментарий. Попробуй перезагрузить страницу.');
		}
	};
	ajax('http://'+thisURL[0]+'/'+thisURL[1]+'/hidecomment/'+id+'/yes', '', this.apply);
	return false;
}

var placeComment = function(el, id, lcid) {
	var length = el.length;
	this.apply = function(o) {
		var json = eval(o.getResponseHeader('X-JSON'));
		this.node = document.getElementsByClassName('comments');
		var comments = document.getElementById('comments');
		if('\v'=='v') comments = comments.getElementsByTagName('tbody')[0];
		if(node.length <= 1) {
			comments.appendChild(elem('tr')).appendChild(elem('td', {'bgcolor': '#ffffff', 'height': 10}));
			comments.appendChild(elem('tr')).appendChild(elem('td', {'bgcolor': '#abdae0', 'height': 1}));
			comments.appendChild(elem('tr')).appendChild(elem('td', {'class': 'tx8', 'align': 'center', 'bgcolor': '#e5e5e5', 'height': 20}, {}, 'Комментарии'));
			comments.appendChild(elem('tr')).appendChild(elem('td', {'bgcolor': '#ffffff', 'height': 10}));
		}
		for(var i = 0; i < json.length; i++) {
			var text = '';
			if(json[i].avatar != '')
				text = '<img src="/forum/images/avatars/' + json[i].avatar + '" hspace="5" vspace="5" align="left" border="0" />';
			text += '<b><a href="/forum/profile.php?mode=viewprofile&u=' + json[i].user_id + '" title="Посмотреть профиль комментатора">' + json[i].name + '</a></b>&nbsp;<span class="comments">' + json[i].time + '<br>комментариев: <a href="/comments/rating/">' + json[i].comments + '</a>&nbsp;&nbsp;|&nbsp;&nbsp;предупреждений: ' + json[i].warnings + /*(typeof json[i].ip != 'undefined' ? ' (<a href="/index.php?moder/warning/' + json[i].comment_id + '">+</a>/<a href="/index.php?moder/excuse/' + json[i].comment_id + '">-</a>)' : '') + */' (5)&nbsp;&nbsp;|&nbsp;&nbsp;статус: ' + json[i].status;
//			if(typeof json[i].ip != 'undefined')
//				text += '<div><a href="/index.php?news/hidecomment/' + json[i].comment_id + '" onclick="return deleteComment(this,' + json[i].comment_id + ');">удалить</a> &nbsp; IP:' + json[i].ip + '</div>';
			text += '</span><br /><br />' + json[i].text;
			comments.appendChild(elem('tr')).appendChild(elem('td')).appendChild(elem('table', {'class': 'russia', 'cellspacing': 0, 'cellpadding': 5}, {'marginBottom': '10px'})).appendChild(elem('tbody')).appendChild(elem('tr')).appendChild(elem('td', {'class': 'tx13'})).innerHTML = text;
		}
		el.onsubmit = function() { return placeComment(this, id, json[json.length-1].comment_id); };
		el.parentNode.style.display = 'none';
		el.parentNode.parentNode.getElementsByTagName('td')[1].style.display = '';
	};
	this.error = function(o) {
		var length = el.length;
		alert('Не удалось добавить комментарий. Попробуйте отправить его чуть позже.');
		el.elements[0].style.background = 'none';
		while(length--)
			el.elements[length].removeAttribute('disabled');
	};
	if(el.elements[0].value != '') {
		ajax('http://'+thisURL[0]+'/'+thisURL[1]+'/comment/'+id+'/'+lcid+'/yes', 'add_comment', this.apply, this.error);
		el.getElementsByTagName('textarea')[0].style.background = '#fafafa url(/images/loading.gif) no-repeat center center'
		while(length--)
			el.elements[length].setAttribute('disabled', 'disabled');
	}
	return false;
}

var openCommentForm = function() {
	var el = document.getElementsByName('add_comment')[0], msg = el.getElementsByTagName('textarea')[0], length = el.length;
	el.parentNode.style.display = '';
	el.parentNode.parentNode.getElementsByTagName('td')[1].style.display = 'none';
	msg.style.background = 'none';
	msg.value = '';
	while(length--)
		el.elements[length].removeAttribute('disabled');
	msg.focus();
	return false;
}