function toggle_item(string, field) {
}

CommentForm = {
	_container: function() { return $('comment_form_container'); },
	ajaxify: function() {
		Event.addBehavior(this.behaviors);
	},
	behaviors: {
		'#comment_form': Remote.Form({
			onCreate: function() {
				CommentForm.loading();
			},
			onComplete: function() {
				Event.addBehavior({
					'#comment_tools a.edit:click': function() {
						CommentForm.toggle();
						return false;
					},
					'#comment_tools.edit a.cancel:click': function() {
						alert('yo');
					},
					'#comment_tools.new a.cancel:click': function() {
						CommentForm.reset(); 
						window.location = '#post_comment';
						return false;
					},
					'#comment_tools a.save:click': function() {
						new Ajax.Request($('comment_form').action+'?save=1', {
							asynchronous:true, 
							evalScripts:true, 
							method:'post', 
							parameters:Form.serialize('comment_form')
						}); 
						return false;
					}
			}); }
		})
	},
	loading: function() { 
		$('comment_preview_area').hide();
		$$('.loading').each( function(e) { e.show(); });
	},
	slideUp: function() {
		return new Effect.Fade('comment_form_container', {duration: 0.3, queue: {position: 'end', scope: 'form'} });
	},
	reveal_comment: function(comment_id) {
		new Effect.Appear(comment_id, {
			afterFinish: function(effect) {
				new Effect.ScrollTo(effect.element.id, {offset: -50});
				new Effect.Highlight(effect.element.id);
				CommentForm.reset();
			},
			queue: {position: 'end', scope: 'comment'} 
		});
	},
	reveal_preview: function() {
		preview_height = $('comment_preview_area').getHeight();
    form_height = $('comment_form_container').getHeight();
    target_height = form_height > preview_height ? form_height : preview_height;
		$('new_comment_area').setStyle({'height': target_height+'px'});
		// new Effect.Morph('new_comment_area', {style: 'height: '+target_height+'px', queue: 'end'});
	  this.slideUp();
		new Effect.ScrollTo('new_comment_area', {offset: -50});
		new Effect.Fade('loading_preview', {
			afterFinish: function() { 
				CommentForm.done_loading(); 
			},
			delay: 0.2,
			duration: 0.6,
			queue: { position: 'end', scope: 'preview' } 
		});
		new Effect.Appear('comment_preview_area', {duration: 0.3, queue: {position: 'end', scope: 'preview' } } );
		// this.done_loading();
		// new Effect.Appear('comment_preview_area', {duration: 1.2, queue: 'end'});
		// console.log('height, scroll up + loading');
	},
	invalid_comment: function() {
		this.done_loading();
		first_error_field = $$('form li.errors').first();
		if (first_error_field) {
			first_error_input = first_error_field.down('input');
			first_error_text  = first_error_field.down('textarea');
			if (first_error_input) first_error_input.focus();
			else if (first_error_text) first_error_text.focus();
		}
		$('comment_preview_area').show();
	},
	done_loading: function() { $$('.loading').each( function(e) { e.hide(); } ) },
	show_container: function(show_options) { 
		new Effect.Appear(this._container(), show_options+{duration: 0.2}); 
	},
	clear_errors: function() {
    error_fields = ['name', 'email', 'url', 'comment'];
		error_fields.each( function(f) {
			$('comment_'+f).up('li').removeClassName('errors');
		});
	},
	reset: function(show_options) {
		if ($('remember_me_check').checked) {
			$('comment_comment').value = '';
		} else {
			document.forms.comment_form.reset();
		}
		$('comment_preview_area').update('');
		this.show_container(show_options);
    this.clear_errors();
	},
	toggle: function() {
		this.clear_errors();
		this.show_container(); 
		$('comment_tools').hide(); 
		window.location = '#preview'; 
		$('comment_comment').focus(); 
	}
}

// Making our own behavior, so we can load it later, without double-loading anything else
CommentEditorLink = Behavior.create({
  initialize: function() {
    
  },
  onclick: function() {
    $$('.comment').each(function(e) { e.setOpacity(100.0); });
		$('new_comment_area').setStyle({height: $('comment_form_container').getHeight() + 'px'});
		new Effect.Fade('comment_form_container', {
			duration: 0.2, 
			afterFinish: function() {
				new Ajax.Request(this.element.href, {
					method : 'get',
					onCreate: function() {
						$('comment_form_container').remove(); 
						$('comment_preview_area').remove(); 
					},
					onComplete: function() {
						CommentForm.ajaxify();
					}
				});
			}.bind(this)
		});
		return false;
  }
});

CommentDestroyLink = Behavior.create({
	initialize: function() {},
	onclick: function() {
		if (confirm('Delete this comment? Are you sure?')) {
			new Ajax.Request(this.element.href, {
				method: 'delete'
			})
		}
		return false
	}
})

edit_comments = function(selector) {
	// var edit_selector = (selector ? selector+' ' : '') + '.edit.private'
	// var del_selector  = (selector ? selector+' ' : '') + '.delete.private'
	var edit_selector = '.edit.private';
	var del_selector  = '.delete.private';
	// console.log(edit_selector + ' / ' + del_selector);
	$$(edit_selector).each( function(e) {
		CommentEditorLink.attach(e);
	})
	$$(del_selector).each( function(e) {
		CommentDestroyLink.attach(e);
	})
}
edit_comments();
CommentForm.ajaxify();

TagSelect = Behavior.create({
	initialize: function(target_field) {
		this.field = target_field;
		this.contents = this.element.getAttribute('rel');
		this._setup();
	},
	_setup: function() {
		this._get_tags();
		// console.log(this.contents + ' => ' + this.current_tags.include(this.contents));
		if (this.current_tags.include(this.contents)) {
			this.element.addClassName('selected');
		} else if (this.element.hasClassName('selected')) {
			this.element.removeClassName('selected');
		}
	},
	_get_tags: function() {
		this.current_tags = $F(this.field).split(/[,;]\s*/).without('');
	},
	_taggify: function() {
		this._get_tags();
		if (this.current_tags.any()) {
			if (this.current_tags.include(this.contents)) {
				this.current_tags = this.current_tags.without(this.contents);
				this.element.removeClassName('selected');
			} else {
				this.current_tags = this.current_tags.concat(this.contents);
				this.element.addClassName('selected');
			}
			$(this.field).value = this.current_tags.join(', ');
		} else {
			this.element.addClassName('selected');
			$(this.field).value = this.contents;
		}
		new Effect.Highlight(this.field, {duration: 0.3});
	},
	onclick: function() {
		this._taggify();
		return false;
	}
})

// console.log('loaded');
Event.addBehavior({
	'body#posts': function() {
		try {
			url = $('breadcrumbs').down('p').select('a').last().href;
		} catch(e) {
			url = false;
		}
		if (url)
			new Ajax.Request(url+'/decorate', {method: 'get'});
	},
	'body#posts .post_form #post_tag_cloud a': TagSelect('post_tag_list'),
	'body#posts .post_form #post_tag_list:keyup': function() {
		TagSelect.instances.each(function(t) {t._setup()});
	}
})