App.Comments = newClass();
App.Comments.prototype = {

	init: function(p) {
		var t = this;
		t.p = p;
		
		t.commentsElement = $('#' + t.p.comments);
		t.dynamicAnswer = $('#' + t.p.dynamicAnswer);
		t.newComment = $('#' + t.p.newComment);
		t.newCommentRef = $('#' + t.p.newCommentRef);
		t.dynamicAnswerSubmit = $('#' + t.p.dynamicAnswerSubmit);
		t.dynamicEditSubmit = $('#' + t.p.dynamicEditSubmit);
		t.dynamicCloseSubmit = $('#' + t.p.dynamicCloseSubmit);
		t.dynamicContent= $('#' + t.p.dynamicContent);
		t.dynamicAnswerLoader = $('#' + t.p.dynamicAnswerLoader);
		t.dynamicAnswerLoaderBg = $('#' + t.p.dynamicAnswerLoaderBg);
		t.dynamicAnswerLoaderBg.fadeTo('fast', 0.7);
		t.currentDynamicCommentParent = null;
		
		// blocked is true if there is a waiting of ajax response
		t.blocked = false;
		
		// TODO: if textarea is empty then submit button is disabled
		
		t.textarea = t.dynamicAnswer.find('textarea');
		if(t.textarea.val() == '') {
			t.dynamicAnswerSubmit.attr('disabled', 'disabled');
			t.dynamicEditSubmit.attr('disabled', 'disabled');
		}
		
		t.textareaTimer = setInterval(function() {
			t.reviseTextarea();
		}, 100);
		
		
		t.newCommentRef
		.click(function(e) {
			return t.newCommentRefClick(e);
		});
		
		t.comments = {};
		t.commentsElement.find('.' + t.p.commentC).each(function(i) {
			var index = $(this).attr('id').replace(t.p.commentPrefix, '');
			t.comments[index] = $(this);
			
			t.comments[index].find('#' + t.p.answerRefPrefix + index).click(function(e) {
				return t.answerRefClick(e);
			});
			
			t.comments[index].find('#' + t.p.editRefPrefix + index).click(function(e) {
				return t.editRefClick(e);
			});
			
			t.comments[index].find('#' + t.p.deleteRefPrefix + index).click(function(e) {
				return t.dynamicDeleteClick(e);
			});
		});
		
		t.setLastCommentIndex();
		
		t.dynamicAnswerSubmit
		.click(function(e) {
			return t.dynamicAnswerSubmitClick(e);
		});
		
		t.dynamicEditSubmit
		.click(function(e) {
			return t.dynamicEditSubmitClick(e);
		});
		
		t.dynamicCloseSubmit
		.click(function(e) {
			var t = this;
			var element = t.parentNode.parentNode.parentNode.parentNode;
			var elid = element.id.split('_')[1];
			$('#dynamic_answer').hide();
			$('#answer_ref_'+elid, element).show();
			$('#edit_ref_'+elid, element).show();
			$('#new_comment_ref',element).addClass('Underlined');
			$('#directory_poll_rating_div').css('display','block');
		});
	},
	
	/**
	*
	* return false if textarea is empty, else: true
	*
	*/
	reviseTextarea: function() {
		var t = this;
				
		if(t.textarea.val() == '') {
			t.dynamicAnswerSubmit.attr('disabled', 'disabled');
			t.dynamicEditSubmit.attr('disabled', 'disabled');
			return false;
		} else {
			t.dynamicAnswerSubmit.removeAttr('disabled');
			t.dynamicEditSubmit.removeAttr('disabled');
			return true;
		}
	},
	
	newCommentRefClick: function(e) {
		var t = this;
		
		if(t.isBlocked()) {
			return false;
		}
		t.moveDynamicCommentTo(null);
				
		return false;
	},
	
	answerRefClick: function(e) {
		var t = this;
		
		if(t.isBlocked()) {
			return false;
		}
		
		var element = $(e.target);
		var index = element.attr('id').replace(t.p.answerRefPrefix, '');
		
		t.moveDynamicCommentTo(index);
		
		return false;
	},
	
	editRefClick: function(e) {
		var t = this;
		$('#directory_poll_rating_div').css('display','none');
		if(t.isBlocked()) {
			return false;
		}
		
		var element = $(e.target);
		var index = element.attr('id').replace(t.p.editRefPrefix, '');
		
		t.moveDynamicCommentToE(index);
		
		return false;
	},
	
	moveDynamicCommentTo: function(index) {
		var t = this;
		
		if(t.currentDynamicCommentParent != null) {
			if(t.comments[t.currentDynamicCommentParent] != undefined) {
				t.comments[t.currentDynamicCommentParent]
				.find('#' + t.p.editRefPrefix + t.currentDynamicCommentParent)
				.css('display', 'inline');
				
				t.comments[t.currentDynamicCommentParent]
				.find('#' + t.p.answerRefPrefix + t.currentDynamicCommentParent)
				.css('display', 'inline')
			}
			
		} else {
			t.newCommentRef.addClass(t.p.underlinedC);
		}
		t.currentDynamicCommentParent = index;
		
		if(index != null) {
			
			t.comments[index]
			.find('#' + t.p.editRefPrefix + index)
			.css('display', 'inline');
			
			t.comments[index]
			.find('#' + t.p.answerRefPrefix + index)
			.css('display', 'none');
						
			t.dynamicAnswerSubmit.val(t.p.answerText);
			t.dynamicEditSubmit.css('display', 'none');
			t.dynamicAnswerSubmit.css('display', 'inline');
			t.dynamicContent= $('#comment_'+index+' #' + t.p.dynamicContent);
			t.textarea.val('');
			
			t.comments[index].find('#' + t.p.commentBodyPrefix + index)
			.after(t.dynamicAnswer);
			t.dynamicAnswer.show();

		} else {
			t.newCommentRef.removeClass(t.p.underlinedC);
			
			t.dynamicAnswerSubmit.val(t.p.postText);
			t.dynamicEditSubmit.css('display', 'none');
			t.dynamicAnswerSubmit.css('display', 'inline');
			t.dynamicContent= $('#comment_'+index+' #' + t.p.dynamicContent);
			t.textarea.val('');

			t.newComment.append(t.dynamicAnswer);
			t.dynamicAnswer.show();
		}
		
		t.textarea.focus();
		
	},
	
	moveDynamicCommentToE: function(index) {
		var t = this;
		
		if(t.currentDynamicCommentParent != null) {
			if(t.comments[t.currentDynamicCommentParent] != undefined) {
				
				t.comments[t.currentDynamicCommentParent]
				.find('#' + t.p.answerRefPrefix + t.currentDynamicCommentParent)
				.css('display', 'inline');
				
				t.comments[t.currentDynamicCommentParent]
				.find('#' + t.p.editRefPrefix + t.currentDynamicCommentParent)
				.css('display', 'inline');
			}
		} else {
			t.newCommentRef.addClass(t.p.underlinedC);
		}
		t.currentDynamicCommentParent = index;
		
		if(index != null) {
			t.comments[index]
			.find('#' + t.p.answerRefPrefix + index)
			.css('display', 'inline');
			
			t.comments[index]
			.find('#' + t.p.editRefPrefix + index)
			.css('display', 'none');
						
			t.dynamicEditSubmit.val(t.p.answerTextE);
			t.dynamicEditSubmit.css('display', 'inline');
			t.dynamicAnswerSubmit.css('display', 'none');
			t.dynamicContent= $('#comment_'+index+' #' + t.p.dynamicContent);
			t.textarea.val(t.dynamicContent.val());
			t.comments[index].find('#' + t.p.commentBodyPrefix + index)
			.after(t.dynamicAnswer);
			t.dynamicAnswer.show();
		}
		
		t.textarea.focus();
		
	},
	
	setLastCommentIndex: function() {
		var t = this;
		
		var maxIndex = null;
		
		/* t.comments should be topical in this time */
		for(i in t.comments) {
			if(maxIndex == null) {
				maxIndex = i
			} else {
				maxIndex = i > maxIndex ? i : maxIndex;
			}
		}
		
		t.lastCommentIndex = maxIndex;
	},
	
	dynamicAnswerSubmitClick: function(e) {
		var t = this;
		
		if(t.isBlocked()) {
			return false;
		}
		
		if(t.reviseTextarea()) {
			t.ajaxAddComment();
		};
		
		return false;
	},

	dynamicEditSubmitClick: function(e) {
		var t = this;
		
		if(t.isBlocked()) {
			return false;
		}
		
		if(t.reviseTextarea()) {
			t.ajaxEditComment();
		};
		
		return false;
	},
	
	dynamicDeleteClick: function(e) {
		var t = this;
		var element = $(e.target);
		var index = element.attr('id').replace(t.p.deleteRefPrefix, '');
		
				
		if(t.isBlocked()) {
			return false;
		}
		
		t.ajaxDeleteComment(index);		
		return false;
	},
	
	/**
	* 
	* Serially post ajax request for new comments from the server
	* 
	*/
	ajaxAddComment: function() {
		var t = this;
		
		t.block();
		
		// TODO: block answerRef click events
		
		var text = t.dynamicAnswer.find('textarea').val();
		var user_name = t.dynamicAnswer.find('#dynamic_name').val();
		
		var data = {
			text: escape(text),
		//lastCommentIndex: t.lastCommentIndex,
			parent_id: t.currentDynamicCommentParent,			
			article: sThreadTable,
			thread_id:iThreadId,
			user_name:escape(user_name),
			iPollRating:t.dynamicAnswer.find('#directory_poll_rating').val()
		};
		
		/*
		jQuery.post(t.p.ajaxAddCommentUrl, data, function(response) {
			// return t.ajaxAddCommentSuccess(response);
		});
		*/
		
		
		$.get(t.p.ajaxAddCommentUrl, data, function(data) {
			window.location.href = './';
		});
		
	},
	
	ajaxEditComment: function() {
		var t = this;
		
		t.block();
		
		// TODO: block answerRef click events
		
		var text = t.dynamicAnswer.find('textarea').val();
		
		
		var data = {
			text: escape(text),
		//lastCommentIndex: t.lastCommentIndex,
			id: t.currentDynamicCommentParent,			
			article: sThreadTable,
			thread_id:iThreadId
		};
		
		/*
		jQuery.post(t.p.ajaxAddCommentUrl, data, function(response) {
			// return t.ajaxAddCommentSuccess(response);
		});
		*/
		
		
		$.get(t.p.ajaxAddCommentUrl, data, function(data) {
			window.location.href = './';
		});
		
	},
	
	ajaxDeleteComment: function(index) {
		var t = this;
		
		t.block();
		
		// TODO: block answerRef click events
		
		var data = {
		//lastCommentIndex: t.lastCommentIndex,
			id: index,			
			article: sThreadTable,
			thread_id:iThreadId
		};
		
		/*
		jQuery.post(t.p.ajaxAddCommentUrl, data, function(response) {
			// return t.ajaxAddCommentSuccess(response);
		});
		*/
		
		
		$.get(t.p.ajaxAddCommentUrl, data, function(data) {
			window.location.href = './';
		});
		
	},
		
	block: function() {
		var t = this;
		t.blocked = true;
		t.dynamicAnswerLoader.css('display', 'block');
	},
	
	unblock: function() {
		var t = this;
		t.blocked = false;
		t.dynamicAnswerLoader.css('display', 'none');
	},
	
	isBlocked: function() {
		return this.blocked;
	},
	
	ajaxAddCommentSuccess: function(r) {
		
	}
};