function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1600, function() {
            $active.removeClass('active last-active');
        });
}

function galleryFader() {

	$('#gallery .frame img').not(':first-child').css('display', 'none')

	$('#gallery .nav a').click(function(){
		var target = $(this).attr('href');
		if( $(target).is(':visible') ) {
			return false;
		} else {
			$('#gallery .frame img:visible').fadeOut();
		}
		$(target).fadeIn('slow');
		return false;
	});
	
}

$(document).ready(function() {

 	if ($('#slideshow').length > 0) {
		$('#slideshow img:first-child').addClass('active');
		setInterval( "slideSwitch()", 3500 );
	}

 	if ($('#gallery .nav').length > 0) {
		galleryFader();
	}
	
	if ($('body#collections').length > 0) {
 		$('#main li.article:nth-child(4n)').addClass('end');
	}
	
	// hover fade effects
	$('#logo')
        .removeClass('highlight')	
    	.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function () {
				// on hover
				$span.stop().fadeTo(500, 1);
			}, function () {
				// off hover
				$span.stop().fadeTo(500, 0);
			});
		});	
	
});
