var timeout = null;

function initGallery()
{
	Event.observe('mainPicture', 'load', function(){ 
						
		window.clearTimeout(timeout);
		if ($('galleryIndicator').visible())
		{
			Effect.Fade('galleryIndicator', { duration: 0.1 }); 
		}
				
	});

	$$('#locationGallery li.thumb').each( function(o){
	
		Event.observe(o, 'click', selectMainPicture);
	
	}); 
	
	Event.observe($('galleryUp'), 'click', prevPicture);
	Event.observe($('galleryDown'), 'click', nextPicture);
}			
			
function selectMainPicture(e, element)
{
	var el;

	if (e == null)
	{
		el = element;
	}
	else
	{				
		el = Event.element(e);
					
		if (!el.match('li'))
		{
			el = el.up('li');
		}
	}
		
	if (!el.hasClassName('selected'))
	{
		window.clearTimeout(timeout);
	
		$$('#locationGallery li.selected').each( function(o){ o.removeClassName('selected'); });		
		el.addClassName('selected');
		$('mainPicture').src = el.getAttribute('target');
		
		timeout = window.setTimeout(function(){ Effect.Appear('galleryIndicator', { duration: 0.1 } ); }, 500);
	}
}

function prevPicture()
{
	var el = $$('#locationGallery li.selected')[0].previous('li.thumb');
	
	if (el != null)
	{
		selectMainPicture (null, el);
	}
}

function nextPicture()
{
	var el = $$('#locationGallery li.selected')[0].next('li.thumb');
	
	if (el != null)
	{
		selectMainPicture (null, el);
	}
}

Event.observe(window, 'load', initGallery);
