/* gallery.js - Revision: Spry Preview Release 1.4 */

// 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 gGalleryCurrentRow 	= 0;

var gThumbWidth;
if (gThumbWidth == undefined)
  gThumbWidth 			= 48;

var gThumbHeight;
if (gThumbHeight == undefined)
  gThumbHeight 			= 48;

var gSlideShowInterval;
if (gSlideShowInterval == undefined)
  gSlideShowInterval 	= 6000; // msecs between images. default=3000

var gAutoStartSlideShow;
if (gAutoStartSlideShow == undefined)
  gAutoStartSlideShow 	= true;

var gBehaviorsArray 	= [];
var gSlideShowOn 		= false;
var gSlideShowTimer 	= null;
var gImageLoader 		= null;


function ShowRecord(rowid)
{
  	var ZERO_OPACITY = 0.0;
  	var FULL_OPACITY = 1.0;
  	
  	var tnID = "tn" + rowid;
  	var img = $("MainImage");
  	if (!img)
    	return;

  CancelBehavior("fadeout_MainImage");
  CancelBehavior("sizemorph_MainImage");

  if (gImageLoader)
  {
    gImageLoader.onload = function() {};
    gImageLoader = null;
  }

	// do morph

	// fadeout image
	gBehaviorsArray["fadeout_MainImage"] = new Spry.Effect.Opacity( img, Spry.Effect.getOpacity(img), ZERO_OPACITY, { duration: 800,
		finish: function()
				{
					// set global pointer
					gGalleryCurrentRow = rowid;					
					
				  	// get new image dimensions
				  	var storedImage = $("SlideShowPix").getElementsBySelector("img")[gGalleryCurrentRow];
				  	var imgPath 	= storedImage.src;
				  	var width 		= storedImage.width;
				  	var height 		= storedImage.height;
				  	var aspectRatio = width / height;
				  	
					// get the div we're applying the morph to
					img 			= $("MainImage");
				  	var morphHandle = img.parentNode;
				  	
					// get viewbox dimensions (available display area for image)
					var boxDims 	= Spry.Effect.getDimensions(morphHandle.parentNode);
					var boxWidth 	= boxDims.width;
					var boxHeight 	= boxDims.height;
				
					// set any paddings here
					boxWidth 		-= 0;
					boxHeight 		-= 0;
					var boxAspect 	=  boxWidth / boxHeight;

					// fit image to viewbox					  
					var newWidth 	= 0;
					var newHeight	= 0;
					if( aspectRatio > boxAspect )
					{
						newWidth 	= boxWidth;
						newHeight 	= newWidth / aspectRatio;
					}
					else
					{
						newHeight 	= boxHeight;
						newWidth 	= newHeight * aspectRatio;
					} 
	  
					// use image loader to preload new image, and then display it
					gImageLoader = new Image();
					gImageLoader.onload = 	function()
											{
												// swap in fully-loaded image and clean up
												img.src = gImageLoader.src;
												gImageLoader = null;
												
												// fade in main image
												gBehaviorsArray["fadein_MainImage"] = new Spry.Effect.Opacity(img, ZERO_OPACITY, FULL_OPACITY, { duration: 800,
													finish: function()
													{				
														// Our new image is fully visible now. If the slide show
														// is on, fire off the timer for the next image.
									
														if (gSlideShowOn)
															SetSlideShowTimer();
													}});				
												gBehaviorsArray["fadein_MainImage"].start();
										  	};
					gImageLoader.src = imgPath;
				}});
	gBehaviorsArray["fadeout_MainImage"].start();
}



function CancelBehavior(id)
{
  if (gBehaviorsArray[id])
  {
    gBehaviorsArray[id].cancel();
    gBehaviorsArray[id] = null;
  }
}



function SizeAndPosition(id, toX, toY, toWidth, toHeight, callback)
{
  CancelBehavior(id);
  var effectCluster = new Spry.Effect.Cluster( { finish: callback } );
  var ele = Spry.Effect.getElement(id);
  var moveEffect = new Spry.Effect.Move(ele, Spry.Effect.getPosition(ele), { x: toX, y: toY, units: "px" }, { duration: 400 });
  var sizeEffect = new Spry.Effect.Size(ele, Spry.Effect.getDimensions(ele), { width: toWidth, height: toHeight, units: "px" }, { duration: 400 });

  effectCluster.addParallelEffect(moveEffect);
  effectCluster.addParallelEffect(sizeEffect);

  //effectCluster.finish = callback;
  gBehaviorsArray[id] = effectCluster;
  gBehaviorsArray[id].start();
}





function AdvanceToNextRecord(moveBackwards)
{
  var numberOfImages = $("SlideShowPix").getElementsByTagName("img").length;

  if (numberOfImages < 1)
    return;

  for (var i = 0; i < numberOfImages; i++)
  {
    if (i == gGalleryCurrentRow)
    {
      if (moveBackwards)
        --i;
      else
        ++i;
      break;
    }
  }

  if (!moveBackwards && i >= numberOfImages)
    i = 0;
  else if (moveBackwards && i < 0)
    i = numberOfImages - 1;

  ShowRecord(i);
}


function SetSlideShowTimer()
{
  KillSlideShowTimer();
  gSlideShowTimer = setTimeout(function(){ gSlideShowTimer = null; AdvanceToNextRecord(false); }, gSlideShowInterval);
}


function KillSlideShowTimer()
{
  if (gSlideShowTimer)
    clearTimeout(gSlideShowTimer);
  gSlideShowTimer = null;
}


function StartSlideShow(skipTimer)
{
  gSlideShowOn = true;
  if (!skipTimer)
  	SetSlideShowTimer();
  else
  	AdvanceToNextRecord(false);
}

function StopSlideShow()
{
  gSlideShowOn = false;
  KillSlideShowTimer();
}

