// general actions that require ajax stuff

	/* templates */
	
	_fotos_list_html = '<ol class="list #{smallerThumbs} clearfix">';
		_fotos_list_item_html = '<li class="item thumb #{last} #{selected}">#{thumb}</li>';
	_fotos_list_html += '#{items}';
	_fotos_list_html += '</ol>';
	
	var _fotos_list_tpl = new Template(_fotos_list_html);
	var _fotos_list_item_tpl = new Template(_fotos_list_item_html);
	
	_foto_thumb_html = '<a class="image clearfix #{smaller}" href="#{url}">';
	_foto_thumb_html += '<img width="#{width}" height="#{height}" alt="" src="#{src}" title="#{title}"/>';
	_foto_thumb_html += '</a>';
	_foto_thumb_html += '#{options}';
		_foto_thumb_options_html = '<ul class="options #{smaller} clearfix">'
				_foto_thumb_options_html_comments = '<li class="point comments"><a class="_icon comments_icon" href="#{url}#comments">#{comments}</a></li>';
		_foto_thumb_options_html += '#{items}'
		_foto_thumb_options_html += '</ul>';
	
	var _thumb_tpl = new Template(_foto_thumb_html);
	var _thumb_options_tpl = new Template(_foto_thumb_options_html);
	var _thumb_options_comments_tpl = new Template(_foto_thumb_options_html_comments);	
	
		
	
	
	function _share_object (n)
	{
		n.observe('click', function(e)
		{
			//alert('sharing');
			n.up('.share').down('.networks').toggle();
			e.stop();
		});
		
	}
	
	
	// object stuff;
	var _ajax_object = new Array(3);
	
	function _get_ajax_object_type (n)
	{
		_type = false;
		
		if (n.hasClassName('ajax_post_object')){
			_type = 'day';	
		} 
		
		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:25,
					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){
										
										_html += '<a class="tag" href="/tags/' + s.tag + '">' + s.clean + '</a>';
										
										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');	
					}
				}
			);
		}
	}
	
	/* custom stuff : */
	
	Ajax.InPlaceEditor.prototype.__enterEditMode = Ajax.InPlaceEditor.prototype.enterEditMode;
	Object.extend(Ajax.InPlaceEditor.prototype, {
	  enterEditMode:function(e) {
		this.__enterEditMode(e);
		this.triggerCallback('onFormReady',this._form);
	  }
	});

	
	
	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);
						return r;
					},
					cols: 46,
					rows: 1,
					onComplete: function (transport)
					{
						data = transport.responseText.evalJSON(true);
						
						_html = data[_ajax_object['type']].title;
						
						n.update(_html);

					}
				}
			);
		}	
	}
	
	function _ajax_response_manage (n)
	{
		
		
		_ajax_object['type'] = _get_ajax_object_type(n);
		
		_total_indicator = n.up('.comments').down('.comments_total');
		
		var _response_id = n.id.sub(/\D+/, '');
		var _response_container = n.down('.story'); 
		var _response_old_copy = _response_container.innerHTML;	
		
		
		// edit
		new Ajax.InPlaceEditor(
			_response_container, 
			 _ajax_object['url'] + '/comments/' + _response_id + '/edit.json', 
			{ 
				callback: function(form, value) { return 'comment[comment]=' + encodeURIComponent(value) },
				onLeaveEditMode: function(form, value) { _response_container.hide(); },
				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:50,
				clickToEditText: '',
				onComplete: function(transport) {
					
					if (transport){
						_response_container.update('');
						if (data = transport.responseText.evalJSON(true)) {
							if (data.errors){										
								_response_container.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.unescapeHTML();
												_response_container.update(_html);
												_response_container.show();
											}
										}
									}
								);
							}
						}
					}
					
				},
				onFailure: function(transport){
					alert('Comment edit failed');
				}
			}
		);
		
		var ajax_post_moderating = false;
		// moderate
		
		ajax_post_moderating = new Ajax.InPlaceCollectionEditor
		( 
			n.down('.ajax_response_moderate'), 
			_ajax_object['url'] + '/comments/' + _response_id + '/edit.json', 
			{ 
				collection: [['1', 'live'], ['2', 'moderating'], ['3', 'spam']],
				callback: function(form, value) 
				{ 
					return 'state_id=' + encodeURIComponent(value) 
				},
				onLeaveEditMode: function(form, value) { 
					n.down('.ajax_response_moderate').hide(); 
				},
				onComplete: function(transport)
				{
					new Ajax.Request
					(
						_ajax_object['url'] + '/comments/' + _response_id + '.json', 
						{
							method: 'get',
							onComplete: function(transport){
								if (data = transport.responseText.evalJSON(true)) {
									_html = data.response.state.state;
									n.down('.ajax_response_moderate').update(_html);
									n.down('.ajax_response_moderate').show();
								}
							}
						}
					);
				}
				
			}
		);

		
		// 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);
								}
								if (_total == 0){
									_html  =  'no comments';
								} else if (_total == 1){
									_html  =  _total + ' comment';
								} else {
									_html  =  _total + ' comments';
								}
								
								
								_total_indicator.update(_html);//_html);
								
								Effect.toggle(_response_container.up('.item'), '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);
				});
				
				// story with tinymce later
				$$('.ajax_story_edit').each(function(n){
				//	_ajax_edit_story(n);
				});
				
				// response -- ajax_response_object
				$$('.ajax_response_object').each(function(n){
					_ajax_response_manage(n);
				});
				
			// foto
				// love
				$$('.ajax_post_show_love').each(function(n){
					_show_love(n);
				});
		
		
		// share
			$$('.share .ajax_share_object').each(function(n){
				
				_share_object(n);
			});
			
	}
	
	_apply_action('gen_ajax_actions');
	
	
	
	
