slideInterval = 7500;
scrollAmount = 400;
scrollPad = 25;
isMoving=false;



// utilities
function getActiveSlide() {
	var $active = $('#ImageContainer>div.slide.active');
	return $active.length?$active: $('#ImageContainer>div.slide').eq(0);	
}
function getNextSlide() {
	var $active = $('#ImageContainer>div.slide.active');
	return $active.next().length?$active.next(): $('#ImageContainer>div.slide').first();
}
function getPrevSlide() {
	var $active = $('#ImageContainer>div.slide.active');
	return $active.prev().length?$active.prev(): $('#ImageContainer>div.slide').last();
}
function slideStart()
{
	
	if ($('#ImageContainer').hasClass('autoPlay'))
	{
		return setInterval("autoSlide()", slideInterval);
	} else {return false;}
	
}
function slideStop(slideshowID)
{
	clearInterval(slideshowID);
}
// setup
function setInitialSlide()
{
	$('#ImageContainer>div.slide').add('#captionContainer>div.slide').add('#quoteWrapper>div.quoteContainer').hide();
	
	var activeButton = $('#controlContainer a.active');
	var active = $('#ImageContainer>div.slide.active');
	var activeCaption = $('#captionContainer>div.slide.active');
	var activeQuote = $('#quoteWrapper>div.quoteContainer.active');
	
	if (activeButton.length == 0)
	{
		$('#controlContainer a:first').addClass('active');
	}
	if (active.length == 0)
	{
		$('#ImageContainer>div.slide:first').addClass('active').show();
	}
	if (activeCaption.length == 0)
	{
		$('#captionContainer>div.slide:first').addClass('active').show();
	}
	if (activeQuote.length == 0)
	{
		$('#quoteWrapper>div.quoteContainer:first').addClass('active').show();
	}
	updateControls();
}
// animated slide
function autoSlide() {

	var $activeButton = $('#controlContainer a.active');
	var $nextButton = $activeButton.next().length? $activeButton.next(): $('#controlContainer a:first');

	var $active = $('#ImageContainer>div.slide.active');
	var $next = $active.next().length ? $active.next(): $('#ImageContainer>div.slide:first');

	var $activeCaption = $('#captionContainer>div.slide.active');
	var $nextCaption = $activeCaption.next().length ? $activeCaption.next(): $('#captionContainer>div.slide:first');
	var $activeQuote = $('#quoteWrapper>div.quoteContainer.active');
	var $nextQuote = $activeQuote.next().length ? $activeQuote.next(): $('#quoteWrapper>div.quoteContainer:first');
	
	$active.add($activeCaption).add($activeQuote).addClass('last-active');
	$next.add($nextCaption).add($nextQuote).show();

	
	
	// $active.add($activeCaption).addClass('last-active').fadeOut(1000);
	$next.add($nextCaption).add($nextQuote).css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function()
		{
			$active.add($activeCaption).add($activeQuote).removeClass('active last-active');
			readMoreSetup()
		}
	);
	updateControls();

}
// forced or jumped slide
function slideJump(jumpSlide, slideshowID) {
	
	var $active = $('#ImageContainer .slide.active');
	var $jumpTo = $('#ImageContainer .slide').eq(jumpSlide);
	
	
	var $activeCaption = $('#captionContainer>div.slide.active');
	var $jumpCaption = $('#captionContainer>div.slide').eq(jumpSlide);
	
	var $activeQuote = $('#quoteWrapper>div.quoteContainer.active');
	var $jumpQuote = $('#quoteWrapper>div.quoteContainer').eq(jumpSlide);
	
	$active.add($activeCaption).add($activeQuote).addClass('last-active').hide();
	$jumpTo.add($jumpCaption).add($jumpQuote).addClass('active').show();
	$active.add($activeCaption).add($activeQuote).removeClass('active last-active');
	readMoreSetup();
	updateControls();

}
function updateControls()
{
	var $activeButton = $('#controlContainer a').eq(getActiveSlide().index());
	var $prevButton = $activeButton.prev();
	var $nextButton = $activeButton.next();
	// check if the next / prev buttons should stay active
	if ($nextButton.length == 0)
	{ 
		$('.slideControl.rightControl').addClass('disabled');
	} else {
		$('.slideControl.rightControl').removeClass('disabled'); 
	}
	if ($prevButton.length == 0)
	{ 
		$('.slideControl.leftControl').addClass('disabled');
	} else {
			$('.slideControl.leftControl').removeClass('disabled') ;
	}
	
	$('#controlContainer a.active').removeClass('active');
	$activeButton.addClass('active');
	
}
function readMoreSetup()
{
	$activeCaption = $('#captionContainer>div.slide.active>div.caption')
		
		$Offset = $activeCaption.position();
	// show the controls	
	if ($activeCaption.height()>scrollAmount)
		
		{
			$('#scrollDown').removeClass('disabled').show();
			$('#scrollUp').removeClass('disabled').show();
			// manage active / inactive buttons
			if ($Offset.top==0)
			{
				$('#scrollUp').addClass('disabled');
			}
			if((Math.abs($Offset.top)+scrollAmount>= $activeCaption.height()))
			{
				$('#scrollDown').addClass('disabled')
			}
			
		} else {
			$('#scrollDown').hide();
			$('#scrollUp').hide();
			
		}
	
	
}

function stopMoving(){isMoving=false;}

$(function()
	{
		var slideshowID = slideStart();
		if ($('#ImageContainer').hasClass('autoPlay'))
		{	
			$('#showControl').show();
		}
		setInitialSlide();
		readMoreSetup()
		
		// these could be set in some slidecontrols object
		$('.jump').click(
			function() {
				if ($(this).hasClass('active')){return false;}
				slideStop(slideshowID);
				$('#controlContainer a.active').removeClass('active');
				$(this).addClass('active');
				slideJump($(this).index());
				slideshowID = slideStart();
				return false;
			}
		);
		$('.slideControl').click(
				function(){
				if (!($(this).hasClass('disabled')))
				{
					// default to going right
					var direction = 1; 
					// unless it's left
					if ($(this).hasClass('leftControl'))
						{
							// get the current slide and subtract 1
							direction = -1;
						}
					var activeIndex = $('#ImageContainer .slide.active').index();
					var newIndex = activeIndex + direction;
					slideStop(slideshowID);
					slideJump(newIndex, slideshowID);
				}
				});
		
		$('#showControl').click(
				function() {
					if ($(this).hasClass('pause'))
					{
						slideStop(slideshowID);
						$(this).addClass('play').removeClass('pause').attr('title', 'Resume Slideshow');
					} else {
						slideshowID = slideStart();
						$(this).addClass('pause').removeClass('play').attr('title', 'Pause Slideshow');
					}

				});
		
		// these need to be put into a scrollbar-type object, that extends slideshow
		$('#scrollDown').click(function(){
			if (!($(this).hasClass('disabled')))
			{
			if (isMoving == false)
			{
					
				$activeCaption = $('#captionContainer>div.slide.active>div.caption');
				$activeCaptionOffset = $activeCaption.position();
				// If the next click would be too far, hide the button
				if (Math.abs($activeCaptionOffset.top)+scrollAmount >= $activeCaption.height()-scrollAmount) {	
						//$('#scrollDown').hide();
						$('#scrollDown').addClass('disabled');
					
				}
				isMoving = true;
				
				$activeCaption.animate({"top": "-="+(scrollAmount-scrollPad)}, "slow", stopMoving);
				$('#scrollUp').removeClass('disabled').show();
			}
			}
		});
		$('#scrollUp').click(function(){
			if (!($(this).hasClass('disabled')))
				{
				if (isMoving == false)
				{
					$activeCaption = $('#captionContainer>div.slide.active>div.caption');
					$activeCaptionOffset = $activeCaption.position();
					// if the next click would bring it above 0, hide the button
					if (Math.abs($activeCaptionOffset.top)-scrollAmount < 0+scrollAmount)
					{
						//$('#scrollUp').hide();
						$('#scrollUp').addClass('disabled');
					}
					isMoving = true;
					$activeCaption.animate({"top": "+="+(scrollAmount-scrollPad)}, "slow", stopMoving);
					$('#scrollDown').removeClass('disabled').show();
				}	
				}
		});
		


	}
);
