// gallery.js - version 0.5 - Spry Pre-Release 1.5
//
// Zmodyfikowana przez Bartłomiej Zabdyr
//
// 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 imgShowTime = 6000; // msecs showing image.
var imgHideTime = 100; // msecs between image.

var imgShowingTime = 600; // msecs opacity 0 to 100.
var imgHidingTime = 200; // msecs opacity 100 to 0.
var gImageLoader = null;

var mainFolder = 'fotoBox'; // folder glowny
var groupFolder = 'images'; // folder galerii

var dsPhotos = new Spry.Data.XMLDataSet("fotoBox/getPhoto.php", "photos/photo"); // zaladowanie xml'a


function SetMainImage(imgPath, width, height, linkPath)
{
	document.getElementById('fotoBoxLink').href = linkPath;
	var img = document.getElementById("mainImage");
	if (!img)
		return;
	
	var hiding = new Spry.Effect.Opacity("mainImage", 1, 0, {duration: imgHidingTime, toggle: true,
		finish: function()
		{
//			img.src = imgPath;
			gImageLoader = new Image();
			gImageLoader.onload = function()
			{
				img.src = gImageLoader.src;
				gImageLoader = null;
				setTimeout("showingImg();", imgHideTime);
			};
			gImageLoader.src = imgPath;
		}
	});
	hiding.start();
}

function showingImg() {
	var showing = new Spry.Effect.Opacity("mainImage", 0, 1, {duration: imgShowingTime, toggle: true});
	showing.start();
}

function ShowCurrentImage()
{
	// pobiera bierzacy wiersza
	var curRow = dsPhotos.getCurrentRow();
	
	// podmienia zdjecie :)
	SetMainImage(mainFolder + "/" + groupFolder + "/" + curRow["@path"], curRow["@width"], curRow["@height"], curRow["@link"]);
}

function AdvanceToNextRow()
{
	//pobranie wierszy
	var rows = dsPhotos.getData();
	
	//pobranie bierzacego wiersza
	var curRow = dsPhotos.getCurrentRow();
	
	// jezeli nie ma wierszy to wychodzi
	if (rows.length < 1)
		return;
	
	// numer nastepnego wiersza
	var nextRow = null;
	
	//sprawdza ktory wiersz jest bierzacy
	for (var i = 0; i < rows.length; i++)
	{
		if (rows[i] == curRow)
		{
			nextRow = i+1;
		}
	}
	
	//jezeli doszedl do konca wraca na poczatek
	if (nextRow >= rows.length)
		nextRow = 0;

	//pobranie parametrow kolejnego wiersza
	curRow = rows[nextRow];
	
	//ustawia parametry nastepnego wiersza
	dsPhotos.setCurrentRow(curRow["ds_RowID"]);
	
	// wyswietla nastepne zdjecie
	ShowCurrentImage();
	setTimeout("AdvanceToNextRow()", imgShowTime);
}

function imgPreloader()
{
	var rows = dsPhotos.getData();
	var imagesTable = new Array();
	for (var i = 0; i < rows.length; i++)
	{
		imagesTable[i] = new Image;
		imagesTable[i].src = mainFolder + "/" + groupFolder + "/" + rows[i]["@path"];
	}
}

function fotoBox()
{
	dsPhotos.loadData();
	var myObserver = new Object;
	myObserver.onPostLoad = function(ds, type)
	{
		// usuniecie obserwera
		dsPhotos.removeObserver(myObserver);
		
		// preload obrazkow
		imgPreloader();

		// strat galerii
		AdvanceToNextRow();

	};
	dsPhotos.addObserver(myObserver);	
}
