$(document).ready(function() {
	$('.biglink, .calendarCell').biggerlink();

	$('#topBar form .input').focus(function() {
		$('#topBar .submit').attr('src', '/images/topbar-arrow-active.gif');
		return false;
	});
	
	$('#topBar form .input').blur(function() {
		$('#topBar .submit').attr('src', '/images/topbar-arrow.gif');
		return false;
	});

    $('.slideshow').cycle({
		fx: 'fade',
		next: '.slideshow_controls .slideshow_next', 
	    prev: '.slideshow_controls .slideshow_previous',
	    before: beforeSlide
	});
	
    $('.slideshow2').cycle({
		fx: 'fade',
		next: '.slideshow2_controls .slideshow_next', 
	    prev: '.slideshow2_controls .slideshow_previous',
	    before: beforeSlide2
	});

    $('.spotlightItems').cycle({
		fx: 'fade',
		next: '.slideshow_controls .slideshow_next', 
	    prev: '.slideshow_controls .slideshow_previous',
	    before: beforeSlide,
	    cleartype: true,
	    cleartypeNoBg:  true
	});
	
	$('.slideshow_control').click(function() {
	
		if($(this).hasClass('active'))
		{
			// make sure the correct slideshow is restarted by checking the parent class
			if($(this).parents().is(".slideshow2_controls"))
			{$('.slideshow2').cycle('resume');}
			else
			{
				if($('.slideshow').length > 0)
				{$('.slideshow').cycle('resume');}
				else
				{$('.spotlightItems').cycle('resume');}

			}
			$(this).removeClass('active');
			$(this).text('Pause');
		}
		else
		{
			if($(this).parents().is(".slideshow2_controls"))
			{$('.slideshow2').cycle('pause');}
			else
			{
				if($('.slideshow').length > 0)
				{$('.slideshow').cycle('pause');}
				else
				{$('.spotlightItems').cycle('pause');}
			}
			$(this).addClass('active');
			$(this).text('Resume');
		}
		return false;
	});
	
	$('.slideshow_controls a, .slideshow2_controls a').click(function() {
		parentIsSlideshow2 = $(this).parents().is(".slideshow2_controls");
		setTimeout(function() {
			if(parentIsSlideshow2)
			{
				$('.slideshow2 .caption').fadeIn();
			}
			else
			{
				$('.slideshow .caption').fadeIn();
			}

		}, '500');
		return true;
	});
	
	var usingControls = false;
	
	$('.slideshow_controls, .slideshow2_controls').hover(
		function() {
			usingControls = true;
		},
		function() {
			usingControls = false;
		}
	);
	
	$('.slideshow_wrapper, .spotlightWrapper').mouseenter( function() {
		$('.caption', this).fadeIn();
		$('.slideshow, .slideshow2, .spotlightItems', this).cycle('pause');
	});

	$('.slideshow').mouseleave( function() {
		// jQuery gets confused and thinks we've left when the cursor is on the next/prev/pause controls, so overrule that
		setTimeout(function() {
			if(usingControls == false)
			{
				// resume the slideshow on mouse out, unless it has been manually paused
				if(! $('.slideshow_controls .slideshow_control').hasClass('active'))
				{$('.slideshow').cycle('resume');}

				$('.slideshow .caption').fadeOut();
			}
		},
		'100');
	});
	$('.slideshow2').mouseleave( function() {
		// jQuery gets confused and thinks we've left when the cursor is on the next/prev/pause controls, so overrule that
		setTimeout(function() {
			if(usingControls == false)
			{
				// resume the slideshow on mouse out, unless it has been manually paused
				if(! $('.slideshow2_controls .slideshow_control').hasClass('active'))
				{$('.slideshow2').cycle('resume');}

				$('.slideshow2 .caption').fadeOut();
			}
		},
		'100');
	});
	$('.spotlightWrapper').mouseleave( function() {
		// jQuery gets confused and thinks we've left when the cursor is on the next/prev/pause controls, so overrule that
		setTimeout(function() {
			if(usingControls == false)
			{
				// resume the slideshow on mouse out, unless it has been manually paused
				if(! $('.slideshow_controls .slideshow_control').hasClass('active'))
				{$('.spotlightItems').cycle('resume');}		
				$('.slideshow .caption').fadeOut();
			}
		},
		'100');
	});
	
	function beforeSlide() {
		$('.slideshow .caption').css('display', 'none');
	}
	function beforeSlide2() {
		$('.slideshow2 .caption').css('display', 'none');
	}
	
});
