function commentHandler() {
	
	var me = this;
	
	this.init = function() {
	
		var sub = document.getElementById('btnComment');
		if(!sub)
			return;
		if(navigator.userAgent.indexOf('MSIE') > -1) { 
			var tmpBtn = sub;
				sub = document.createElement('<INPUT type="button" />');
				sub.value = tmpBtn.value;
				sub.id = 'btnComment';
			tmpBtn.parentNode.insertBefore(sub, tmpBtn);
			tmpBtn.parentNode.removeChild(tmpBtn);
		} else
			sub.setAttribute('type', 'button');
		sub.onclick = function() {
			me.addComment();
		}
			
	}
	
	this.addComment = function() {
		
		//debugger;
			
		var form = document.getElementById('comment_form');
		var comment = document.getElementById('comment_text');
		
		if(comment.value.length == 0) {
			alert('You must type in something!');
			return;
		}
		
		var itemId = document.getElementById('item');
		var params = new Array();
			params['itemId'] = itemId.value;
			params['comment_text'] = comment.value;
			params['ajax'] = true;
		doRequest(form.getAttribute('action'), function(res) {
			if(!res.responseXML) {
				alert(res.responseText);
				return;
			}
			me.displayComment(res.responseXML.documentElement, comment.value);
		}, params);
		
	}
	
	this.displayComment = function(res, commentText) {
		
		var id = res.getAttribute('id');
		var username = res.getAttribute('username');
		var image = res.getAttribute('userimage');
		var tools_view = res.getAttribute('view');
		
		var firstTComment = document.getElementById('firstComment');
		if(firstTComment)
			firstTComment.style.display = 'none';
		
		var container = document.getElementById('collectibles_specific_comments_details_container');
		var form = document.getElementById('comment_form');
		
		var comments = me.getCommentContainers(container);
		document.getElementById('commentsCount').innerHTML = 'Comments (' + (comments.length+1) + ')';
		var nextClass = ((comments.length == 0) ? 'collectibles_specific_comments_row1' : ((comments[comments.length-1].className == 'collectibles_specific_comments_row1') ? 'collectibles_specific_comments_row2' : 'collectibles_specific_comments_row1'));
		
		var comment = document.createElement('DIV');
			comment.className = nextClass;
			comment.id = 'comment_cont_' + id;
			
		var anc = document.createElement('A');
			anc.name = 'comment:' + id;
			comment.appendChild(anc);
		
		var imageContainer = document.createElement('DIV');
			imageContainer.className = 'collectibles_specific_comments_userpic';
		var userimage = document.createElement('IMG');
			userimage.setAttribute('title', username);
			userimage.setAttribute('alt', username);
			userimage.src = ((image == '') ? 'http://www.collectionstation.com/resources/images/misc/profile_pic_50x50.gif' : 'http://www.collectionstation.com/resources/images/profile/thumb/' + image);
			userimage.style.width = '50px';
			userimage.style.height = '50px';
			imageContainer.appendChild(userimage);
			comment.appendChild(imageContainer);
		var comment_data = document.createElement('DIV');
			comment_data.className = 'collectibles_specific_comments_postcont';
		var user_cont = document.createElement('DIV');
			user_cont.className = 'collectibles_specific_comments_username';
		var user_link = document.createElement('A');
			user_link.appendChild(document.createTextNode(username));
			user_link.href = 'http://www.collectionstation.com/'+username;
			user_cont.appendChild(user_link);
			user_cont.appendChild(document.createTextNode(' commented ' + me.commentTime(new Date())));
			comment_data.appendChild(user_cont);
			
		var itemId = document.getElementById('item');
		var commentCount = document.createElement('DIV');
			commentCount.className = 'comment_count';
		var commentCountLink = document.createElement('A');
			commentCountLink.href = 'http://www.collectionstation.com/item/view/'+itemId.value+'/comments/' + id;
			commentCountLink.appendChild(document.createTextNode('#' + (comments.length+1)));
			commentCount.appendChild(commentCountLink);
			comment_data.appendChild(commentCount);	
			
		var rss_cont = document.createElement('DIV');
			rss_cont.className = 'collectibles_specific_comments_rss';
			rss_cont.id = 'comment_rss';
		var rss_link = document.createElement('A');
			rss_link.href = '#';
		var rss_img = document.createElement('IMG');
			rss_img.src = 'http://www.collectionstation.com/resources/images/icons/rss_16x16.png';
			rss_link.appendChild(rss_img);
			rss_cont.appendChild(rss_link);
		var temp = document.getElementById('comment_rss');
			if(!temp)
				comment_data.appendChild(rss_cont);
				
				
		var comment_text = document.createElement('DIV');
			comment_text.className = 'collectibles_specific_comments_post';
			comment_text.id = 'comment_' + id;
			comment_text.innerHTML = me.nl2br(commentText);
			comment_data.appendChild(comment_text);
			
		var comment_tools = document.createElement('DIV');
			comment_tools.className = 'collectibles_specific_comments_tools';
		var tools_edit = document.createElement('A');
			tools_edit.id = 'edit_' + id;
			tools_edit.onclick = function() {
				me.editComment(this.id.split('_')[1]);				
			}
			tools_edit.href = 'javascript:void(0)';
			tools_edit.appendChild(document.createTextNode('Edit'));
		comment_tools.appendChild(tools_edit);
		comment_tools.appendChild(document.createTextNode(' | '));
		var tools_delete = document.createElement('A');
			tools_delete.id = 'delete_' + id;
			tools_delete.onclick = function() {
				me.deleteComment(this.id.split('_')[1]);
			}
			tools_delete.href = 'javascript:void(0)';
			tools_delete.appendChild(document.createTextNode('Delete'));
		comment_tools.appendChild(tools_delete);
		if(tools_view)
			comment_data.appendChild(comment_tools);
		comment.appendChild(comment_data);
		var clearer = document.createElement('BR');
			clearer.style.clear = 'both';
		comment.appendChild(clearer);
		container.insertBefore(comment, form);
		document.getElementById('comment_text').value = '';
		new Effect.Highlight(comment, {queue: 'front'});
		new Effect.ScrollTo(comment, {queue: 'end'});
	}
	
	this.nl2br = function(text){
		return text.replace(/\n/g, "<br />");
	}
	
	this.commentTime = function(d) {
		
		var hour = d.getHours();
		var isAM = true;
		if(hour < 10) {
			hour = '0' + hour;
			isAM = true;
		} else if(hour > 12) {
			isAM = false;
			hour = hour - 12;
		}
		var minute = d.getMinutes();
		if(minute < 10)
			minute = '0' + minute;
		
		return 'Today at ' + hour + ':' + minute + ((isAM) ? ' AM' : ' PM');
	}
	
	this.removeComment = function(id) {
		
		var comment_cont = document.getElementById('comment_cont_'+id);
		var form = document.getElementById('comment_form');
		new Effect.SlideUp(comment_cont, { afterFinish: function(obj) {
			var par = obj.element.parentNode;
				par.removeChild(obj.element);
			var comments = me.getCommentContainers(par);
			if(comments.length == 0)
				document.getElementById('commentsCount').innerHTML = 'Comments';
			else
				document.getElementById('commentsCount').innerHTML = 'Comments (' + comments.length + ')';
			var firstTComment = document.getElementById('firstComment');
			if(comments.length == 0) {
				if(firstTComment)
					firstTComment.style.display = 'block';
				else {
					firstTComment = document.createElement('DIV');
					firstTComment.id = 'firstComment';
					firstTComment.innerHTML = '<br />&nbsp;&nbsp;&nbsp;No comments have been made. Be the first to comment!';
					par.insertBefore(firstTComment, form);
				}
			} else {
				if(firstTComment)
					firstTComment.style.display = 'none';
				if(!document.getElementById('comment_rss')) {
					var comment = comments[0];
					var cont = document.getElementById('comment_' + comment.id.split('_')[2]);
					var rss_cont = document.createElement('DIV');
						rss_cont.className = 'collectibles_specific_comments_rss';
						rss_cont.id = 'comment_rss';
					var rss_link = document.createElement('IMG');
						rss_link.src = 'http://www.collectionstation.com/resources/images/icons/rss_16x16.png';
						rss_link.href = '#';
						rss_cont.appendChild(rss_link);
					cont.parentNode.insertBefore(rss_cont, cont);
				}
			}
		}});
		
	}
	
	this.getBaseUrl = function(url) {
		
		var parts = url.split('/');
		var newUrl = '';
		for(var i = 0; i < parts.length-((url[url.length-1] == '/') ? 2 : 1); i++) {
			newUrl += parts[i] + '/';
		}
		return newUrl;
		
	}
	
	this.deleteComment = function(id) {
			
		if(!confirm('Are you sure?'))
			return;
		var form = document.getElementById('comment_form');
		var params = new Array();
			params['id'] = id;
			params['ajax'] = true;
		doRequest(me.getBaseUrl(form.getAttribute('action')) + 'delete', function(res) {
			if(!res.responseXML) {
				alert(res.responseText);
				return;
			}
			me.removeComment(id);
		}, params);
		
	}
	
	this.editComment = function(id) {
		
		var form = document.getElementById('comment_form');
		var comment_text = document.getElementById('comment_'+id);
		var currentText = comment_text.innerHTML;
			currentText = currentText.replace(/[\n\r]+/g, '');
			currentText = currentText.replace(/\<br\>/g, "\n");	
			currentText = currentText.replace(/\<br \/\>/g, "\n");	
			//alert(currentText);
		var cont = document.createElement('DIV');
		var textarea = document.createElement('TEXTAREA');
			textarea.id = 'editting_comment_' + id;
			textarea.value = currentText;
			textarea.setAttribute('original', currentText);
			comment_text.innerHTML = '';
			cont.appendChild(textarea);
		var controls = document.createElement('DIV');
		var submit = document.createElement('INPUT');
			submit.setAttribute('type', 'button');
			submit.value = 'Save';
			submit.id = 'editting_submit_' + id;
			submit.onclick = function() {
				var form = document.getElementById('comment_form');
				var id = this.id.split('_')[2];
				var commentText = document.getElementById('editting_comment_' + id);
				var params = new Array();
					params['id'] = id;
					params['comment_text'] = commentText.value;
				doRequest(me.getBaseUrl(form.getAttribute('action')) + 'update', function(res){
					if(!res.responseXML) {
						alert(res.responseText);
						return;
					}
					var comment_text = document.getElementById('comment_'+id);
						comment_text.innerHTML = '';
						comment_text.innerHTML = me.nl2br(commentText.value);
					new Effect.Highlight('comment_cont_' + id);
				}, params);
			}
			controls.appendChild(submit);
		var cancel = document.createElement('INPUT');
			cancel.setAttribute('type', 'button');
			cancel.value = 'Cancel';
			cancel.id = 'editting_cancel_' + id;
			cancel.onclick = function() {				
				var id = this.id.split('_')[2];
				var original = document.getElementById('editting_comment_' + id).getAttribute('original');
				var comment_text = document.getElementById('comment_'+id);
					comment_text.innerHTML = me.nl2br(original);
				new Effect.Highlight('comment_cont_' + id);
			}
			controls.appendChild(document.createTextNode('\u00a0'));
			controls.appendChild(cancel);
			cont.appendChild(controls);
			comment_text.appendChild(cont);
	}	
	
	this.getCommentContainers = function(c) {
		
		var results = new Array();
		for(var i = 0; i < c.childNodes.length; i++) {
			
			if(c.childNodes[i].className && c.childNodes[i].className.indexOf('collectibles_specific_comments_row') > -1)
				results[results.length] = c.childNodes[i];
			
		}
		return results;
		
	}
}