var currentStart = 0;
var fading = false;

/**
 * Go one page up
 */
function goPageUp()
{
	if (!fading ) {
		fading = true;
		Effect.Appear('pic_choice_nav', {from:1.0, to:0.0, duration:0.5 });
		Effect.Puff('pic_choice', {duration:0.5});
		window.setTimeout('changePics(true)',500);
	}
}

/**
 * Go one page down
 */
function goPageDown()
{	
	if (!fading ) {
		fading = true;
		Effect.Appear('pic_choice_nav', {from:1.0, to:0.0, duration:0.5 });
		Effect.DropOut('pic_choice', {duration:0.5});
		window.setTimeout('changePics(false)',500);
	}
}

/**
 * Show new pictures
 */
function changePics(up)
{	
	if ( up )
		currentStart = currentStart + picsPerPage;
	else
		currentStart = currentStart - picsPerPage;
	
	for ( i = 0; i < picsPerPage && (currentStart + i) < imageCount; i++ )
	{
		$("pic"+i).show();
		eval('$("pic'+i+'").src = thumbs['+(currentStart+i)+'].src;');
	}
	for ( i = i; i < picsPerPage; i++ )
	{
		$("pic"+i).hide();
	}
	for ( i = 0; i < picsPerPage && (currentStart + picsPerPage + i) < imageCount; i++ )
	{
		if ( typeof(pics[currentStart + picsPerPage + i]) == "undefined" ) {
			pics[currentStart + picsPerPage + i] = new Image();
			pics[currentStart + picsPerPage + i].src = picFolder + fileNames[currentStart + picsPerPage + i];
		}
		if ( typeof(thumbs[currentStart + picsPerPage + i]) == "undefined" ) {
			thumbs[currentStart + picsPerPage + i] = new Image();
			thumbs[currentStart + picsPerPage + i].src = thumbFolder + fileNames[currentStart + picsPerPage + i];
		}
		
	}
	
	if ( up )
		window.setTimeout('Effect.Appear(\'pic_choice\', {duration:0.5 })',100);
	else
		window.setTimeout('Effect.SlideDown(\'pic_choice\', {duration:0.5 })',100);
		
	showHidePicNavs();
	window.setTimeout('Effect.Appear(\'pic_choice_nav\', {from:0.0, to:1.0, duration:0.6 })',100);
	window.setTimeout('fading = false', 600);
}

/**
 * Show and hide 'up'/'down' buttons
 */
function showHidePicNavs()
{
	if ( currentStart + picsPerPage > imageCount )
		$("go_up").style.visibility = 'hidden';
	else
		$("go_up").style.visibility = 'visible';
	
	if ( currentStart - picsPerPage < 0 )
		$("go_down").style.visibility = 'hidden';
	else
		$("go_down").style.visibility = 'visible';
		
}

/**
 * Hide 'up'/'down' buttons
 */
function hidePicNavs() {
	$("go_up").style.visibility = 'hidden';
	$("go_down").style.visibility = 'hidden';
}

/**
 * Show (big) picture with given number
 * ...fadeout
 */
function showPic(no)
{
	// Set Id for next pic
	var nextPicId = no + 1;
	
	// If the end of the set has been reached
	if ( (currentStart+nextPicId) >= imageCount ) {
		nextPicId = -1;
	}
	// If the end of the current page has been reached, go one page up
	else if (nextPicId > picsPerPage) {
		goPageUp();
		nextPicId = 1;
	}
	
	setTimeout('showNewPic('+(currentStart+no)+', ' + nextPicId + ')',600);
	Effect.Fade('mainpic', {duration:0.5});
}

/**
 * Show (big) picture with given number
 * ...fadein
 */
function showNewPic(no, next)
{
	$("mainpic").src = pics[no].src;
	$("mainpiclink").href = ( next == -1 ? "#" : "javascript:showPic(" + next + ");" );
	Effect.Appear('mainpic', {duration:0.5});
}
