
	// GLOBALS
	var user_to_highlight_id;
	//createCookie('show_all_threads','false'); // do we really need this? It causes errors with Vars
	
	// on page load, set the show_all_threads cookie to false
	
	function successConfCode(response) {
		if (response.responseText.indexOf('You are confirmed') != -1) {
			$('comment_form').style.display = '';
		}
	}
	
	function flagComment(comment_id){
		if ($('report_comment_'+comment_id).className != 'report_comment_inactive') {
			$('report_comment_'+comment_id).className = 'report_comment_inactive';
			$('report_comment_'+comment_id).src = '/artwork/icon_flag_light.gif';
			
			new Req.ajax({
				url: '/ajax/comment/flag/',
				options: {
					parameters: 'comment_id='+comment_id,
					onFailure: alertTextFailure
				}
			});
		}
	}
	
	function alertTextFailure() {
			alert('your request failed. Try again or notify a developer.');
	}
	
	function toggleAllThreads(node_id, show_all_threads){
		
		// toggle visibility of links and loading animation
	
		$A(document.getElementsByClassName('expand_threads_link')).each(
			function (node) {
				dToggle(node);
			}
		);
		
		$A(document.getElementsByClassName('threads_loading')).each(
			function (node) {
				dToggle(node);
			}
		);
		
		
		createCookie('show_all_threads','true');
		
		new Req.ajax({
			url: '/ajax/comment/thread/',
			div: 'thread_container',
			options: {
				parameters: 'node_id='+node_id+'&show_all_threads='+show_all_threads,
				onComplete: function(req){
					showEditCommentLinks(user_to_highlight_id);
					if(typeof showDeleteLinks == 'function'){
						showDeleteLinks()
					}
					hideExpandThreadLinks();
					
					if(eval(readCookie('focus_user_comment'))){
					 focus_user_comment();
					}
				}
			}
		});
	}
	
	function toggleReplies(comment_div, parent_id, show_all_replies, show_reply_form, comment_id){
		
		// show the loading animation
		
		$('show_replies_link_' + parent_id).style.display='none';
		$('replies_loading_' + parent_id).style.display='inline';
		
		new Req.ajax({
			url: '/ajax/comment/single_thread/',
			div: comment_div,
			options: {
				parameters: 'parent_id=' + parent_id + '&show_all_replies=' + show_all_replies,
				onSuccess: function(req){
					showEditCommentLinks(user_to_highlight_id);
					// show delete option if available
					if(typeof showDeleteLinks == 'function'){
						showDeleteLinks();
					}
					if(show_reply_form){
						dToggle("comment_" + comment_id +  "_reply");
						document.getElementById('reply_field_' + parent_id ).focus();
					}
	
					// are we in the middle of a search?
					if(eval(readCookie('focus_user_comment'))){
					 focus_user_comment();
					}
				}
			}
		});
	}
	
	function postComment(comment_div, comment_id, node_id) {
		
		var commentButton = $('reply_button_'+comment_id);
		var defaultButtonValue = commentButton.value;
		commentButton.value = 'Please wait...';
		commentButton.disabled = true;
		
		var frm = $('frm_reply_'+comment_id);
		
		var pars = 'node_id='+frm['node_id'].value;
		pars += '&reply_to_comment_id='+frm['reply_to_comment_id'].value;
		pars += '&text='+escape(frm['text'].value);
		pars += '&highlight_user_id='+user_to_highlight_id;
		pars += '&show_all_threads=' + readCookie('show_all_threads');	
		
		// test and set if true
		
		if(typeof frm['show_all_replies'] == 'object'){
			pars += '&show_all_replies=' + frm['show_all_replies'].value;
		} 
	
		new Req.ajax({
			url: '/ajax/comment/post_comment',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('There was an error posting your comment. Please try again,');
				},
				onComplete: function(req) {
					commentButton.disabled = false;
					commentButton.value = defaultButtonValue;
					showEditCommentLinks(user_to_highlight_id);
					if(typeof showDeleteLinks == 'function'){
						showDeleteLinks();
					}
				}
			}
		});
	}
	
	function editComment(comment_div, comment_id, node_id) {
		
		var commentButton = $('edit_button_'+comment_id);
		var defaultButtonValue = commentButton.value;
		commentButton.value = 'Please wait...';
		commentButton.disabled = true;
		
		var frm = $('frm_edit_'+comment_id);
	
		var pars = 'node_id='+frm['node_id'].value;
		pars += '&comment_id='+frm['comment_id'].value;
		pars += '&reply_to_comment_id='+frm['reply_to_comment_id'].value;
		pars += '&text='+escape(frm['text'].value);
		pars += '&highlight_user_id='+user_to_highlight_id;
		pars += '&edit=true';
		pars += '&comment_div=' + comment_div;
		pars += '&show_all_threads=' + readCookie('show_all_threads');	
		
		if(typeof frm['show_all_replies'] == 'object'){
			pars += '&show_all_replies=' + frm['show_all_replies'].value;
		} 
	
		new Req.ajax({
			url: '/ajax/comment/post_comment/',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('There was an error editing your comment. Please try again,');
				},
				onComplete: function(req) {
					commentButton.disabled = false;
					commentButton.value = defaultButtonValue;
					showEditCommentLinks(user_to_highlight_id);
					if(typeof showDeleteLinks == 'function'){
						showDeleteLinks();
					}
				}
			}
		});
	}
	
	function changeActionTitle(comment_id,text) {
		$('actionText_'+comment_id).innerHTML = text;
	}
	
	function hideActionTitle(comment_id) {
		$('actionText_'+comment_id).innerHTML = '&nbsp;';
	}
	
	function toggleComments(node_id, highlight_id) {
		
		var pars = 'node_id='+node_id;
		pars += '&highlight_user_id='+user_to_highlight_id;
	
		new Req.ajax({
			url: '/ajax/comment/toggle_comments',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('There was an error toggling the comments. Please try again,');
				}
			}
		});
	}
	
	function showReplyLinks() {
		$A(document.getElementsByClassName('replyLink', 'thread_container')).each(
			function (node) {
				Element.show(node);
			}
		);
	}
	
	function hideExpandThreadLinks() {
		$A(document.getElementsByClassName('expandThread_span')).each(
				function(node){
					node.style.display='none';
				}
		);
	}
	function showEditCommentLinks(user_id) {
		$A(document.getElementsByClassName('user_edit_'+user_id, 'thread_container')).each(
			function (node) {
				Element.show(node);
			}
		);
	}
	
	function showFlaggingIcons(flagged_comment_ids) {
		$A(document.getElementsByClassName('report_comment')).each(
			function (node) {
				if (flagged_comment_ids.indexOf(node.id.substring(15)) != -1) {
					node.className = 'report_comment_inactive';
					node.src = '/artwork/icon_flag_light.gif';
				}
				node.style.display = '';
			}
		);
	}
	
	function highlightUserComments(user_id) {
		user_to_highlight_id = user_id;
		$A(document.getElementsByClassName('user_'+user_id, 'thread_container')).each(
			function (node) {
				Element.addClassName(node, 'highlight');
			}
		);
	}
	
	/* showAllComments
	 * used when an anchor is present in the url to locate the 
	 * right thread and focus the page onto that comment
	 */
	
	function search_comments(parent_id, target_comment, reply_link){
		
		var ret = true;
		var parent_comment_id = "comment_" + parent_id;
	
		if(!chkObject(target_comment)){	// target comment does not exist.
	
			if(chkObject(parent_comment_id) && !chkObject(target_comment)){
	
				if(chkObject($(reply_link))){
	
					$(reply_link).onclick();
					ret = search_comments(parent_id, target_comment, reply_link);		
	
				}else{
	
					// something is not right - time to fail
					ret = false;
				}
			
			}else if(!chkObject(parent_comment_id)){ // parent comment does not exist and parent_id != 0
	
					if(chkObject($('show_all_threads_top'))){
	
						$('show_all_threads_top').onclick();
	
					}
	
					ret = search_comments(parent_id, target_comment, reply_link);
				
			}else{
				ret = false;
			}
		}
		return ret;
	}
	
	// used during debugging
	
	function focus_user_comment(){
	 
		var comment_id = readCookie('user_comment_id');
		var parent_id = readCookie('parent_comment_id');
		var reply_link = 'actual_show_replies_link_' + parent_id;
		var search = readCookie('focus_user_comment');
		var target_comment = (parseInt(parent_id) != 0) ? "reply_" + comment_id : "comment_" + comment_id;
		
		if(search_comments(parent_id, target_comment, reply_link) && eval(search)){
			// this should only be called after the comment is found
			var anchor = 'comment_link_jump_' + comment_id;
			window.scrollTo(commentPos($(target_comment))[0], commentPos($(target_comment))[1]);
		} // else, no comment found - boo
		
		createCookie('focus_user_comment', false);
	
	}
	/* this checks for an objects existence in the working document
	 * found here: http://www.guyfromchennai.com/?p=20
	 */
	
	function chkObject(theVal) {
		if($(theVal) != null){
			return true;
		}else{
			return false;
		}	
	}
	
	function commentPos(comment_obj){
		c_lft=comment_obj.offsetLeft;
		c_top=comment_obj.offsetTop;
		while(comment_obj.offsetParent!=null){
			c_parent=comment_obj.offsetParent;
			c_lft+=c_parent.offsetLeft;
			c_top+=c_parent.offsetTop;
			comment_obj=c_parent;
		}
		return [c_lft,c_top];
	}

