var selectedImageId			= null;
var selectedCategoryId		= null;
var selectedGalleryId		= null;
var selectedGalleryPage		= 0;
var selectedGalleryPageMax	= 0;
var selectedGalleryOffset	= 0;

function changeGallery(galleries_id, categories_id, offset)
{	
	$('imageScroller').style.display = '';
	
	selectedGalleryOffset = $('imageBoxInside').style.left.replace(/px/, '');
	
	if(selectedGalleryId != galleries_id)
	{
		// Viewing a new gallery, reset the page
		selectedGalleryPage 	= 0;
		selectedGalleryOffset	= 0;
	}
	
	if(offset != null)
	{
		selectedGalleryOffset = offset;
	}
	
	new Ajax.Updater('imageSlider', '/image_slider.php', 
		{
			method: 'get',
			evalScripts: true,
			parameters:
				{
					categories_id:		categories_id,
					galleries_id:		galleries_id,
					page:				selectedGalleryPage
				}
		}
	);

	selectedCategoryId		= categories_id;
	selectedGalleryId		= galleries_id;
	selectedImageId			= null;
}

function moveToPrevious()
{
	var newOffset	= (getOffset() - ($('imageBox').getWidth()/1.5 ));
	
	if(newOffset < 0)
	{
		scrollToStart();
		return;
	}
	
	new Effect.Move('imageBoxInside',
						{
							x: ($('imageBox').getWidth()/1.5 ),
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue: 
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
} 

function moveToNext()
{	
	var max			= (getTotalImageWidth() - $('imageBox').getWidth());
	var newOffset	= (getOffset() + ($('imageBox').getWidth()/1.5 ));
	
	if(newOffset > max)
	{
		scrollToEnd();
		return;
	}
	
	new Effect.Move('imageBoxInside',
						{
							x: -($('imageBox').getWidth()/1.5 ),
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue:
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
}

function getTotalImageWidth()
{	
	width = 0;
	try
	{
		for(i=0; true; i++)
		{
			width += ($('thumbnail'+(i)).getWidth()+2);
		}
	} catch(e) {
		
	}
	return width;
}

function scrollToPosition(position)
{		
	new Effect.Move('imageBoxInside',
						{
							x: position,
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue:
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
}

function scrollToEnd()
{
	if((getTotalImageWidth() - $('imageBox').getWidth()) - getOffset() == 0)
	{
		selectedGalleryPage++;
		if(selectedGalleryPage > selectedGalleryPageMax)
		{
			selectedGalleryPage		= 0;	// Wrap
		}
		selectedGalleryOffset	= 0;
		changeGallery(selectedGalleryId, selectedCategoryId, selectedGalleryOffset);
	}
	
	new Effect.Move('imageBoxInside',
						{
							x: -( (getTotalImageWidth() - $('imageBox').getWidth()) - getOffset() ),
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue:
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
}

function scrollToStart()
{	
	if(getOffset() == 0)
	{
		selectedGalleryPage--;
		if(selectedGalleryPage < 0)
		{
			selectedGalleryPage		= selectedGalleryPageMax;	// Wrap
		}
		selectedGalleryOffset	= 0;
		changeGallery(selectedGalleryId, selectedCategoryId, selectedGalleryOffset);
	}
	
	new Effect.Move('imageBoxInside',
						{
							x: ( getOffset() ),
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue:
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
}

function getOffset()
{	
	return -$('imageBoxInside').style.left.replace(/px/, '');
}

function setOffset(offset)
{	
	new Effect.Move('imageBoxInside',
						{
							x: -( offset ),
							y: 0,
							transition: Effect.Transitions.sinoidal,
							mode: 'relative',
							queue:
								{
									position: 'end',
									scope: 'global',
									limit: 1
								},
							duration: 1.0
						}
					);
}

