$(document).ready(function(){
	
	// Validate forms
	$('.formNewsletter').validate();
	$('.formNewsletterFooter').validate();
	$('.formNewsletterMiniatures').validate();
	
	// Smooth scroll
	$('.lnkReadMore a, .lnkTop a').click(function(){
		var $link = $(this).attr('href');
		$('html, body').stop().animate({scrollTop: $($link).offset().top - 20}, 1000);
		return false;
	});
	
	if ($('body').is('#pageIndex')) {
				
		$('#head, .lnkFeatureHome, .backgrounds').append('<div class="mask"></div>'); // Fading opacity bg for IE *sulk*
		$('#pageIndex #container #main #content .backgrounds').appendTo('#container');
		
		$(window).load(function() {
			
			// Full-scale backgrounds
			var theWindow = $(window),
			$bg = $('.image1'),
			aspectRatio = $bg.width() / $bg.height();
			
			function resizeBg() {
				if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
					$bg.removeClass().addClass('bgheight');
				} else {
					$bg.removeClass().addClass('bgwidth');
				}
			}
			
			theWindow.resize(function() {
				resizeBg();
			}).trigger("resize");
			
			$('.backgrounds').css('visibility', 'visible');
			$('#pageIndex #head, #pageIndex #head .inner').delay(1400).fadeIn(800);
			$('#pageIndex .lnkFeatureHome, #pageIndex .lnkFeatureHome .inner').delay(2000).fadeIn(800);
			$('.backgrounds .mask').fadeOut(900);
		
		});
		
		// Homepage feature slideshow
		/*
		$('#pageIndex #container #main #content .backgrounds').appendTo('#container').find(':not(.image1)').each(function() {$(this).css('opacity', 0);});
		$(window).load(function() {
			$('.backgrounds').cycle({delay: 1000, speed: 1800, timeout: 7500, slideExpr: 'div'});
		});*/
	
		// Toggle footer on homepage
		$('#pageIndex .lnkSkipToFooter').appendTo('#head .inner').text('Show footer').show().bind('click', function() {
			if ($('#foot').is(':visible')) {
				$(this).text('Show footer').removeClass('lnkHideFooter');
			} else {
				$(this).text('Hide footer').addClass('lnkHideFooter');
			}
			$('#foot').toggle();
		});
		
	} // If pageIndex
	
	if ($('body').is('#pageGallery')) {
		// Gallery filter form	
		if($('#fsTags input[type=checkbox]:checked').size() != 0) {
			$('#formFilter #fsTags').show();
		}
		$('#formFilter #fViewFiltered').click(function() {$('#formFilter #fsTags').fadeIn();}); // Show categories
		$('#formFilter #fViewAll').click(function() {
			window.location.href = window.location.href.replace(/\/gallery\/([^\/]+)\/?.*$/i, "/gallery/$1");
		}); // Hide categories
	
		// Gallery detail slide-up
		$('.listGallery li').hover(
			function() {
				if($(this).data('slide_timer') != null)
					clearTimeout($(this).data('slide_timer'));
				$(this).find('.detail').slideDown('400');
			}, 
			function() {
				$(this).data('slide_timer', setTimeout("$('.listGallery li:nth("+ $('.listGallery li').index($(this)) +") .detail').slideUp('400');", 736));
			}
		);
		
		// Print detail page
		$('#navActions .actPrint a').click(function() {
			window.print();
			return false;
		});
		
		// tag filter
		$('#fsTags input').live('click', function(e){
			url = window.location.href.replace(/\/gallery\/([^\/]+)\/?.*$/i, "/gallery/$1/");
			url = url + $.map($(this).closest('form').find('input[type=checkbox]:checked'), function(tag){
				return $(tag).val();
			}).join('/');
			window.location.href = url;
		});
		
	} // If pageGallery
	
	if ($('body').is('#pageGalleryDetail')) {
		
		Shadowbox.init({skipSetup:true});
		Shadowbox.setup($('.actEnlarge a'));
		
		// Print page
		$('.actPrint').click(function() {
			window.print();
			return false;
		});
		
		// mediaelementjs player
		if($('audio').length > 0) {
			$('audio').mediaelementplayer();
		}
		
		// Detail image swap
		$('#sectDetail #still2').css('visibility', 'hidden'); // Hide 2nd image
		$('#navStills a').click(function() {
			var altID = $(this).attr('href'); // Get href (which is also large image ID)
			if ($(altID).css('visibility') == 'hidden') { // If image is not current
				$('#navStills a').removeClass('selected'); // Remove selected styles
				$('#sectDetail .stills .still').css({visibility: 'hidden'}); // Fade out existing image
				$(altID).css({opacity: 0.0, visibility: 'visible'}).animate({opacity: 1.0}, 700); // Fade in target image
				$(this).addClass('selected'); // Add selected class to anchor image
				$('.actEnlarge a').attr('href',$(altID + ' a').attr('href')); // Update enlarge link
				Shadowbox.setup($('.actEnlarge a')); // Re-run shadowbox on new
			}
			return false;
		});
	} // If PageGalleryDetail

});
