// slideshow plugin
(function($)
{
	var top = 0,
		obj,
		timer,
		settings,
		methods = {
		
			init: function( options )
			{
				obj = this;
				
				settings = {
				  init_pause: 2000,
				  pause: 2000,
				  speed: 1800,
				  slides: obj.find('li'),
				  before_callback: function(){},
				  after_callback: function(){}
				};
				
				if ( options ) { settings = $.extend( settings, options ) }
				
				settings.slides.eq(0).addClass('top');
				
				if ( settings.slides.length > 1 )
				{
					timer = setTimeout(function()
					{
						methods.goToSlide(1);
					}, settings.init_pause );
				}
			},
			
			goToSlide: function (next, special_speed)
			{
				// stop timer
				clearTimeout(timer);
				// stop all other animations, and remove 'current' class
				settings.slides.stop().removeClass('current');
				
				var $current = settings.slides.eq(top),
					$next = settings.slides.eq(next);
				
				settings.before_callback( $current, $next );
				
				// fade out all other slides
				timer = setTimeout(function()
				{
					settings.slides.not($next).animate({
						opacity: 0
					}, (special_speed ? special_speed : settings.speed) * .45);
				}, (special_speed ? special_speed : settings.speed) * .45);
				
				// animate to next slide
				$next.css('opacity', 0).addClass('current').animate({
					opacity: 1
				}, special_speed ? special_speed : settings.speed, 'linear', function()
				{
					settings.after_callback( $current, $next );
					
					settings.slides.removeClass('top').removeClass('current');
					$next.addClass('top');
					
					timer = setTimeout(function()
					{
						top = next;
						// calculate next slide to be shown
						next = top == settings.slides.length - 1 ? 0 : top + 1;
						
						// call goToSlide on next slide
						methods.goToSlide(next);
						
					}, settings.pause);
				});
			}
		};

	$.fn.gcnySlideshow = function(method)
	{
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.gcnySlideshow' );
		}    
	};

})(jQuery);

























(function($)
{
	var $window = $(window),
		$document = $(document),
		$htmlBody = $('html,body'),
		$body = $('body'),
		body_id = $('body').attr('id'),
		slideshow_timer;
	
	// get all anchor links on the current page
	$($.grep($('a'), function(el)
	{
		return $(el).attr('href').substring(0,1) == '#';
	}))
	// and animate the scrolling when clicked
	.click(function(e)
	{
		e.preventDefault();
		var $this = $(this),
			scrollTop = 0,
			speed = 400,
			distance;
		
		if ($this.attr('href').length > 1)
		{
			distance = $($(this).attr('href')).offset().top;
			scrollTop = Math.min( distance - 20, $document.height() - $window.height() ),
			speed = Math.min(distance / 2, 1000)
		}
		
		$htmlBody.animate({
			scrollTop: scrollTop
		}, speed, 'easeOutCirc');
	});
	
	// AJAXify footer subscribe to newsletter form
	var $bubble = $('#newsletter-bubble');
	
	$bubble.css({
		opacity: 0
	}).data('css', {
		top: $bubble.css('top')
	});
	
	if ($.browser.msie && $.browser.version < 9) $bubble.addClass('ie');
	
	$('#footer form').submit(function()
	{
		var $this = $(this),
			post = {
				email: $this.find('input.email').val(),
				ajax: 1
			}
		
		$.post($this.attr('action'), post, function(data)
		{
			if ($bubble.css('display') !== 'block') $bubble.css('display', 'block');
			if ($bubble.data('timer') !== undefined) clearTimeout($bubble.data('timer'));
			
			if ($bubble.data('opacity') !== 0)
			{
				$bubble.stop().animate({
					opacity: 0,
					top: $bubble.data('css').top
				}, 50, 'linear', function()
				{
					data.success ? $bubble.removeClass('error').addClass('success') : $bubble.removeClass('success').addClass('error');
					$bubble.html(data.message);
				});
			}
			else
			{
				data.success ? $bubble.removeClass('error').addClass('success') : $bubble.removeClass('success').addClass('error');
				$bubble.html(data.message);
			}
			
			$bubble.animate({
				opacity: 1,
				top: parseFloat($bubble.data('css').top) - 30
			}, 550, 'easeInQuad', function()
			{
				$bubble.data('timer', setTimeout(function()
				{
					$bubble.animate({
						opacity: 0,
						top: $bubble.data('css').top
					}, 500, 'easeOutQuad', function()
					{
						$bubble.css('display', 'none');
					});
				}, 5000));
			});
		});
		return false;
	});
	
	/*----------------------------------------------------------------------------------------------*/
	/*                                        per-page-scripts                                      */
	/*----------------------------------------------------------------------------------------------*/
	
	if (body_id == 'browse-home')
	{
		// set up slideshow
		$('#slideshow').gcnySlideshow();
		
		// fade out other links on hover
		var $links = $('#videos-link, #mission-box li a');

		$links.hover(function(){
			$links.not(this).stop().animate({opacity:0.5},300, 'linear');
		}, function(){
			$links.stop().animate({opacity:1},300, 'linear');
		});
	}
	
	else if (body_id == 'browse-about')
	{
		var $window = $(window),
			$sidebar = $('#sidebar'),
			$to_top = $('#back-to-top'),
			to_top_opened = false;
		
		// show/hide back-to-top link when scrolling
		$to_top.css({
			display: 'block',
			position: 'fixed',
			top: '-47px',
			left: $sidebar.offset().left
		});		
		
		// reposition back-to-top link upon window resize
		$window.resize(function(){
			$to_top.css('left', $sidebar.offset().left);
		}).scroll(function()
		{
			if ( $window.scrollTop() > $sidebar.offset().top + $sidebar.outerHeight() && to_top_opened !== true )
			{
				$to_top.stop().animate({top: 0}, 300, 'easeOutCirc');
				to_top_opened = true;
			}
			else if ( $window.scrollTop() < $sidebar.offset().top + $sidebar.outerHeight() && to_top_opened !== false )
			{
				$to_top.stop().animate({top: '-47px'}, 100, 'easeOutCirc');
				to_top_opened = false;
			}
		});
	}
	
	else if (body_id == 'browse-events')
	{
		// set up Fancybox
		$('tbody td a').fancybox({padding:3});
		
		if ($.browser.msie)
		{
			$('.calendar-wrapper').addClass('ie');
			return;
		}
		
		var $wrapper = $('#cal-wrap');
		
		// load next/prev month via AJAX
		$wrapper.delegate('.calendar-heading a', 'click', function(e)
		{
			$.get($(this).attr('href'), function(data)
			{
				$wrapper.html($(data).find('#cal-wrap > *'));
				
				// set up Fancybox, again
				$('tbody td a').fancybox({padding:3});
			});
			return false;
		});
	}
	
	else if (body_id == 'browse-testimonials')
	{
		// sidebar hover effect
		$('#sidebar li').css('opacity', 1).hover(function()
		{
			$(this).siblings().stop().animate({
				opacity: 0.5
			}, 300, 'linear');
			
		}, function() {
		
			$(this).siblings().andSelf().stop().animate({
				opacity: 1
			}, 300, 'linear');
			
		});
	}
	
	else if (body_id == 'browse-contact')
	{
		// extend li's to all be the same size
		var biggest = 0;
		$('ul.staff li').each(function()
		{
			biggest = Math.max(biggest, $(this).height());
		})
		.css('height', biggest);
	}
	
	else if (body_id == 'galleries-photo' || body_id == 'galleries-facility' || body_id == 'galleries-video')
	{
		var $slideshow = $('#slideshow'),
			$thumbs = $('a.thumb'),
			$caption = $('#caption'),
			slideshow_snippet = [],
			speed = 1800,
			pause = 4000,
			init_pause = 2000,
			captions = [];
		
		// create list of enlarged images
		$thumbs.each(function()
		{
			slideshow_snippet.push('<li><img src="' + $(this).attr('href') + '" /></li>');
			captions.push( $(this).attr('title') );
			$caption.text(captions[0]);
		});
		
		// set up pagination
		if ($thumbs.length / 8 > 1)
		{
			// set up variables
			var $thumbs_pages,
				$thumbs_links,
				pagination_html = [],
				$thumbs_list = $('.thumbs > li');
			
			for (var i = 0; i < $thumbs.length; i += 8)
			{   
				// wrap each set of 8 in a div
				$thumbs_list.slice(i, i+8).wrapAll('<div class="page-' + i / 8 + '" />');
				
				// create pagination links
				pagination_html.push('<li><a href="#">' + (i / 8 + 1) + '</a></li>');
			}
			
			// add pagination links and attach function to change pages on click
			$('#thumbs-pages-links').html( pagination_html.join('') ).delegate('a', 'click', function(e)
			{
				e.preventDefault();
				goToPage( $(this).text() - 1 );
				
				if (body_id != 'galleries-video')
				{
					$thumbs_pages.eq( $(this).text() - 1 ).find('a:first').click();
				}
			});
		
			$thumbs_pages = $('.thumbs > div');
			$thumbs_links = $('#thumbs-pages-links a');
			
			function goToPage(page_num)
			{
				$thumbs_pages.not(':eq(' + page_num + ')').hide();
				
				$thumbs_pages.eq(page_num).show();
				
				$thumbs_links.removeClass('active').eq(page_num).addClass('active');
			}
			goToPage(0)
		}
		
		if (body_id != 'galleries-video')
		{
			$slideshow.html(slideshow_snippet.join('')).gcnySlideshow({
				init_pause: init_pause,
				speed: speed,
				pause: pause,
				before_callback: function( $current, $next )
				{
					$caption.text(captions[this.slides.index($next)]);
					
				}
			});
			
			$thumbs.click(function(e)
			{
				e.preventDefault();
				$slideshow.gcnySlideshow('goToSlide', $thumbs.index(this), 200);
			});
		}
		else
		{
			$slideshow.html('<iframe frameborder="0" src="' + $thumbs.eq(0).attr('href') + '"></iframe>');
			
			$thumbs.click(function(e)
			{
				e.preventDefault();
				$slideshow.html('<iframe frameborder="0" src="' + $(this).attr('href') + '"></iframe>').find('iframe');
				$caption.text( $(this).attr('title') );
			});
		}
	}
})(jQuery);
