			$(function(){				
				// initialize
				var current = 1;
				var next = current+1;
				var total = $("#slides").children().length;

				$("#slides li").css({'position':'absolute','top':'0'});
				$("#slides li:not(:first-child), #captions li:not(:first)").hide();

				$("#slide-nav span#current").html(current);
				$("#slide-nav span#total").html(total);

				var timer = setInterval( function() { slide( 'default',current); }, 5000 );
				var running = 1;
				
				function stopTimer(){
					clearInterval(timer);	
				};
				
				// next
				function slide(how, index){
					switch(how){
						case "thumb":
							$("#slides li:nth-child(" + current + "), #captions li:nth-child(" + current + ")").fadeOut('slow');
							current = index+1;
							$("#slides li:nth-child(" + current + "), #captions li:nth-child(" + current + ")").fadeIn('slow');
							$("#slide-nav span#current").html(current);
							//update_thumbnail_scroller();
							break;
						case "prev":
							$("#slides li:nth-child(" + current + "), #captions li:nth-child(" + current + ")").fadeOut('slow');
							if( current == 1 ){	current=total+1; } 
							previous = current-1;	
							$("#slides li:nth-child(" + previous + "), #captions li:nth-child(" + previous + ")").fadeIn('slow');
							current--;
							$("#slide-nav span#current").html(current);
							//update_thumbnail_scroller();							
							break;
						default: 
							//next
							$("#slides li:nth-child(" + current + "), #captions li:nth-child(" + current + ")").fadeOut('slow');
							if( current == total ){	current=0;	} 
							next = current+1;	
							$("#slides li:nth-child(" + next + "), #captions li:nth-child(" + next + ")").fadeIn('slow');
							current++;
							$("#slide-nav span#current").html(current);
							//update_thumbnail_scroller();
							break;
					}
				}

				// play-pause mausover
				$("#play-pause").mouseenter(function(){
					if ( running == 1 ){
						$("#play-pause").css('background-position', '-17px -10px');
					} else {
						$("#play-pause").css('background-position', '-30px -10px');
					}
				});
				// play-pause
				$("#play-pause").mouseout(function(){
					if ( running == 1 ){
						$("#play-pause").css('background-position', '-17px 0');
					} else {
						$("#play-pause").css('background-position', '-30px 0');
					}
				});

				// play-pause
				$("#play-pause").click(function(){
 					if ( running == 1 ){
						stopTimer();
						running = 0;
					} else { 
						slide();
						timer = setInterval( function() { slide( 'default',current); }, 5000 );
						running = 1;
					}
					return false;
					
				});		
					
				// next arrow click
				$("#next").click(function(){
					slide();
					stopTimer();
					running = 0;
					$("#play-pause").css('background-position', '-30px 0');
					return false;
				});
				
				// prev arrow click
				$("#prev").click(function(){
					slide('prev');
					stopTimer();
					running = 0;
					$("#play-pause").css('background-position', '-30px 0');
					return false;
				});

				
				// arrow keys work for next/prev too... also make spacebar play/pause
				$(document).keydown(function(event){
					if (event.keyCode==39){ //right arrow
						$("#next").css('background-position', '-8px -10px');
					} if (event.keyCode==37){ //left arrow
						$("#prev").css('background-position', '0 -10px');
					} if (event.keyCode==32 && running == 1){
						$("#play-pause").css('background-position', '-17px -10px');
					} else if (event.keyCode==32 && running == 0){
						$("#play-pause").css('background-position', '-30px -10px');
					} 
				});
				$(document).keyup(function(event){
					if (event.keyCode==39){ //right arrow
						slide(); stopTimer(); running = 0;$("#play-pause").css('background-position', '-30px 0'); $("#next").css('background-position', '-8px 0');return false;
					} if (event.keyCode==37){ //left arrow
						slide('prev'); stopTimer(); running = 0;$("#play-pause").css('background-position', '-30px 0');	$("#prev").css('background-position', '0 0'); return false;
					} if (event.keyCode==32 && running == 1){
						stopTimer();
						running = 0;
						$("#play-pause").css('background-position', '-30px 0');
					} else if (event.keyCode==32 && running == 0){
						slide();
						timer = setInterval( function() { slide( 'default',current); }, 5000 );
						running = 1; 
						$("#play-pause").css('background-position', '-17px 0');
					} 
				});
				
				
				// thumbnails slide out
				$('#thumbnails').mouseenter(function() {  
    				$("#thumbnails li").animate({ "left": 0 }, "slow"); 
				});
				$('#thumbnails').mouseleave(function() {  
    				$("#thumbnails li").animate({ "left": -110 }, "slow");
				});
				
				
				// tn mousemove function that scrolls the thumbnails based on where the mouse is in relation to the #thumbnails box
				var mousewheel_used = 0;
				
				    $("#thumbnails").mousemove(function(e){
						if (mousewheel_used == 0){
							var scroll_max = this.scrollHeight - $('#thumbnails').height();
							var scroll_percentage = this.scrollTop/scroll_max;

							var tn_offset = $("#thumbnails").offset();
							var maus_pos_percent = (e.pageY-tn_offset.top)/$("#thumbnails").height() ;
							if (maus_pos_percent >.6){
								this.scrollTop += 35;
				 				$('#scrollbar-slider').css({ top: (scroll_percentage*($('#thumbnails').height()-$('#scrollbar-slider').height())) });
							}else if (maus_pos_percent <.4) {
								this.scrollTop -= 35;
				 				$('#scrollbar-slider').css({ top: (scroll_percentage*($('#thumbnails').height()-$('#scrollbar-slider').height())) });
							}							
						}
				    });

				
				// mousewheel scrolling for thumbnails
				$('#thumbnails').mousewheel(function(event, delta){
				  	// set a flag to cancel mousemove action if mousewheel is used..
					mousewheel_used = 1;
					
					// scroll_max is the height of the content - height of visible area of #thumbnails
					var scroll_max = this.scrollHeight - $('#thumbnails').height();
					
					// scroll the thumbnails
					this.scrollTop -= (delta*5);
					// thumb scrollTop max = scroll_max, min = 0 so current_position/scrollmax is a percentage of how far down it is.
					var scroll_percentage = this.scrollTop/scroll_max;
					// move the slider to correspond with the percentage that the thumbnails have moved.
				 	$('#scrollbar-slider').css({ top: (scroll_percentage*($('#thumbnails').height()-$('#scrollbar-slider').height())) });

					return false;
				});		
				
				// thumbnail click handler
				$('#thumbnails li').click(function(){
					slide("thumb",$(this).index());
					stopTimer();
					running = 0;
					$("#play-pause").css('background-position', '-30px 0');
				});
				
				// scroller updater
				function update_thumbnail_scroller(){
					var cur_percent; 
					if (current==1){cur_percent=0;}else{ cur_percent=(current-1)/(total-1)};
					$('#thumbnails').scrollTop(cur_percent*total*$('#thumbnails li').outerHeight());
					$('#scrollbar-slider').css({ top: (cur_percent*490 ) });	

				};
			
				
			});

