var ActiveScroll = false;
scrollDivImage= function(direction)
{
	clearInterval(slideShowInt);
	if (ActiveScroll == false)
	{
		ActiveScroll = true;
		// Find current item
		var current	= $$('.current');
		var prevView	= current[0];
		// Find next view based on direction.
		if (direction == 'back') {
			var nextView		= prevView.previous();	// If going back.
			var itemLength		= prevView.previousSiblings().length;
			var leftCheck		= 0;
		}
		else if(direction == 'fwd') {
			var nextView		= prevView.next();	// If going forward.
			var itemLength		= prevView.nextSiblings().length+1;
			var leftCheck		= 1;
		}
		else {
			ActiveScroll	= false;
			return false;
		}	
		
		if (nextView != null)
		{
			if( itemLength > leftCheck )
			{
				prevView.removeClassName('current');	// Remove class from current
				nextView.addClassName('current');	// Add class to next
				
				Effect.Fade(prevView, {
					duration: 0.5,
					delay: 0.3,
					afterFinish: ( function() {
						Effect.Appear(nextView, {
								duration: 0.5 });
						}
					)
				});
				
			}
			else
			{
				ActiveScroll	= false;
				return false;
			}
		}
		ActiveScroll	= false;
	}
	return false;
}

var ActiveScroll = false;
scrollDiv= function(direction)
{
	if (ActiveScroll == false)
	{
		ActiveScroll = true;
		// Find current item
		var current	= $$('.currentItem');
		var prevView	= current[0];
		// Find next view based on direction.
		if (direction == 'back') {
			//var nextView		= prevView.previous();	// If going back.
			var nextView		= prevView.previousSiblings()[4];	// If going back.
			var itemLength		= prevView.previousSiblings().length;
			var leftCheck		= 0;
		}
		else if(direction == 'fwd') {
			//var nextView		= prevView.next();	// If going forward.
			var nextView		= prevView.nextSiblings()[4];	// If going forward.
			var itemLength		= prevView.nextSiblings().length;
			var leftCheck		= 5;
		}
		else {
			ActiveScroll	= false;
			return false;
		}	
		if (nextView != null)
		{
			if( itemLength > leftCheck )
			{
				prevView.removeClassName('currentItem');	// Remove class from current
				nextView.addClassName('currentItem');	// Add class to next
				var prevViewOffset	= prevView.cumulativeOffset(); // Find current offset.
				var nextOffset	= nextView.cumulativeOffset();	// Find new offset.
				var scrolltopixle	= (nextOffset[1] - prevViewOffset[1]); // Find difference
				
				new Effect.Move($('collectioninside'),{x: 0, y: -scrolltopixle, duration: 0.35, mode:'relative', afterFinish: 
					( function() { ActiveScroll	= false; } )
				}); // Move to
			}
			else
			{
				ActiveScroll	= false;
				return false;
			}
		}
		ActiveScroll	= false;
	}
	return false;
}

changeItem= function(load, extension, type)
{
	Effect.BlindUp('viewWrap', { afterFinish: (
		function() {
			Effect.Appear('loading');
			new Ajax.Updater('viewWrap', 'loadInfo.php', {
				method:	'get',
				parameters: { id: load, ext: extension, t: type }, 
				onComplete: ( function()
				{
					Effect.BlindDown('viewWrap', {
						delay: 0.7,
						afterFinish: ( function() {
							Effect.Fade('loading');
							}
						)
					});
				}
			) });
		}
	)});
}

var playingSS = false;
var slideShowInt = false;
var allPhotos;
startPlaying= function(type) {
	if (playingSS == true) {
		$('ssControls').src = './images/play.png';
		playingSS = false;
		clearInterval(slideShowInt);
		return;
	}
	else {
		if ($('ssControls').visible() == false)
		{
			$('ssControls').show();
		}
		$('ssControls').src = './images/pause.png';
		playingSS = true;
		slideShowInt = setInterval("photoSlideShow()", 5500);
		return;
	}
}
autostartslideshow = function() {
	playingSS = true;
	slideShowInt = setInterval("photoSlideShow()", 5500);
	return;
}

var activeSlide = false;
var activeCurrent = false;
var nextItem = false;
var queue = false;
photoSlideShow = function() {
	if (playingSS == false) {
		clearInterval(slideShowInt);
		return;
	}
	if (activeSlide == false) {
		activeSlide = true;
		if (activeCurrent == false) {
			var currentD = $$('.current');
			currentD = currentD[0];
			/*var currentAll = current;*/
			activeCurrent = currentD;
		}
		var current = activeCurrent;
		nextItem	= current.next();
		if (nextItem == null) {
			allPhotos = $('allPhotoItems').descendants();;
			nextItem = allPhotos[0];
		}
		Effect.Fade(current, { afterFinish:(function() {
				/*currentAll.invoke('removeClassName', 'current');
				currentAll.invoke('hide');*/
				current.removeClassName('current');
				current = false;
				nextItem.addClassName('current');
				activeCurrent = nextItem;
				Effect.Appear(nextItem, {
					delay: 0.55,
					afterFinish:(function() {
						activeSlide = false;
						if (queue != false) {
							showPhoto(queue);
							queue = false;
						}
					})
				});
			})
		});
	}
}

showPhoto= function(id) {
	if (slideShowInt != false) {
		playingSS = false;
		clearInterval(slideShowInt);
	}
	if (activeSlide == false) {
		var next = false;
		activeSlide = true;
		if (activeCurrent == false) {
			var currentD = $$('.current');
			currentD = currentD[0];
			/*var currentAll = current;*/
			activeCurrent = currentD;
		} 
		if (id == 'fwd') {
			next = activeCurrent.nextSiblings()[0];
			if (next == null) {
				activeSlide = false;
				return false;
			}
		}
		else if (id == 'back') {
			next = activeCurrent.previousSiblings()[0];
			if (next == null) {
				activeSlide = false;
				return false;
			}
		}
		var current = activeCurrent;
		/*if ($('describe').visible() == true) {
			Effect.BlindUp(current, { afterFinish:(function() {
				$('describe').hide();
				current.removeClassName('current');
				var next = $('photo_'+id);
				next.addClassName('current');
				Effect.BlindDown('allPhotoItems', {
					delay: 0.2,
					afterFinish: ( function() {
								Effect.BlindDown(next, {
									afterFinish:(function() {
										activeSlide = false;
									})});
								activeCurrent = next;
								if (playingSS == false) {
									startPlaying();
								}
							}
						)
					});
				})
			});
		}
		else */{
			Effect.Fade(current, { afterFinish:(function() {
				current.removeClassName('current');
				if (next == false) {
					next = $('photo_'+id);
				}
				next.addClassName('current');
				Effect.Appear(next, {
					delay: 0.55,
					afterFinish: ( function() {
								if (playingSS == false) {
									startPlaying();
								}
								activeCurrent = next;
								activeSlide = false;
								return false;
							}
						)
					});
				})
			});
		}
	} else {
		queue = id;
	}
}