// slideshow actions that require ajax stuff

	/* templates */

	/*_slideshow_html = '<h1 class="_title">#{page_title}</h1>';
	_slideshow_html += '<span class="_words">#{page_words}</span>';
	_slideshow_view_html = '<div class="clearfix">#{foto}#{list}</div>';
	*/
	_slideshow_foto_view_html = '<div class="w foto_view clearfix">';
	_slideshow_foto_view_html += '		<ul class="clearfix">';
	_slideshow_foto_view_html += '			<li class="left traverse_point" style="display: none"><a href="#prev" class="_slide_show_previous">PREV</a></li>';
	_slideshow_foto_view_html += '			<li class="foto">';
	_slideshow_foto_view_html += '				<a class="image" href="#{url}" title="by &ndash; #{creator}"><img src="#{src}" width="#{width}" height="#{height}" alt="#{title}" /></a>';
	/*_slideshow_foto_view_html += '				<ul class="meta clearfix">';
	_slideshow_foto_view_html += '					<li class="point author">#{author}</li>';
	_slideshow_foto_view_html += '					<li class="point date">#{date}</li>';
	_slideshow_foto_view_html += '					<li class="point _icon comments_icon">#{comments}</li>';
	_slideshow_foto_view_html += '					<li class="point _icon love_icon">#{love}</li>';
	_slideshow_foto_view_html += '				</ul>';*/
	_slideshow_foto_view_html += '			</li>';
	_slideshow_foto_view_html += '			<li class="right traverse_point" style="display: none"><a href="#next" class="_slide_show_next">NEXT</a></li>';
	_slideshow_foto_view_html += '		</ul>';
	_slideshow_foto_view_html += '	</div>';
		
	var _slideshow_foto_tpl = new Template(_slideshow_foto_view_html);
	
	_str = '<li class="item thumb #{selected}" id="foto-index-#{index}">';
		_str += '	<a class="image clearfix smaller" href="#{foto_url}">';
		_str += '		<img height="40" width="40" alt="" src="#{foto_src}" title="#{foto_title}"/>';
		_str += '	</a>';
		_str += '</li>';
		
	var _slideshow_list_item_tpl = new Template(_str);					
		
		_str = '<div class="block fotos clearfix">';
		_str +=	'	<div class="panel traversed">';						
		_str +=	'		<ol class="list smallerThumbs clearfix">';
		_str += '			#{foto_list}';
		_str += '		</ol>';
		_str += '		<ul class="traverse clearfix" style="display: none">';
		_str += '			<li class="point prev"><a href="#">PREV</a></li>';
		_str += '			<li class="point next"><a href="#">NEXT</a></li>';
		_str += '		</ul>';
		_str += '	</div>';
		_str += '</div>';
		
	var _slideshow_list_tpl = new Template(_str);
	
		_str = '<div class="view slide_shown clearfix">';
		_str += '<h3 class="title top_foto_title"><a href="#{foto_url}">#{foto_title}</a></h3>';
		_str += '<span class="_words _creator top_foto_creator">#{creator}</span>'
		_str += '	<div class="clearfix _view_block">'
		_str +=	'		<div class="w foto_view">#{foto}</div>';
		_str +=	'		<div class="x fotos_list">#{list}</div><div class="x story" style="display: none"><p>You can use left/right arrow keys to navigate slideshow.</p></div>';
		_str += '	</div>';
		_str += '</div>';
		
	var _slideshow_tpl = new Template(_str);
	
	function _slideshow (n)
	{
		this.object = $(n);
		
		this.url = n.href;
		
		this.old_location = window.location;
		this.old_html = $('body').innerHTML;
		
		_title = ($('body').down('._title')) ? $('body').down('._title').innerHTML : document.title;//'slide show';//n.href;
		_words = ($('body').down('._title')) ? $('body').down('._words').innerHTML: '';
		
		if (!$('body').down('.slide_show_close')){
			_words += ' &ndash; slideshow: <a class="slide_show_close" href="' + this.old_location  + '">close.</a>';
		}
		
		this.heading = document.createElement('div');
		
		Element.extend(this.heading);
		
		this.heading.id = this.id = 'slideshow';//document.createElement('div');
		this.heading.addClassName('view slide_shown');
		this.heading.insert('<h1 class="_title">' + _title + '</h1>');
		this.heading.insert('<span class="_words">' + _words + '</span>');
		
		this.fotos = null;
		this.fotos_pagination = null;
		this.index = 0;
		
		// update
			
		this.update = function ()
		{
			_sso = this;
			
			this.object.up('body').addClassName('go_black go_slide_show');
			
			$('body').update();
			
			if (!$(this.id)){ // insert heading;
				$('body').update(this.heading);
			}
			
			_loading_html = '<div style="height: 20px"><p class="slide_show_loader_spin_icon" id="slide_show_laoder">Building slideshow.</p></div>';
			
			$(this.id).insert(_loading_html);
						
			var r = new Ajax.Request
			(
			
				this.url + '.json',
				{
					method: 'GET',
					onSuccess: function(transport){
						if (data = transport.responseText.evalJSON(true)){
							$('header').hide();
							_sso.process(data);	
						}
					},
					onFailure: function (){
						alert('failed to get slideshow data');	
					}
				}
			);
		};
		
		this.process = function (data)
		{	
					
			_foto_index_shift = null;
			// hide everything;
			
			// pagination;
			if (data.pagination){
				this.pagination = data.pagination;	
			}
					
			if (data.book){ 

				this.fotos = data.book.photographs;
				this.foto = data.book.photographs[this.index];
			} else if (data.dialogue){ // dialogue
				
				_foto_index_shift= 'photograph';
				this.fotos = data.dialogue;
				this.foto = data.dialogue[this.index][_foto_index_shift];
				
			} else if (data.photographs){

				this.fotos = data.photographs;
				this.foto = data.photographs[this.index];
			}
						
			_html = _slideshow_tpl.evaluate({});
						
			$(this.id).insert(_html);
			
			
			if (this.fotos) {	
				//alert(this.fotos[0]);
				
				this.foto_index = 0;
				
				this.set_list(_foto_index_shift);
				this.set_foto();
				
				_slideshow_apply_actions(this);
				
			}
			//$('header').hide();
			$('neck').scrollTo();
		}
		
		this.display_foto = function() 
		{
			_title = (this.foto.title) ? this.foto.title : '(untitled)';
			_creator = (this.foto.owner) ? this.foto.owner.alias : '(none)';
			_height = (this.foto.image_size) ? this.foto.image_size[1] : '';
			_width = (this.foto.image_size) ? this.foto.image_size[0] : '';
			
			show = {
					src: this.foto.medium.url,
					url: this.foto.url,
					title: _title.unescapeHTML(),
					creator: _creator.unescapeHTML(),
					height: _height,
					width: _width
				}
				
			_html = _slideshow_foto_tpl.evaluate(show);
			
			$(this.id).down('.top_foto_title').update('<a href="' + this.foto.url + '">' + _title.unescapeHTML() + '</a>');
			$(this.id).down('.top_foto_creator').update(_creator.unescapeHTML());
			
			$(this.id).down('.foto_view').update(_html);
			
			$(this.id).down('.foto_view').show();
			
			$(this.id).down('.slide_show_loader_spin_icon').hide();
			//$(this.id).down('.slide_show_loader_spin_icon').update('done');//();
			
			$(this.id).down('.story').show();
			
			$('neck').scrollTo();	
		}
		
		this.set_foto = function () 
		{	
			if (this.fotos[this.foto_index].cached){
				this.foto = this.fotos[this.foto_index];
				this.display_foto();
			} else {
				tso = this; 
				
				$(this.id).down('.slide_show_loader_spin_icon').show();
				$(this.id).down('.slide_show_loader_spin_icon').update('getting foto');
				
				
				
				var r = new Ajax.Request
				(
					this.fotos[this.foto_index].url + '.json',
					{
						method: 'GET',
						onSuccess: function(transport){
							if (data = transport.responseText.evalJSON(true)){
								
								tso.fotos[tso.foto_index] = tso.foto = data.photograph;
								tso.fotos[tso.foto_index].cached = true;
								tso.display_foto();
							}
						},
						onFailure: function (){
							alert('failed to get foto');	
						}
					}
				)
			}
			
		};
		
		this.set_list = function (_foto_index_shift)
		{	
			_list_items =  '';
			var foto_index = this.foto_index;
			
			//$(this.id).down('.story').show();
			
			this.fotos.each(function(foto, index)
			{
				if (_foto_index_shift){
					foto = foto[_foto_index_shift];	
				}
			
				_selected =  (index==foto_index) ? 'selected' : '' ;
				_title = (foto.title) ? foto.title : '(untitled)';
				_creator = (foto.owner) ? foto.owner.alias : '(none)';
				
				show = {
					foto_url: foto.url,
					foto_title: _title.unescapeHTML(),
					foto_src: foto.thumb.url,
					selected: _selected,
					index: index
				}
				_list_items += _slideshow_list_item_tpl.evaluate(show);
			});
			
			show = {
				foto_list: _list_items			
			}
			
			_html = _slideshow_list_tpl.evaluate(show);
			
				
				
			$(this.id).down('.fotos_list').update(_html);
			//.update(_html);
			
			if (this.pagination){
				this.set_pagination();
			}
				
		};
		
		this.set_pagination = function () 
		{
			_block = $(this.id).down('.fotos_list').down('.traverse');
			_pg = this.pagination;
			
			_block.hide();
			
			_next = _block.down('.next');
			_previous = _block.down('.prev');
			
			if (_pg.page>1){ 
				
				_prev_counter = (_pg.page>_pg.pages) ? _pg.pages : _pg.page-1;
				_prev_url = (_prev_counter>1) ? _pg.path +'/page/' + _prev_counter: _pg.path;
				
				_previous.down('a').href = _prev_url;
				_block.show();
				_previous.show();			
			} else {
				_previous.hide();
			}
			
			// prev
			if (_pg.page<_pg.pages){ 
			
				_next_url = _pg.path + '/page/' + eval(Number(_pg.page) + 1);
				_next.down('a').href = _next_url;
				_next.show();
				_block.show();
			} else {
				_next.hide();
			}	
		}
		
		this.next = function()
		{
			var _shift_index = this.foto_index;
			_shift_index = _shift_index+1;
			
			//alert(this.fotos[_shift_index] + ' < ' + _shift_index  + ' < ' + this.foto_index );
		
			if (this.fotos[_shift_index]){
	
				$('foto-index-'+this.foto_index).removeClassName('selected');
				$('foto-index-'+_shift_index).addClassName('selected');
				
				this.foto_index = _shift_index;
				
				//this.foto_index--;
				this.set_foto();
			}
		}
		
		this.prev = function()
		{
			var _shift_index = this.foto_index;
			_shift_index = _shift_index-1;
		
			if (this.fotos[_shift_index]){
	
				$('foto-index-'+this.foto_index).removeClassName('selected');
				$('foto-index-'+_shift_index).addClassName('selected');
				
				this.foto_index = _shift_index;
				
				//this.foto_index--;
				this.set_foto();
			}
		}
	}
	
	
		// actions
		function _slideshow_apply_actions (ss_object)
		{
			// traversal
			$$('.fotos_list .traverse .point a').each(function(n){
				n.observe('click', function (e){
					e.stop();
					ss_object = new _slideshow(n);
					$(ss_object.id).down('.story').hide();
					
					ss_object.update();
				});
			});
			
			Event.stopObserving(document, 'keyup');
			
			Event.observe(document, 'keyup', function(e){
				// 
				if (e.keyCode == 37){
					ss_object.prev();
				} else if (e.keyCode == 39){
					ss_object.next();
				}/* else if (e.keyCode == 13){ // enter
				
					window.location = $('bodyHTML').down('.fotos_list').down('.selected').down('a').href;//('selected');
				}*/
			});
						
			
			// traversal
			$$('.fotos_list .list .item a.image').each(function(n){
				n.observe('click', function (e)
				{
					e.stop();
					
					n.up('.list').down('.selected').removeClassName('selected');
					n.up('.item').addClassName('selected');
					
					ss_object.foto_index = new Number(n.up('.item').id.sub(/\D+/, ''));
					
					ss_object.set_foto();					
					
				});
			});
		}
			
	
	function _apply_slide_show(n)
	{
		ss_object = new _slideshow(n);
		
		n.observe('click', function(e)
		{
			e.stop();
			ss_object.update();
			
		});		
	}
		
	function slide_ajax_actions()
	{
		// slide show;
		$$('.action .slideshow_icon').each(function(n){
			//n.show();
			if (n.hasClassName('_no_change_url')==false){
				url = window.location.protocol + '//' + window.location.host + window.location.pathname;			
				n.href = url;
			}
			
			_apply_slide_show(n);
			
		});
		
	}
	
	_apply_action('slide_ajax_actions');
	
	