// 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 = 10000; // msecs showing image.
var imgHideTime = 1; // msecs between image.

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

var mainFolder = rootDomain + 'photoBox'; // folder glowny
var groupFolder = 'images'; // folder galerii

var dsPhotos = new Spry.Data.XMLDataSet(rootDomain + "photoBox/pics.xml", "photos/photo"); // zaladowanie xml'a
var rows;


function SetMainImage(imgPath, width, height, linkPath)
{
	document.getElementById('photoBoxLink').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();
//	alert(curRow["@path"]);
	
	// podmienia zdjecie :)
	SetMainImage(mainFolder + "/" + groupFolder + "/" + curRow["@path"], curRow["@width"], curRow["@height"], curRow["@link"]);
}

function AdvanceToNextRow()
{
	//pobranie wierszy
//	var rows = dsPhotos.getData();

//	alert(rows.length);
	
	//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"];
//		alert(imagesTable[i].src);
	}
}



shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


function photoBox()
{
//	alert('ok');
	dsPhotos.loadData();
//	alert('ok2');
	var myObserver = new Object;
//	alert('ok3');
	myObserver.onPostLoad = function(ds, type)
	{
		rows = dsPhotos.getData();
		shuffle(rows);
//		alert(rows[0]["@path"]);
//		alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
		// usuniecie obserwera
		dsPhotos.removeObserver(myObserver);
//		alert('ok4');
		
		// preload obrazkow
		imgPreloader();
//		alert('imgPreloader');
	
		// strat galerii
		AdvanceToNextRow();
//		alert('AdvanceToNextRow');

	};
	dsPhotos.addObserver(myObserver);	
}

