jQuery(function(){
	for (var i = 0; i < galleryImages.length; i++)
	{
		jQuery('#gallery').append('<a href="' + galleryImages[i] + '" title="' + galleryTitles[i] + '" style="display:none;" class="lightbox"></a>');
	}
	
	jQuery('.lightbox').lightBox(lightBoxConfig);
	jQuery('#gallery a.previousImage, #gallery a.nextImage').unbind('click').click(pagination);
	jQuery('#mainImage').click(function(e){
		e.preventDefault();
		jQuery('.lightbox[href="' + jQuery(this).attr('href') + '"]').click();
	});
	jQuery('#mainImage img').load(function(e){
		var height = Math.max(jQuery(this).height(), jQuery('.performance .head .aside > div').height() + 20);
		jQuery('#gallery, .performance .head').height(height);
		jQuery('#gallery').height(jQuery(this).height());
	});
	
	jQuery('h5.handle').click(function(){
		jQuery(this).toggleClass('open');
		jQuery(this).next('.section').toggleClass('open');
	});
});

function pagination(e)
{
	e.preventDefault();
	var src = jQuery(this).attr('href');
	jQuery('#mainImage').attr('href', src);
	jQuery('#mainImage img').attr('src', src.replace('fl/', 'fl/th2/'));
	
	if (firstp(galleryImages, src))
	{
		jQuery('#gallery a.previousImage').hide();
	}
	else
	{
		jQuery('#gallery a.previousImage').attr('href', previousItem(galleryImages, src));
		jQuery('#gallery a.previousImage').show();
	}
	
	if (lastp(galleryImages, src))
	{
		jQuery('#gallery a.nextImage').hide();
	}
	else
	{
		jQuery('#gallery a.nextImage').attr('href', nextItem(galleryImages, src));
		jQuery('#gallery a.nextImage').show();
	}
}

function firstp(array, item)
{
	return array[0] == item;
}

function lastp(array, item)
{
	return array[array.length - 1] == item;
}

function previousItem(array, item)
{
	var r = null;
	for (var i = 0; i < array.length;)
	{
		r = array[i];
		if (array[++i] == item)
		{
			return r;
		}
	}
}

function nextItem(array, item)
{
	var r = null;
	for (var i = 0; i < array.length; i++)
	{
		if (array[i] == item)
		{
			return array[++i];
		}
	}
}