var currentEffect;

var moving = false;

//these are the current positions of each column.
var colCurElement = new Array(1, 1, 1, 1);

//these two functions move the given column number up or down if possible.
function MoveUp(colNum) {
	colIndex = colNum - 1;
	
	//if (currentEffect != null && currentEffect.finishOn > 0) 
	//don't start moving until the last move is done
	if (moving)
		return;
		
	//make sure we're not moving past the first element
	if (colCurElement[colIndex] <= 1) {
		checkArrows(colNum);
		return;
	}
	
	currentEffect = new Effect.MoveBy('main_scroll' + colNum, 405, 0, {
		duration: 1,
		transition: Effect.Transitions.sinoidal,
		afterFinish: moveFinished
	});

	moving = true;
	colCurElement[colIndex]--;
	
	checkArrows(colNum);
}

function MoveDown(colNum){
	colIndex = colNum - 1;
	
	//if (currentEffect != null && currentEffect.finishOn > 0)
	//don't start moving until the last move is done
	if (moving)
		return;

	//make sure we're not moving past the last element
	if (colCurElement[colIndex] >= colMaxElements[colIndex])  {
		checkArrows(colNum);
		return;
	}

	currentEffect = new Effect.MoveBy('main_scroll' + colNum, -405, 0, {
		duration: 1,
		transition: Effect.Transitions.sinoidal,
		afterFinish: moveFinished
	});

	moving = true;
	colCurElement[colIndex]++;

	checkArrows(colNum);
}

//check and change the arrow graphics to indicate not clickable if we are at the
//end of the list.
function checkArrows(colNum) {
	colIndex = colNum - 1;

	var upArrow = document.getElementById("upArrow" + colNum);
	var downArrow = document.getElementById("downArrow" + colNum);
	if (colCurElement[colIndex] <= 1)
		upArrow.className = "arrow_up_disabled";
	else
		upArrow.className = "arrow_up";
	if (colCurElement[colIndex] >= colMaxElements[colIndex])
		downArrow.className = "arrow_down_disabled";
	else
		downArrow.className = "arrow_down";
}

//call this when the effect is done.
//it essentially enables another move to happen.
function moveFinished(obj) {
	moving = false;
}

var slideShowIndex = 1;
function slideShowNext() {
	//only run the slideshow if next image and current image are loaded
	var nextIndex = slideShowIndex - 2;
	if (nextIndex < 0) {
		nextIndex = maxSlides - 1;
	}

	if (imgDone($('slide' +  nextIndex     ).getElementsByTagName('img')[0]) &&
		imgDone($('slide' + (nextIndex + 1)).getElementsByTagName('img')[0])) {

		if (--slideShowIndex <= 0) {
			slideShowIndex = maxSlides;
			for (var i = 1; i <= maxSlides; i++) {
				Element.show('slide' + i);
			}
		}

		new Effect.SlideUp('slide' + slideShowIndex);
	}
}

function hideSlideShow() {
	//stop the show!
	if (slideShowIntId > 0) {
		clearInterval(slideShowIntId);
	}

	//we will make the columns scroll up in sequence,
	//but first move them all down so they can scroll up.
	//this function will call finishHideSlideShow when it's done.
	initializeColumns();
}
function finishHideSlideShow() {
	$('columns_slide').style.display = "block";

	//hide all non-visible slides, then slide up the visible (topmost) one.
	var curSlide = slideShowIndex - 1;
	for (var i = 0; i <= maxSlides; i++) {
		if (i != curSlide)
			Element.hide('slide' + i);
	}
	new Effect.SlideUp('slide' + curSlide, {
		afterFinish: superMagicColumnSlideGoNowTime
	}); 	//make the columns scroll up in sequence once this is done.
	
	showColumns();
}

function showColumns() {
	//show the arrows, but hide all the unclickable arrows at first
	// for (var i = 1; i < 5; i++) {
	for (var i = 1; i < 4; i++) {
		$('arrows' + i).style.visibility = "visible";
		checkArrows(i);
	}

	//just in case we didn't get to it before
	$('columns_slide').style.display = "block";
}

//makes the columns scroll up in sequence
var curSlideCol = 4
var colSlideInterval = -1;
var colSlideDuration = 0.5;
function superMagicColumnSlideGoNowTime()  {
	//do this every half second since it takes each column 1 second to slide up.
	if (curSlideCol == 4 && colSlideInterval < 0)
		colSlideInterval = setInterval('superMagicColumnSlideGoNowTime()',
			(colSlideDuration / 1.7) * 1000);
	else if (curSlideCol <= 0 && colSlideInterval >= 0)
		clearInterval(colSlideInterval);

	if (curSlideCol > 0)
		curSlide = new Effect.MoveBy('main_scroll' + curSlideCol, -405, 0, {
			duration: colSlideDuration,
			transition: Effect.Transitions.sinoidal
		});
	curSlideCol--;
}

//this will move the columns down so they can slide up when the slideshow ends.
//it will call finishHideSlideShow() when done.
function initializeColumns() {
	for (var i = 1; i < 4; i++) {
		curSlide = new Effect.MoveBy('main_scroll' + i, 405, 0, {
			duration: 0
		});
	}
	curSlide = new Effect.MoveBy('main_scroll4', 405, 0, {
		duration: 0,
		afterFinish: finishHideSlideShow
	});
}

function imgDone(img) {
	var complete = img.complete;
	var ret = null; // Return value
	// Workaround for Safari -- it only supports the
	// complete attrib on script-created images
	if (typeof complete == 'undefined') {
		var test = new Image();
		// If the original image was successfully loaded,
		// src for new one should be pulled from cache
		test.src = img.src;
		complete = test.complete;
	}
	// Check the complete attrib. Use strict equality
	// check -- don't want undefined, null, etc.
	// --------------------------
	// False -- Img failed to load in IE/Safari, or is
	// still trying to load in FF
	if (complete === false) {
		ret = false;
	}
	// True, but image has no size -- image failed to
	// load in FF
	else if (complete === true &&
	img.naturalWidth == 0) {
		ret = false;
	}
	// Otherwise all we can do is assume image loaded
	else {
		ret = true;
	}
	return ret;
}
