/*##################################################
*                                scroll.js
*                            -------------------
*   begin                : August 23, 2007
*   copyright          : (C) 2007 Viarre Régis
*   email                : crowkait@phpboost.com
*
*
###################################################
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation; either version 2 of the License, or
*   (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
###################################################*/

var scroll_stopped = false; //Statut du défilement.
var timeout;

//Démarrage du défilement, dépendant du mode.
function scroll_start()
{
	if( scroll_mode == 'dynamic_scroll' )
		scroll_pics();
	else
		switch_pics();
}

//Arrêt/redémarrage du défilement.
function temporize_scroll()
{
	if( scroll_stopped ) //Lancement du défilement.
	{
		scroll_start();
		scroll_stopped = false;
	}
	else //Arrêt
	{	
		if( timeout )
			clearTimeout(timeout);	
		scroll_stopped = true;
	}
}

//Transtypage des entiers.
function numeric(number)
{
	return isNaN(number = parseInt(number)) ? 0 : number;
}

/* Scroll mode */
//pics_height, nbr_pics et hidden_height, scroll_speed en global.
function scroll_pics()
{
	var regex = /(-?[0-9]+)px/;
	for(var i = 0; i < nbr_pics; i++)
	{
		var thumb = document.getElementById('thumb_mini' + i);
		if( thumb )
		{
			var get_top = regex.exec(thumb.style.top);		
			if( get_top[1] )
				get_top = numeric(get_top[1]) - 1;
			else
				get_top = 0;	
			thumb.style.top = get_top + 'px';	

			if( -get_top > sum_height )
				restart_scroll();
		}
	}
	timeout = setTimeout('scroll_pics()', scroll_speed);
}

//Recommence le défilement du début.
function restart_scroll()
{
	for(var i = 0; i < nbr_pics; i++)
	{
		var thumb = document.getElementById('thumb_mini' + i);
		if( thumb )
			thumb.style.top = hidden_height + 'px';	
	}
}

/* Switch mode */	
function switch_pics()
{
	var index;

	index = Math.round(Math.random() * (array_pics_mini.length - 1));		
	document.getElementById('thumb_mini').innerHTML = '<a href="../gallery/gallery' + array_pics_mini[index]['link'] + '"><img src="../gallery/pics/thumbnails/' + array_pics_mini[index]['path'] + '" alt="" /></a>';

	timeout = setTimeout("switch_pics()", scroll_speed);
}

//Lancement.
scroll_start(); 

