var errorText = new Array();
errorText["emptyBody"] = "Пожалуйста, введите ваш комментарий.";
errorText["tooFast"] = "Вы пытаетесь комментировать слишком быстро.";
errorText["sameComment"] = "Вы пытаетесь оставить два одинаковых комментария подряд.";

function fLoadComments(url) {	
	new Request.HTML({update: 'commentbody', 
		onComplete: function(response) {
			fAjaxComment(1);
			window.location.href = appendAnchor('comments');
		}
	}).get(url);	
}

function fDeleteComment(url, entityId) {
	new Request.HTML({ 
		onComplete: function(response) {
			$('commentDeleteLink'+entityId).setStyle('display', 'none');
			$('commentRestoreLink'+entityId).setStyle('display', 'inline');
			$('commentBody'+entityId).setStyle('display', 'none');
			$('commentDeleted'+entityId).setStyle('display', 'block');
		}
	}).get(url);
}

function fRestoreComment(url, entityId) {
	new Request.HTML({ 
		onComplete: function(response) {
			$('commentDeleteLink'+entityId).setStyle('display', 'inline');
			$('commentRestoreLink'+entityId).setStyle('display', 'none');
			$('commentBody'+entityId).setStyle('display', 'block');
			$('commentDeleted'+entityId).setStyle('display', 'none');
		}
	}).get(url);
}

function fAjaxComment(setToolTip) {
	commentPaging = $$('.commentPaging a');
	for(i=0;i<commentPaging.length;i++) {
		commentPaging[i].addEvent('click', function(e) {
			e.stop();			
			fLoadComments(this.get('href'));
		});
	}
	
	commentDelete = $$('.commentDelete');
	for(i=0;i<commentDelete.length;i++) {
		commentDelete[i].addEvent('click', function(e) {
			e.stop();		
			fDeleteComment(this.get('href'), this.get('rel'));				
		});
	}
	
	commentRestore = $$('.commentRestore');
	for(i=0;i<commentRestore.length;i++) {
		commentRestore[i].addEvent('click', function(e) {
			e.stop();		
			fRestoreComment(this.get('href'), this.get('rel'));				
		});
	}
	
	if ( setToolTip )
		setToolTips(1);
}

window.addEvent('domready', function() {
	commentUrlData = JSON.decode($('commentbody').get('rel'));	
	
	if ($('comment')) {
		$('comment').addEvent('submit', function(e){
			e.stop();
			this.set('send', {
				onComplete: function(response) {
					responseText = response.trim();
					if (responseText != 'ok') {
						errors = JSON.decode(responseText);
						for (var i in errors) {
							errorMessage(i, errorText[errors[i]]);
						}
					}
					else {
						$('body').set('value', '');
						fLoadComments('comment/listing/' + commentUrlData.controller + '/' + commentUrlData.entityId+'/0');
					}
					
					$('save').setStyle('display', 'inline');		
					$('loading').setStyle('display', 'none');
				}
			});
			
			var ok = true;
			
			errorClear();
			
			if ($('body').value.trim().length < 1) {
				errorMessage('body', errorText["emptyBody"]);
				ok = false;
			}
			
			if (ok) {
				$('save').setStyle('display', 'none');		
				$('loading').setStyle('display', 'block');
				this.send();
			}
		});
	}
	
	fAjaxComment(0);	
});
