function initiate_commenter() {
	if ($$('#cmntz li')[0]) bgcol = $$('#cmntz li')[0].getStyle('background-color').parseColor('#ffffff');
	//else bgcol = "#001F00";
	if ( $('add_comment_form') ) $('add_comment_form').observe('submit', add_comment);
	$$('.del').invoke('observe','click', remove_comment);
	$$('.edtz').invoke('observe','click', get_comment);
	
	if ( $('unsubscr') ) {
		$('unsubscr').observe('click', function(e){
			e.stop();
			new Ajax.Request(base_url+'comments/unsubscribe',{
				postBody: 'type='+$F('comment_type')+'&eid='+$F('eid'),
				onCreate: function() { 	$('unsubscr_response').update('<span class="loading"></span>');	},
				onSuccess: function() { $('unsubscr_response').update('You have unsubscribed from this thread.'); }
			});
		});
	}
	
	$$('#cmntz li.cmnt a.c_up')
		.invoke('writeAttribute','title','Vote up this comment')
		.invoke('observe','click',function(e){cvote(e,1);});
	$$('#cmntz li.cmnt a.c_dn')
		.invoke('writeAttribute','title','Vote down this comment')
		.invoke('observe','click',function(e){cvote(e,-1);});
	$$('#cmntz li.cmnt a.c_na')
		.invoke('observe','click',function(e){cvote(e,0);})
		.each(function(e){
			e.up('div').down('.userz').setStyle({'margin':'0px'});
		});

	$$('#cmntz li.cmnt a, #cmntz a.c_dn, #cmntz a.c_na')
		.invoke('writeAttribute','onmouseover','this.previous(\'p.c_vtz\').morph(\'opacity:0.8\',{duration:0.2});')
		.invoke('writeAttribute','onmouseout','this.previous(\'p.c_vtz\').morph(\'opacity:0.1\',{duration:0.2});');

	var is_photos = ( $('sidebar') ) ? false : true;
	var widt = (is_photos) ? window.innerWidth + 325 : window.innerWidth;
	$$('.headline').invoke('setStyle',{width:widt-433+'px'});	//same as down, but could be done better by perhaps checking the size of the text below
	widt -= 709;												//this is too much ONLY if there's no scrollbar
	$$('.cmnt div:first-child').each(function(e){
		  e.down('span').setStyle({width: widt - e.getStyle('margin-left').slice(0,-2) + 'px'}); //each span gets width = widt - threading indent
	});
	spanwidt = widt; //save this here for add_comment
	
	widt += ( $('captcha') ) ? 0: 252; //bigger field for login mode
	if ( $('edit') ) {
		$('edit_txt').setStyle({width:widt+'px'});
		$('edit').hide();
	}
	if ( $('txt') ) $('txt').setStyle({width:widt+'px'});
}


function cvote(e,value) {
	e.stop();
	var control = {0:'a.c_na',1:'a.c_up'};
	control[-1] = 'a.c_dn';
	var owner = e.element().up('li').down('p.userz').down('a').readAttribute('id').split('_u_').last();
	var cid = e.element().up('li').readAttribute('id').substr(2);	//i.e can ditch href tags in voters if i dont care about non-js	
	new Ajax.Request(base_url+'comments/vote',{
		postBody: 'value='+value+'&cid='+cid+'&owner='+owner,
		onCreate: function() {
			var oldvote = (ele = $('c_'+cid).down('a.voted')) ? {'c_up':1,'c_dn':-1,'c_na':0}[ ele.removeClassName('voted').classNames() ] : 0;
			$('c_'+cid).down(control[value]).addClassName('voted')
					   .previous('p.c_vtz').update( 	parseInt($('c_'+cid).down('p.c_vtz').innerHTML) - oldvote + value	);
		}//,
		//onSuccess: function(r) {$('heading').update(r.responseText);}
	});
}

function done_editing() {
	$('edit').hide();
	$('post').show();
}

function get_comment(evt) {
	evt.stop();
	$('cid').value = evt.findElement('li').readAttribute('id').substr(2);
	new Ajax.Request(base_url+'comments/get', {
		postBody: 'cid='+$F('cid')+'&type='+$F('comment_type'),
		onCreate:function() {
			$('edit').show().down('input').hide(); 
			$('post').hide();
			$('message').update('<span class="loadingi"></span>').show();
		},
		onSuccess: function(res) { 
			d=res.responseText;
			switch (d.substring(0,5)) {
				case 'fail:':
					$('edit_txt').hide();
					$('message').update('<p class="red">Access denied.</p>')
					break;
				case 'error':
					$('edit_txt').hide();
					$('message').update('<p class="red">Comments are only editable for an hour.</p><br>');
					break;
				default:
					$('edit_txt').show().highlight({duration:0.5,startcolor:'#707000'}).value = d.substring(5);
					Effect.ScrollTo('edit');	
					$('message').hide().next('input').show();
					break;
			}
		}
	});
}

function save_edit(elem) {
	var cid = $F('cid');	
	new Ajax.Request(base_url+'comments/edit',{
		postBody: elem.up('form').serialize()+'&type='+$F('comment_type'),
		onCreate: function() { 
			$('c_'+cid).down('span').update('<span class="loadingi"></span>');
		},
		onSuccess: function(res) {
			d=res.responseText;
			$('c_'+cid).down('span').update(d).up('li').highlight({startcolor:'#707000',endcolor:bgcol});
		}
	});
}

function add_comment(e) {
	e.stop(); 
	var k = Math.floor(Math.random()*10000000);  //random id for new div
	var here = ( !$F('parentid') ) ? $('new') : $('c_'+$F('parentid')).down('.new_here');
	here.insert('<div id="c_'+k+'"></div>');
	
	new Ajax.Request(base_url+'comments/post/', {
		postBody: $('add_comment_form').serialize(),
		onCreate: function() {
			$('c_'+k).update('<span class="loadingi"></span>');
			$('error','subbtn').invoke('hide');
		}, 
		onSuccess: function(res) {
			var d=res.responseText;
			$('subbtn').show();
			switch (d.substring(0,5)) {
				case 'error':
				$('error').update(d.substr(5)).appear({duration:0.3});
				$('c_'+k).update('');
				break;
			default:
				var edt,del;
				$('c_'+k).update(d.substr(5))
						 .down('span').setStyle({width: spanwidt - $('c_'+k).down('div').getStyle('margin-left').slice(0,-2) + 'px'})
						 .up('li').highlight({startcolor:'#707000',endcolor:bgcol});
				if ( edt = $('c_'+k).down('.edtz') ) edt.observe('click',get_comment);
				if ( del = $('c_'+k).down('.del') ) del.observe('click', remove_comment);
				if ( $('c_'+k).up('.new_here') ) $('c_'+k).addClassName('first');	//does not conflict with below addClassName
				if ( $('nocomments').visible() ) {
					$('nocomments').blindUp({duration: 0.4});
					$('c_'+k).addClassName('first');
				}
				break;
			}
		}
	});
}

function remove_comment(e) {
	e.stop();
	var cid = e.findElement('li').readAttribute('id').substr(2);
	var type = $F('comment_type');
	new Ajax.Updater ('allcomments', base_url+'comments/delete', { postBody:'id='+cid+'&eid='+$F('eid')+'&type='+type,
		onCreate: function() {
			$('c_'+cid).highlight({startcolor: '#700000', endcolor: bgcol, duration: 0.3}).blindUp({duration:0.3});
			done_editing();
		},
		onSuccess: function() {
			var fn = function() { if ( !$$('.cmnt').invoke('visible').any() ) $('nocomments').blindDown({duration:0.7}); };
			fn.delay(0.4);
		}
	});
}

function reply_to(id,author,uid,depth){ //make form move inside
	$('parentid').value=id;
	$('depth').value=depth;
	$('rep').innerHTML='Reply to '+author;
	$('comment_author').value=uid;
	Effect.ScrollTo('rep');
}
