
// gallery.js - version 0.5 - Spry Pre-Release 1.5
//
// Copyright (c) 2006. Adobe Systems Incorporated.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// Global variables:

var gThumbWidth;
if (gThumbWidth == undefined)
	gThumbWidth = 30;
  
var gThumbHeight;
if (gThumbHeight == undefined)
	gThumbHeight = 30;

var gSlideShowInterval;
if (gSlideShowInterval == undefined)
	gSlideShowInterval = 3000; // msecs between images.

var gAutoStartSlideShow;
if (gAutoStartSlideShow == undefined)
	gAutoStartSlideShow = true;

var gBehaviorsArray = [];
var gSlideShowOn = false;
var gSlideShowTimer = null;
var gImageLoader = null;
var highlightTheThumb = [];
var dimTheThumb = [];

// Register a callback on the dsPhotos data set so we can turn
// off the slide show before it attempts to load new data.

dsPhotos.addObserver(function(nType, notifier, data) {
	if (nType == "onPreLoad")
		StopSlideShow();
});

// Register a callback on the thumbnails region so we can show the first
// image in the data set after all the thumbnails have loaded.

Spry.Data.Region.addObserver("thumbnails", function(nType, notifier, data) {
	if (nType == "onPostUpdate")
	{
		ShowCurrentImage();
		if (gAutoStartSlideShow)
			StartSlideShow(true);
	}
});

// Trigger the transition animation from the current image
// being displayed to the image at imgPath.
function setMainImage(imgPath, tnID){
	var img = document.getElementById('mainImage');
	if (!img)
		return;
	CancelBehavior('mainImage');
	Spry.Utils.SelectionManager.clearSelection("thumbnailSelection");

	if (tnID)
		Spry.Utils.SelectionManager.select("thumbnailSelection", document.getElementById(tnID), "selectedThumbnail");
	
	gBehaviorsArray[tnID] = new Spry.Effect.Fade(img, {duration: 500, from: 100, to: 0, finish: function(){
			gImageLoader = new Image();
			gImageLoader.onload = function()
			{
				img.src = gImageLoader.src;
				gImageLoader = null;
				gBehaviorsArray[tnID] = new Spry.Effect.Fade(img, {duration: 500, from: 0, to: 100});
				gBehaviorsArray[tnID].start();
				if (gSlideShowOn)
					SetSlideShowTimer();
			}
			gImageLoader.src = imgPath;
		}
	});
	gBehaviorsArray[tnID].start();
}

// Cancel the animation behavior of the object with the given id.
function CancelBehavior(id)
{
	if (gBehaviorsArray[id])
	{
		gBehaviorsArray[id].cancel();
		gBehaviorsArray[id] = null;
	}
}

// change thumbnail from dim to bright
function HighlightThumb(thumbdiv){
	var id = thumbdiv.getAttribute("id");
	if(!highlightTheThumb[id]){
		highlightTheThumb[id] = new Spry.Effect.Fade(thumbdiv, {duration: 250, from: 50, to: 100});
	}
	highlightTheThumb[id].start();
}

// change thumbnail from bright to dim
function DimThumb(thumbdiv){
	var id = thumbdiv.getAttribute("id");
	if(!dimTheThumb[id]){
		dimTheThumb[id] = new Spry.Effect.Fade(thumbdiv, {duration: 500, from: 100, to: 50});
	}
	dimTheThumb[id].start();
}

// Show the image of the current selected row inside the dsPhotos data set.

function ShowCurrentImage()
{
	var curRow = dsPhotos.getCurrentRow();
	setMainImage(curRow["url"], "thumb_" + curRow["ds_RowID"]);
}

// Utility function to advance (forwards or backwards) the current selected row
// in dsPhotos. This has the side effect of "selecting" the thumbnail and image
// of the new current row.

function AdvanceToNextImage(moveBackwards)
{
	var rows = dsPhotos.getData();
	var curRow = dsPhotos.getCurrentRow();
	//alert("Next slide show image:" + curRow);
	if (rows.length < 1)
		return;
	
	for (var i = 0; i < rows.length; i++)
	{
		if (rows[i] == curRow)
		{
			if (moveBackwards)
				--i;
			else
				++i;
			break;
		}
	}
	
	if (!moveBackwards && i >= rows.length)
		i = 0;
	else if (moveBackwards && i < 0)
		i = rows.length - 1;
	
	curRow = rows[i];
	dsPhotos.setCurrentRow(curRow["ds_RowID"]);
	ShowCurrentImage();
}

function SetSlideShowTimer()
{
	//alert("SetSlideShowTimer");
	KillSlideShowTimer();
	gSlideShowTimer = setTimeout(function(){ gSlideShowTimer = null; AdvanceToNextImage(false); }, gSlideShowInterval);
}

function KillSlideShowTimer()
{
	//alert("KillSlideShowTimer");
	if (gSlideShowTimer)
		clearTimeout(gSlideShowTimer);
	gSlideShowTimer = null;
}

// Start the slide show that runs forwards through all
// the rows in dsPhotos.

function StartSlideShow(skipTimer)
{
	//alert("Start Slide Show:" + skipTimer);
	gSlideShowOn = true;
	if (!skipTimer)
		SetSlideShowTimer();
}

// Kill any slide show that is currently running.
function StopSlideShow()
{
	//alert("stop slideshow");
	gSlideShowOn = false;
	KillSlideShowTimer();
}

function HandleThumbnailClick(id)
{
	StopSlideShow();
	dsPhotos.setCurrentRow(id);
	ShowCurrentImage();
}
