

	function slideshow_goto_prev_img(){
		
		// if loading
		if( !$(this).hasClass('loading') ){
			
			// block
			this_a	= $(this);
			$(this_a).addClass('loading');

			
			// load prev
			current_image		= get_current_img_index();
			num_images			= get_num_images();
			next_image_index	= current_image-1;
			//if( next_image_index < 0 ){ next_image_index = 0; }

			// preload next
			if( next_image_index > -1 ){
					
				
				// fadeout current
				fadeout_current();
				
				// url
				next_img_href	= $("#slideshow_preloader ul li a").eq(next_image_index).attr('href');
				
				// first?
				//if( next_image_index == (num_images - 1) ){ $(this_a).fadeOut(); }
				if( next_image_index == 0 ){ $(this_a).fadeOut(); }
				else { $(this_a).fadeIn(); }
				$("#nav_next").fadeIn();
				
				$('<img />')
			    .attr('src', next_img_href)
			    .load(function(){
			        
			        loaded_img = $(this);
			        
			        if( $("#slideshow img").size() ){
			        
						$("#slideshow img")
						.stop()
						.fadeOut( function(){ 
							
							$(this).remove();
							
							show_new_img(loaded_img , next_image_index);
							
						});
						
			        }
			        else {
			        	
			        	show_new_img(loaded_img , next_image_index);
			        	
			        }

				});	
						
			}
			

						
			
		}

		return false;
		
	}
	
	
	function get_current_img_index(){
		
		return parseInt($("#slideshow_preloader ul li a").index( $("#slideshow_preloader ul li a.active") ), 10);
		
	}
	
	
	
	function get_num_images(){
		
		return $("#slideshow_preloader ul li a").size();
		
	}

	function show_new_img( el , mark_index ){
		
   		// show
        $("#slideshow")
		.append( $(el) )
		.height('auto');
		
		$("#slideshow img")
		.hide()
		.fadeIn( function(){ unblock_click(); } );
		
		// mark active
		$("#slideshow_preloader ul li a").removeClass('active');
		$("#slideshow_preloader ul li a").eq(mark_index).addClass('active');
		
		// update desc
		$("#img_desc").html( $("#slideshow_preloader ul li a").eq(mark_index).html() );
		
		
	}
	
	
	function fadeout_current(){
	
		$("#slideshow").height( $("#slideshow img").height() );
		$("#slideshow img")
		.fadeOut(function(){ $(this).remove(); });
		
	}

	function slideshow_goto_next_img(){
		
		// if loading
		if( !$(this).hasClass('loading') ){
			
			// block
			this_a	= $(this);
			$(this_a).addClass('loading');
			

			// load next
			current_image		= get_current_img_index();
			num_images			= get_num_images();
			next_image_index	= current_image+1;		
			if( next_image_index == num_images ){ next_image_index = 0; }

	
			// preload next
			if( next_image_index ){
				
				// fadeout current
				fadeout_current();
				
				// url
				next_img_href	= $("#slideshow_preloader ul li a").eq(next_image_index).attr('href');
				
				// last?
				if( next_image_index == (num_images - 1) ){ $(this_a).fadeOut(); }
				else { $(this_a).fadeIn(); }
				$("#nav_prev").fadeIn();
				
				$('<img />')
			    .attr('src', next_img_href)
			    .load(function(){
			        
			        loaded_img = $(this);
			        
			        if( $("#slideshow img").size() ){
			        
						$("#slideshow img")
						.stop()
						.fadeOut( function(){ 
							
							$(this).remove();
							
							show_new_img(loaded_img , next_image_index);
							
						});
						
			        }
			        else {
			        	
			        	show_new_img(loaded_img , next_image_index);
			        	
			        }

				});	
						
			}
			

						
			
		}

		return false;
		
	}
	
	
	function unblock_click(){
		
        $("#nav_prev").removeClass('loading');
        $("#nav_next").removeClass('loading');
		
	}

	function init_slideshow(){
		
		// events
		$("#nav_prev").bind('click', slideshow_goto_prev_img );
		$("#nav_next").bind('click', slideshow_goto_next_img );
		
		// has more pics
		if( $("#slideshow_preloader ul li a").size() <= 1 ){
			$("#nav_prev").hide();
			$("#nav_next").hide();
		}
		
		// mark active
		if( $("#slideshow_preloader ul li a.active").size() == 0 ){
			$("#slideshow_preloader ul li a:first").addClass('active');
		}
		
		
		
		
	}
