// general actions that require ajax stuff

	 
	// object stuff;
	var _ajax_object = new Array(3);
	
	function _get_ajax_object_type (n)
	{
		_type = false;
		
		if (n.hasClassName('ajax_post_object')){
			_type = 'post';	
		} else if (n.hasClassName('ajax_exhibition_object')){
			_type = 'exhibition';
		}	
		
		return _type;
	}
		
	function _ajax_edit_tags (n)
	{
		_ajax_object['type'] = _get_ajax_object_type(n);
		if (_ajax_object['type'])
		{
			new Ajax.InPlaceEditor (
				$('ajax_tags_editor'), 
				 _ajax_object['url'] + '/edit.json', 
				{ 
					
					callback: function(form, value) { return 'tagged=' + encodeURIComponent(value) },
					externalControlOnly: true,
					externalControl: n,
					rows:4,
					cols:17,
					loadTextURL: _ajax_object['url'] + '.text?get='+_ajax_object['type']+'/tagged',
					
					onComplete: function (transport){	
						if (transport){
							$('ajax_tags_editor').hide();
							if (data = transport.responseText.evalJSON(true)) {
								_html = '';
								if (data[_ajax_object['type']].tags){
									//alert(data[type].tags
									data[_ajax_object['type']].tags.each(function(s, index){
										switch(_ajax_object['type']){
											case 'photograph':
													_html += '<a href="' + data.photograph.space.url + '/tag/' + s.tag + '">' + s.clean + '</a>';
												break;
											case 'note':
													_html += '<a href="/notes/tag/' + s.tag + '">' + s.clean + '</a>';
												break;
											case 'post':
													_html += '<a href="/journal/tag/' + s.tag + '">' + s.clean + '</a>';
												break;
										};
										
										if (index+1<data[_ajax_object['type']].tags.length){
											_html += ', ';
										}
									});
								}
								
								$('ajax_tags_editor').update(_html);
								$('ajax_tags_editor').show()
							}
						}
					},
					onFailure: function(transport){
						alert('Sorry, we failed to process that request');	
					}
				}
			);
		}
	}
	
	function _ajax_edit_title (n)
	{
		_ajax_object['type'] = _get_ajax_object_type(n);
		
		if (_ajax_object['type'])
		{
			 new Ajax.InPlaceEditor 
			 (
				 n, 
				 _ajax_object['url'] + '/edit.json', 
				{ 
					callback: function(form, value) { 
						r = 'title=' + encodeURIComponent(value);
						if (_ajax_object['type']=='post'){
							r = 'article[title]=' + encodeURIComponent(value)	
						}
						return r;
					},
					cols: 52,
					onComplete: function (transport){
						if (transport){
							n.hide();
							if (data = transport.responseText.evalJSON(true)) {
								
								if (_ajax_object['type']=='post'){
									_html = data[_ajax_object['type']].article.title;
								} else {
									_html = data[_ajax_object['type']].title;
								}
								
								n.update(_html);
								n.show();
							}
						}
					}
				}
			);
		}	
	}
	
	function _ajax_response_manage (n)
	{
		
		
		_ajax_object['type'] = _get_ajax_object_type(n);
		
		_total_indicator = n.up('.comments').down('.total');
				
		var _response_id = n.id.sub(/\D+/, '');
		var _response_container = $('response-'+_response_id);
		
		// alert(_response_container);
		 
		var _response_old_copy = _response_container.down('.story').innerHTML;	
		
		//alert('hello');
		// edit
		new Ajax.InPlaceEditor(
			_response_container.down('.story'), 
			 _ajax_object['url'] + '/comments/' + _response_id + '/edit.json', 
			{ 
				callback: function(form, value) { return 'comment[comment]=' + encodeURIComponent(value) },
				externalControlOnly: true,
				externalControl: n.down('a.ajax_response_edit'),
				loadTextURL: _ajax_object['url'] + '/comments/' + _response_id + '/edit.text?get=response/comment/comment',
				rows:8,
				cols:48,
				clickToEditText: '',
				onComplete: function(transport) {
					if (transport){
						_response_container.down('.story').update('');
						if (data = transport.responseText.evalJSON(true)) {
							if (data.errors){										
								_response_container.down('.story').update(_response_old_copy);
								alert('Comment edit failed');
							} else {
								new Ajax.Request
								(
									_ajax_object['url'] + '/comments/' + _response_id + '.json', 
									{
										method: 'get',
										onComplete: function(transport){
											if (data = transport.responseText.evalJSON(true)) {
												_html = data.response.comment.comment;
												//@TODO a better way is required
												_response_container.down('.story').update(_html);	
											}
										}
									}
								);
							}
						}
					}
					
				},
				onFailure: function(transport){
					alert('Comment edit failed');
				}
			}
		);
		
		// delete
		n.down('.ajax_response_delete').observe('click', function(e)
		{
			
			
			if (confirm('Are you sure you want to delete this comment')){
				new Ajax.Request
				(
					_ajax_object['url'] + '/comments/' + _response_id  + '/delete.json', 
					{
						method: 'post',
						parameters: {'response_id' : _response_id},
						onComplete: function(transport){
							if (data = transport.responseText.evalJSON(true)) {
								_total = 0;
															
								if (data[_ajax_object['type']].comments){
									_total = _append_zero(data[_ajax_object['type']].comments.length);
								}
								_html  = _total;
								_total_indicator.update(_html);
								
								
								Effect.toggle($('response-' + _response_id), 'appear');
							}
						},
						onFailure: function(transport){
							alert('Comment delete failed');
						}
					}
				);
			}
			
			e.stop();
		});
	}
	
	
	
	
	
	
	function gen_ajax_actions()
	{
		
			
		// generic objects;
			// all 
			
				// title ajax_title_edit ajax_photograph_object
				$$('.ajax_title_edit').each(function(n){
					_ajax_edit_title(n);
				});
				
				// tags ajax_tags_edit ajax_photograph_object
				$$('.ajax_tags_edit').each(function(n){
					_ajax_edit_tags(n);
				});
				
				// response -- ajax_response_object
				$$('.ajax_response_object').each(function(n){
					_ajax_response_manage(n);
				});
				
		
		
	}
	
	_apply_action('gen_ajax_actions');
	
	
	
	
