/*
 *
 *	jason's gallery slideshow flipper script
 *	written by Jason Baker september 2006 for:
 *		onejasonforsale.com
 *		anesotericvision.com
 *		and worldsworstsoftware.com
 *
 *	feel free to use it on your sites!
 *
 *  Note: the library has only been tested in FF 1.X+ and IE 6+
 *
 *	Note: This script depends on the "Worlds Worst Software Image Library" (wwsimagelib.js).
 *        Be sure to include that in your page before you include this script.
 *
 */


//these are internal variables used by the gallery script, do not alter them.
var _galleryArray = new Array();
var _galleryIndex = -1;
var _galleryContainerId = false;
var _galleryMessageContainerId = false;
var _galleryName = "";
var _galleryImageHasBeenShown = false;

var _imageScroller = null;


function resetIndex()
{
	_galleryIndex = 0;
}

function showNextImage()
{
	if (_galleryIndex < (_galleryArray.length - 1))
	{
		_galleryIndex += 1;
		loadImage('next');
	}

	return false;
}

function showPrevImage()
{
	if (_galleryIndex > 0)
	{
		_galleryIndex -= 1;
		loadImage('prev');
	}

	return false;
}

function loadImage(action)
{
	var newImage = new Image();	
	newImage.onload = imageLoadComplete;
	newImage.action = action;
	newImage.galleryIndex = _galleryIndex;
	newImage.galleryImage = _galleryArray[newImage.galleryIndex];
	newImage.src = _galleryArray[newImage.galleryIndex];
}

function imageLoadComplete()
{		
	
	//for 2009 version just call image scroller..
	
	_galleryImageHasBeenShown = true;
	
	if (this.action == 'next')
	{
		_imageScroller.scrollImageFromLeft(this.src);
	}
	else
	{
		_imageScroller.scrollImageFromRight(this.src);
	}
	
	_galleryIndex = this.galleryIndex;
	reset_thumbs_page(_galleryIndex);
	show_or_hide_gallery_controls();
	replace_direct_link(this.galleryImage);
	
	/*
	var _container = document.getElementById(_galleryContainerId);
	
	if (_container == false)
	{
		alert("ERROR: imageLoadComplete could not find container \"" + divId + "\"");
		return false;
	}
	
	var imageWidth = this.width;
	var imageHeight = this.height;
	
	var resizeRatio = 1;
	
	if ((imageWidth > _container.offsetWidth) || (imageHeight > _container.offsetHeight))
	{
		//image is bigger than container, scale it down..
		if (imageWidth > imageHeight)
		{
			//image is wider than tall
			resizeRatio = imageWidth / _container.offsetWidth;

			//make sure the resize ratio wouldnt make the image taller than the bounding size
			if ((imageHeight / resizeRatio) > _container.offsetHeight)
			{
				resizeRatio = imageHeight / _container.offsetHeight;
			}

		}
		else
		{
			//image is either square or taller than wide
			resizeRatio =  imageHeight / _container.offsetHeight;

			//make sure the resize ratio wouldnt make the image wider than the bounding size
			if ((imageWidth / resizeRatio) > _container.offsetWidth)
			{
				resizeRatio = imageWidth / _container.offsetWidth;
			}
		}
		
		imageWidth = Math.round(imageWidth / resizeRatio);
		imageHeight = Math.round(imageHeight / resizeRatio);		
	}
	

	var leftStyle = Math.round(getCenteredOffset(imageWidth, _container.offsetWidth)) + "px";
	var rightStyle = Math.round(getCenteredOffset(imageHeight, _container.offsetHeight)) + "px";
	
	
	var imageHtml = "<img class=\"slideshow\" src=\"" + this.src + 
		"\" style=\"position: absolute; left: " + leftStyle + "; top: " + rightStyle + 
		";\" width=\"" + imageWidth + "\" height=\"" + imageHeight + "\" >";
	//alert(imageHtml);
	_container.innerHTML = imageHtml;
	replace_direct_link(this.galleryImage);
	_galleryIndex = this.galleryIndex;
	showGalleryMessage();
	*/
	
}

function show_or_hide_gallery_controls()
{
	if (_galleryIndex == 0)
	{
		showOrHideDiv(false, 'left');
	}
	else
	{
		showOrHideDiv(true, 'left');
	}
	
	if (_galleryIndex == (_galleryArray.length - 1))
	{
		showOrHideDiv(false, 'right');
	}
	else
	{
		showOrHideDiv(true, 'right');
	}
}

function showGalleryMessage()
{
	var message = _galleryName + " " + (_galleryIndex + 1) + " of " + _galleryArray.length;
	document.getElementById(_galleryMessageContainerId).innerHTML = message;
}

function setupGallery(imageContainerId, messageContainerId, galleryName, pictureArray)
{
	_galleryArray = pictureArray;

	//don't need this stuff in 2009 esoteric..
	/*
	_galleryContainerId = imageContainerId;

	_galleryMessageContainerId = messageContainerId;
	
	_galleryName = galleryName;

	//do some integrity checking (which isn't working for some reason.. sorry)

	var errors = "";

	if (document.getElementById(_galleryContainerId) == false)
	{
		errors = errors + "\n could not find gallery container \"" + _galleryContainerId + "\".";
	}

	if (document.getElementById(_galleryMessageContainerId) == false)
	{
		errors = errors + "\n could not find gallery container \"" + _galleryMessageContainerId + "\".";
	}

	if (errors != "")
	{
		errors = "Errors Occurred Checking Gallery Setup:" + errors;
		alert(errors);
		return false;
	}
	*/

	resetIndex();
	//dont do this in esoteric.
	//loadImage();
}

function galleryShowImage(imageSrc)
{
	for (var i = 0; i < _galleryArray.length; i++)
	{
		if (_galleryArray[i] == imageSrc)
		{
		
			//if this is the same image as already shown, don't do anything..
			if (i == _galleryIndex && _galleryImageHasBeenShown)
			{
				return;
			}
			
			var oldIndex = _galleryIndex;
			_galleryIndex = i;
			if (i >= oldIndex)	
			{
				loadImage('next');
			}
			else
			{
				loadImage('prev');
			}
			return;
		}
	}
	alert("Error: could not find image \"" + imageSrc + "\" in gallery array.");

}





