// JavaScript Document

/***********************************************
* TC SlideShow- Displays a series of elements as a slide show
***********************************************/

function slideShow(slidePrefix, slideIndex, slideCount, delay, repeatFlag) {
	if (self.cycle) {clearInterval(cycle);}
	theDisplayedSlideIndex = Math.max(slideIndex - 1, 0);
	theSlidePrefix = slidePrefix;
	theNextSlideIndex = slideIndex;
	theSlideCount = slideCount;
	theDelay = delay;
	theRepeatFlag = repeatFlag;
	playStatus = 1;
	serializedStatus = 0;
	cycle = setInterval('fadeSlides(); theNextSlideIndex++;', theDelay);
}

function tcSlideShow() {
	if (self.cycle) {clearInterval(cycle);}
	displayedSlide = 0;
	slideCount = 3;
	tick = 0;
	tickCount = 12;
	idle = 0;
	cycle = setInterval('playSlides(); tick++;', 1000);
}

function playSlides() {
	tick = (tick % tickCount);
	//alert('tick: ' + tick);
	if (tick == 0) {
		// change before image
		idle = 0; // clear idle flag, we're doing stuff
		nextSlide = ((displayedSlide + 1) % slideCount); // when tick count begins anew, increment the slide
		theNextBeforeImageSrc = 'images/before_' + nextSlide + '.jpg';
		crossfade(document.getElementById('before_image'), theNextBeforeImageSrc, '1');
	} else if (tick == 2) {
		// idle cycle, do nothing but set idle flag
		idle = 1;
	} else if (tick == 3) {
		// change after image
		idle = 0;
		theNextAfterImageSrc = 'images/after_' + nextSlide + '.jpg';
		crossfade(document.getElementById('after_image'), theNextAfterImageSrc, '1');
	} else if (tick == 4) {
		// change profile
		idle = 0;
		theDisplayedProfileID = 'profile_' + displayedSlide;
		theNextProfileID = 'profile_' + nextSlide;
		Effect.Fade(theDisplayedProfileID, {duration:0.1});
		Effect.Appear(theNextProfileID);
		displayedSlide = nextSlide;
	} else {
		// idle cycle, do nothing but set idle flag
		idle = 1;
	}
}


function pauseSlideShow() {
	if (playStatus == 1) {
		clearInterval(cycle);
		playStatus = 0;
	}
}

function playSlideShow() {
	if (playStatus == 0) {
		playStatus = 1;
		if (theDisplayedSlideIndex >= theSlideCount) {
			theNextSlideIndex = 0;
		}
		else {
			theNextSlideIndex = theDisplayedSlideIndex + 1;
		}
		if (serializedStatus == 1) {
			deserializeSlides();
			theNextSlideIndex = 0;
		}
		slideShow(theSlidePrefix, theNextSlideIndex, theSlideCount, theDelay, theRepeatFlag);
	}
}

function changeSlide(increment) {
	if (playStatus == 1) {
		pauseSlideShow();
	}
	theNextSlideIndex = theDisplayedSlideIndex + increment;
	theNextSlideIndex = Math.max(theNextSlideIndex, 0);
	theNextSlideIndex = Math.min(theNextSlideIndex, theSlideCount);
	fadeSlides();
}

function fadeSlides() {
	if (theRepeatFlag != 1 && (theNextSlideIndex >= theSlideCount)) {
		pauseSlideShow();
		return true;
	}
	else {
		theNextSlideIndex = (theNextSlideIndex % theSlideCount);
		theNextBeforeImageSrc = 'images/before_' + theNextSlideIndex + '.jpg';
		//alert('theNextBeforeImageSrc: ' + theNextBeforeImageSrc);
		theNextAfterImageSrc = 'images/after_' + theNextSlideIndex + '.jpg';
		theDisplayedProfileID = 'profile_' + theDisplayedSlideIndex;
		theNextProfileID = 'profile_' + theNextSlideIndex;
		crossfade(document.getElementById('before_image'), theNextBeforeImageSrc, '1');
		cycle = setTimeout("//alert('calling after fade'); crossfade_2(document.getElementById('after_image'), theNextAfterImageSrc, '2');", 2000);
		Effect.Fade(theDisplayedProfileID, {duration:0.1});
		Effect.Appear(theNextProfileID);
		theDisplayedSlideIndex = theNextSlideIndex;
	}
} 

function serializeSlides() {
	pauseSlideShow();
	theContainer = document.getElementById('lb_story_static');
	theContainer.style.overflow = "auto"; 
	for (var i=0; i < theSlideCount; i++) {
		theDisplayID = theSlidePrefix + i;  
		theSlide = document.getElementById(theDisplayID);
		theSlide.style.display = "block";
	}
	serializedStatus = 1;
}
		
function deserializeSlides() {
	pauseSlideShow();
	theContainer = document.getElementById('lb_story_static');
	theContainer.style.overflow = "hidden"; 
	for (var i=0; i < theSlideCount; i++) {
		theDisplayID = theSlidePrefix + i;  
		theSlide = document.getElementById(theDisplayID);
		theSlide.style.display = "none";
	}
	serializedStatus = 0;
}
	