	var ThumbnailCounter = 0;
	function RunReady(activateThumbs,showPhotoControls){
		/* Takeout for initial debugin	*/
		if (activateThumbs !== false){
			$('#ThumbnailsWr').mouseout(function(){
					clearTimeout(ThMIn);
					ThMOut = setTimeout("hideThumbnails()",700);
				}).mouseover(function(){
					clearTimeout(ThMOut);
				});
			$('#Thumbnails').mouseover(function(){
					clearTimeout(ThMOut);
				});
			$('#ScrollBar').mouseover(function(){
					clearTimeout(ThMOut);
				});
			$('#ThumbnailsDot').mouseover(function(){
					clearTimeout(ThMOut);
					ThMIn = setTimeout("showThumbnails()",100);
				});
				
			initHoverScroller();
		}
		$('#Nav_Photos').mouseover(function(){ $('#PhotosSub').fadeIn(); });
		$('#PhotosSub').fadeOut(10).mouseout(function(){ 
			clearTimeout(TO_HideSub);
			TO_HideSub = setTimeout("HideSub()",500);
		}).mouseover(function(){ clearTimeout(TO_HideSub) });
		
		// SetThumbWidth
		ThumbWidth = 85;
		ThumbnailCounter = 0;
		$('#Thumbnails a').each(function(){ ThumbnailCounter++; });
		if (ThumbnailCounter == 0){	$('#Thumbnails .itemWr').each(function(){ ThumbnailCounter++; });	}

		NewWidth = ThumbnailCounter*ThumbWidth;
		$('#Thumbnails').css('width',NewWidth+'px');
		//alert( ThumbnailCounter +' * '+ThumbWidth+' = '+NewWidth );
		thePhotoCount = document.location.href.split('photo=');
		if ( thePhotoCount.length == 1 ){ thePhotoCount = 1 ; }else{ thePhotoCount = eval(thePhotoCount[1].split('&')[0])+1; }
		
		if (activateThumbs !== false && showPhotoControls !== false){
			setPhotoControls( thePhotoCount,  ThumbnailCounter, $('#Thumbnails a:first').attr('href') );
		}
		
		$('#PhotosSub li a').hover(
			function(){
				imgName = $('img',this).attr('src');
				//alert(imgName);
				if (imgName !== 'images/SNav_Photos.png' && imgName !== 'images/SNav_Bottom.png'){ 
					$('img',this).attr('src', imgName.split(".").join("_H.")) 
					//alert();
				}
			},
			function(){
				imgName = $('img',this).attr('src');
				if (imgName !== 'images/SNav_Photos.png' && imgName !== 'images/SNav_Bottom.png'){ 
					$('img',this).attr('src', imgName.split("_H.").join(".")) 
					//alert();
				}
			}
		);
		//initDotScrollBar();
		
	}
	
	function HideSub(){
		$('#PhotosSub').fadeOut()
	}
	
	function initHoverScroller(){
		$('#ThumbnailsWr').mousemove(function(e){

			//if (freeToMove == undefined && freeToMove != 'false' ){var freeToMove = 'true'; }
			lastLi = $('#Thumbnails').find('a:last-child');
			
			//As images are loaded ul width increases,
			//so we recalculate it each time
			//var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
			var ulWidth = lastLi.offsetLeft + lastLi.outerWidth();
		
			var x = e.pageX - $('#ThumbnailsWr').offset().left -(1024/2);
			var whichRange = Math.floor((x/8)*.1)  ; //512
			moveMe = whichRange;
			//$('#Debug').html(moveMe);
			
			if (moveMe < -1){
				moveMe= moveMe+1;
				freeToMove = 'true';
				startThumbScroll('<');
			} else if (moveMe > 0){
				freeToMove = 'true';
				startThumbScroll('>');
			} else if (moveMe == 0){
				freeToMove = 'false';
				started = false;
				//$('#Debug').html( moveMe )
				$('#ThumbnailsWr').stop();
			}
		 })
	}
	
	function startThumbScroll( dir ){
		$('#Debug').html( 'started '+started+' '+(moveMe*10)+' : '+dir )
		if (!started && thumbOpen ){
			freeToMove = 'true';
			started = true;
			MoveThumbnails(moveMe);
		}
	}
	

	function MoveThumbnails(theMove){
		//moveMe
		var thetime = 250; //8000;
		//$('#Debug').html( xt + ' ' + theMove + ' : ' + freeToMove )
			$('#ThumbnailsWr').scrollTo( {left:'+='+moveMe*10}, thetime , {axis:'x', easing:'linear', onAfter:function(){ 
				if (freeToMove == 'true'){
					MoveThumbnails(theMove); 
				}
			} });
	}
	
	function showThumbnails(){
		if (ThumbViewStatus == 'closed'){
			$('#ThumbnailsDot').hide();
			//$('#ScrollBar').show();
			ThumbViewStatus = 'open';
			$('#Debug').html( 'showThumbnails' );
			$('#ThumbnailsWr').stop(true,true).css({ /* backgroundPosition:"-8px -20px" */ }).animate(
			{ bottom:'0px' }, 700 , 'swing', function(){ thumbOpen = true; })
			clearTimeout(ThMOut);
			clearTimeout(ThMIn);
		}
	}
	
	function hideThumbnails(){
		freeToMove = 'false';
		if (ThumbViewStatus == 'open'){
			$('#ThumbnailsDot').show();
			//$('#ScrollBar').hide();
			ThumbViewStatus = 'closed';
			moveMe = 0; 
			started = false;
			$('#ThumbnailsWr').stop().animate(
			{ bottom:'-122px' }, 
			{"duration":400, complete:function(){ thumbOpen = false; } })
			clearTimeout(ThMOut);
			clearTimeout(ThMIn);
		}
	}
	
	function setPhotoControls( currentImg, totalThumbs, directLink ){
		//alert(currentImg)
		photoPos = currentImg;
		if (currentImg < 10){ currentImg = '0'+currentImg }
		
		if (!directLink){ 
			theDirectLinkCode =''; 
		}else{ 
			theDirectLinkCode =' <a id="DLink" href="'+directLink+'"><span id="LText"><img src="images/LinkToThisPhoto.gif" border="0"></span><img src="images/linksm_90ccw.png" border="0" align="absbottom"></a>'; 
		}
		$('#LinkToThisPhoto').fadeOut(400);
		$('#PhotoControls').fadeOut(400,function(){
			theTextControls = '<b>';
			
			theTextControls += '<a href="#" onclick="$(\'#'+(photoPos-1)+'.ThumbnailLink\').click(); return false;">&lt; </a>';
			
			theTextControls += currentImg;+'/'+totalThumbs;
			
			theTextControls += '<a href="#" onclick="$(\'#'+(photoPos+1)+'.ThumbnailLink\').click(); return false;"> &gt;</a>';
			
			theTextControls += '</b>';
			$(this).html(theTextControls).fadeIn();
			
			$('#DLink').hover(
			function(){
				$('#LText').fadeIn();
			},
			function(){
				$('#LText').fadeOut();
			});
			$('#LinkToThisPhoto').html( theDirectLinkCode ).fadeIn();
			//$('#LText').fadeOut(1300);
		})
	}
	
	function initDotScrollBar(){
		//AutoScroll = false;
	//	AllPhotosW = $('#Photos table').width()-1250;
		AllPhotosW = NewWidth;
		$('#bar').animate({ left:'0px', width:'1000px' }, 700 );
		$('#bar img').animate({ left:'0px', width:'1000px' }, 700 );

		$('#drag').animate({ left:'0px' }, 700)
		$('#drag').draggable(
			{
				zIndex: 	1000,
				axis: 'x',
				containment: '#bar',
				start: function() {
					//updateCounterStatus($start_counter,counts[0]);
				},
				drag: function() {
					setScrollToDrag();
				},
				stop: function() {
					//updateCounterStatus($stop_counter,counts[2]);
				}

			}
		);
	}
	
	function setDragToScroll(){
		NewDragL = ( $('#ThumbnailsWr').scrollLeft( ) / ($('#Thumbnails').width()) ) * $('#bar').width();
		$('#drag').animate( { left: NewDragL+'px' } , 1 );
	}
	
	function setScrollToDrag(){
		NewScrollL = ($('#drag').position().left / $('#bar').width()) * ($('#Thumbnails').width());
		$('#ThumbnailsWr').stop().scrollTo( {top:'0px', left: NewScrollL+'px'}, 1 , {axis:'x',  onAfter:function(){}} );
	}
	
	function endofSlideshow(){
		$('#Content img').fadeOut();
		$('#Content').css({'backgroundImage':'url(images/restartslideshow.jpg)','cursor':'pointer'});
		$('#Content').click(function(){ restartSlideshow()} );
	}
	
	function restartSlideshow(){
		$('#Content').unbind('click');
		$('#Content').css({'backgroundImage':'url(images/loading.gif)','cursor':'auto'});
		photoPos = 0;
		startSlideShow();
	}
	
	

