var timerRunning = false;
var fadeRunning = false;
var effectRun = null;
var time1 = null;
var time3 = null;
var frame = null;

//------------------------------
// BEGIN TRANSITION FOR IMAGE
//------------------------------
var delay = 6000;  //how long the image sits on screen, in MS
var start_frame = 0;  // beginning of the array



function init() {
	imageName.each(function(item) { // adds each images as a list item to the ul "slide-images"
   		var element = $(Builder.node('li', [
			$(Builder.node('img', { src: item }))
		]));
	$('slide-images').appendChild(element);
	});

	lis = $('slide-images').getElementsByTagName('li');  // makes an "array" of all list items inside the element with the id "slide-images"

	for( i=0; i < lis.length; i++){
		if(i!=0){
			lis[i].style.display = 'none';
		}
	} //sets all list items inside "slide-images" to 'none'
	end_frame = lis.length -1;  //end of the array

	start_slideshow(start_frame, end_frame, delay, lis);


}



function start_slideshow(start_frame, end_frame, delay, lis) {
	setTimeout(fadeInOut(start_frame,start_frame,end_frame, delay, lis), delay);  // sets the timeout to run "fadeInOut"
}


function fadeInOut(frame, start_frame, end_frame, delay, lis) {  // fades the last image out and the next one in (e.g. fades out 2, fades in 3)
	return (function() {
		lis = $('slide-images').getElementsByTagName('li');
		effectRun = Effect.Fade(lis[frame], {afterFinish: function(){fadeRunning = false;}});
		if (frame == end_frame) { frame = start_frame; } else { frame++; }
		lisAppear = lis[frame];
		setTimeout("Effect.Appear(lisAppear);", 0); // this determines how long between images - 0 is crossfade
		time1 = setTimeout(fadeInOut(frame, start_frame, end_frame, delay), delay + 1500);
	})

}




//-------------------------------------------------------------------------
// BEGIN TRANSITION FOR BOTTOM INFO BAR (IMAGE NAME AND POSTER), TOP BAR
//-------------------------------------------------------------------------

var topDelay = 6000;  // how long the copy sits on screen, in MS
var top_start_frame = 0;  // beginning of the array



function init3() {
	for (i=0; i<topPhoto.length; i++){ // adds each images as a list item to the ul "topSlide"
	var element3 = Builder.node('li', [
						Builder.node('strong', topPhoto[i]),
						' shared by ' + topPoster[i] + ", member since " + memberSince[i],
						]);
	$('topSlide').appendChild(element3);
	};

	lis3 = $('topSlide').getElementsByTagName('li');  // makes an "array" of all list items inside the element with the id "topSlide"

	for( i=0; i < lis3.length; i++){
		if(i!=0){
			lis3[i].style.display = 'none';
		}
	} //sets all list items inside "slide-images" to 'none'
	top_end_frame = lis3.length -1;  //end of the array

	start_slideshow3(top_start_frame, top_end_frame, topDelay, lis3);


}


function start_slideshow3(top_start_frame, top_end_frame, topDelay, lis3) {
	setTimeout(topFadeInOut(top_start_frame,top_start_frame,top_end_frame, topDelay, lis3), topDelay);  // sets the timeout to run "fadeInOut"
}


function topFadeInOut(topFrame, top_start_frame, top_end_frame, topDelay, lis3) {// fades the last image out and the next one in (e.g. fades out 2, fades in 3
	fadeRunning = true;
	return (function() {
		lis3 = $('topSlide').getElementsByTagName('li');
		Effect.Fade(lis3[topFrame]);
		if (topFrame == top_end_frame) { topFrame = top_start_frame; } else { topFrame++; }
		callToActionscript(subImageName[topFrame].toString());
		lisAppear3 = lis3[topFrame];
		setTimeout("Effect.Appear(lisAppear3);", 1500);
		time3 = setTimeout(topFadeInOut(topFrame, top_start_frame, top_end_frame, topDelay), topDelay + 1500);
	})

}

//-------------------------------------------------------------------
// MAKE THE ARROWS VISIBLE
//-------------------------------------------------------------------

function startArr(){
	theArrs = $('topArr');
	setTimeout("Effect.Appear(theArrs);", 1500);
}




//-------------------------------------------------------------------
// GET THE FUNCTIONS MOVING ON WINDOW LOAD
//-------------------------------------------------------------------

Event.observe(window, 'load', initAll, false);

function initAll(){
	if ($('topSlide')) {
		timerRunning = true;
		init3();
		init();
		startArr();
	}
}


//-------------------------------------------------------------------
// CALL THE MANUAL SLIDE TRANSITIONS
//-------------------------------------------------------------------

function goLast(){
	if (fadeRunning){
		return;
	} else 	if(timerRunning){
	clearTimeout(time1);
	clearTimeout(time3);
	timerRunning = false;
	} else {
		fadeRunning = true;
		lis = $('slide-images').getElementsByTagName('li');
		howLong = lis.length - 1;
		for (i=0; i<lis.length; i++){
			if (lis[i].style.display != 'none'){
				frame = i;
				topFadeBackNoTime(frame, howLong);
				FadeBackNoTime(frame, howLong);
			};
		}
	}
}

function goNext(){
	if (fadeRunning){
		return;
	} else 	if(timerRunning){
	clearTimeout(time1);
	clearTimeout(time3);
	timerRunning = false;
	} else {
		fadeRunning = true;
		lis = $('slide-images').getElementsByTagName('li');
		howLong = lis.length - 1;
		for (i=0; i<lis.length; i++){
			if (lis[i].style.display != 'none'){
				frame = i;
				topFadeNoTime(frame, howLong);
				FadeNoTime(frame, howLong);
			};
		}
	}
}



//-------------------------------------------------------------------
// MANUAL SLIDE TRANSITIONS
//-------------------------------------------------------------------


function topFadeNoTime(topFrame, top_end_frame) {// fades the current image out and the next one in (e.g. fades out 2, fades in 3
	lis1 = $('topSlide').getElementsByTagName('li');
	Effect.Fade(lis1[topFrame]);
	if (topFrame == top_end_frame) { topFrame = 0; } else { topFrame++; }
	callToActionscript(subImageName[topFrame].toString());
	lisAppear1 = lis1[topFrame];
	setTimeout("Effect.Appear(lisAppear1);", 2000);
	setTimeout("clearFade()", 2000);
}

function topFadeBackNoTime(topFrame, top_end_frame) {// fades the current image out and the previous one in (e.g. fades out 3, fades in 2
	lis1 = $('topSlide').getElementsByTagName('li');
	Effect.Fade(lis1[topFrame]);
	if (topFrame == 0) { topFrame = top_end_frame; } else { topFrame--; }
	callToActionscript(subImageName[topFrame].toString());
	lisAppear1 = lis1[topFrame];
	setTimeout("Effect.Appear(lisAppear1);", 2000);
	setTimeout("clearFade()", 2000);
}

function FadeNoTime(frame, end_frame) {// fades the last current out and the next one in (e.g. fades out 2, fades in 3
	lis3 = $('slide-images').getElementsByTagName('li');
	effectRun = Effect.Fade(lis3[frame]);
	if (frame == end_frame) { frame = 0; } else { frame++; }
	lisAppear3 = lis3[frame];
	setTimeout("Effect.Appear(lisAppear3);", 0);
}

function FadeBackNoTime(frame, end_frame) {// fades the current image out and the previous one in (e.g. fades out 3, fades in 32
	lis3 = $('slide-images').getElementsByTagName('li');
	effectRun = Effect.Fade(lis3[frame]);
	if (frame == 0) { frame = end_frame; } else { frame--; }
	lisAppear3 = lis3[frame];
	setTimeout("Effect.Appear(lisAppear3);", 0);
}

function clearFade(){
	fadeRunning = false;
}

//-------------------------------------------------------------------
// CALL TO ACTIONSCRIPT IN FLASH
//-------------------------------------------------------------------

function getFlashMovie(movieName) {
  //var isIE = navigator.appName.indexOf("Microsoft") != -1;
  //return (isIE) ? window[movieName] : document[movieName];

	return $(movieName);
}

function callToActionscript(str)
{
     getFlashMovie("gathertopswf").sendToActionscript(str);
}