// 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 _show_love(n)
	{
		var _love_making = false;
		
		n.observe('click', function(e)
		{
			e.stop();
			//alert(_love_making);
			
			//var _love_icon = n.down('img').src;
			
			if (_love_making == false){
				_love_making = true;
				
				//n.down('img').hide();
				n.removeClassName('icon');
				n.removeClassName('love_icon_off');
				n.removeClassName('love_icon_on');
				n.addClassName('spin');
				
				
				new Ajax.Request
				(
					n.href + '.json', 
					{
						method: 'post',
						onSuccess: function (transport){
							if (data = transport.responseText.evalJSON(true)) {
								
								if (data.post.loving){
									total = _append_zero(data.post.loving.total);
									n.update(total);
								}
							}
							
							_loveMaking = false;
						},
						
						onFailure: function(){
							alert('Sorry, love making failed.');
						},
						
						onComplete: function(transport){
							
							n.addClassName('icon');
							
							data = transport.responseText.evalJSON(true);
							if (data.post.loving.total){
								n.addClassName('love_icon_on');
							} else {
								n.addClassName('love_icon_off');
							}
									
							n.removeClassName('spin');
							_love_making = false;
						
						}
					}
				);
				
				
			} else {
				alert('Sorry, one lover at a time.');	
			}
		});
	}
	
	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 = 'post';	
		} else if (n.hasClassName('ajax_magazine_object')){
			_type = 'magazine';
		} 	
		
		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:50,
					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="/blog/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');	
					}
				}
			);
		}
	}
	
	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: 58,
					onComplete: function (transport){
						if (transport){
							n.hide();
							if (data = transport.responseText.evalJSON(true)) {
								
								_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('.top').down('.comments_icon');
		
		
		var _response_id = n.id.sub(/\D+/, '');
		var _response_container = n.down('.story'); //$('ajax_comment_container_'+_response_id);
		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) },
				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);	
											}
										}
									}
								);
							}
						}
					}
					
				},
				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  ='<a href="' + _ajax_object['url'] + '#comments">' + _total + '</a> Comment(s)';
								_total_indicator.update(_total);//_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);
				});
				
				// 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');
	
	
	
	
