$(document).ready(function(){
  $('.cycle').cycle();
	startCycle();
	
  //When a category is selected, the thumbnails and images are removed from the slideshow
  $('.thumbswrapper a').click(function()
  {
		window.location.hash = $(this).attr('href');
		location.reload(true);
  });
	
	//Pause and resume the cycle when the buttons are clicked
	$('.pause').click(function(){
		$('.resume').removeClass('selected');
		$(this).addClass('selected');
		$('.image-fader').cycle('pause');
	});
	$('.resume').click(function(){
		$('.pause').removeClass('selected');
		$(this).addClass('selected');
		$('.image-fader').cycle('resume');
	});
});

function startCycle()
{
	$(document).ready(function(){
		var cat = window.location.hash.slice(1);
		if(cat==' '|cat==null|cat=='')
		{
			//If the URL hash doesn't contain a category string, start the Cycle with all slides
		}
		else
		{
			cat.toString();
			$('.thumb:contains('+cat+')').addClass('selected');
			//Because the URL hash contains a string, use this to display specific slides
			$('.image-fader li img[alt!='+cat+']').parent('li').remove();
		}
		$('.image-fader').cycle({
			fx                  :   'fade',
			pager               :   '.image-chooser',
			pagerAnchorBuilder  :   function(idx, slide)
			{
				return '<li><a href="#"><img src="' + $(slide).find('img').attr('src') + '" alt="' + slide.alt + '" /></a></li>';
			},
			after								:		function()
			{
				$(document).ready(function(){
					$('.image-chooser').scrollTo($('.activeSlide'),1000,{offset:-104});
				});
			}
		});
	});
}

