

	var currentGallery = null;
	var galleryTimeout = null;

	$(document).ready(function() {

		// load up a random gallery
		// randomGallery();

		// attach events to homepage gallery controls
		$('#studentwork-content ul.subnav li a').click( function() {
			var gal = $(this).attr('class');
			switchGallery(gal);
			return false;
		});

		$('#sw-next').click(nextPhoto);
		$('#sw-prev').click(prevPhoto);
		$('#sw-more').click(gotoMajor);


		// get the slideshow rolling
		galleryTimeout = setTimeout('nextPhoto()', 3000);
		$('ul.swgallery').hide().children('li').hide();
		$('ul#' + currentGallery).show().children('li:first').addClass('selected').show();


		//
		// On click of a gallery grid item, load the carousel.
		//
		$('ul.gridview li').css('cursor', 'pointer').click( function() {
			var img = $(this).attr('id').split('_')[1];
			var gallery = $(this).parent('ul').attr('title');

			loadCarouselGallery(gallery, img);
		});

	});


	//
	// switchGallery
	//
	// Given a particular gallery to load, if photos exist in the DOM, show them,
         // otherwise, load via ajax and start the slideshow.
	//
	// @param gal - code of the gallery to show/laod
	//
	function switchGallery(gal) {
		if(galleryTimeout) clearTimeout(galleryTimeout);
		$('ul.swgallery').hide();
		if( $('ul#' + gal).length ) {
			$('ul#' + gal).show().children('li:first').addClass('selected').show();
			currentGallery = gal;
			galleryTimeout = setTimeout('nextPhoto()', 3000);
		} else {
			var url = '/gallery/load-student-work';
			url += '?gallery=' + gal;
			dynamItLoadData(url, gal, function(data) {
				$('#studentwork-art').append(data).show();
				currentGallery = gal;
				galleryTimeout = setTimeout('nextPhoto()', 3000);
				$('ul.swgallery').hide()
				$('ul#' + gal).show().children('li').hide().eq(0).addClass('selected').show();
			});
		}
		$('#studentwork-content ul.subnav').children('li').removeClass('here');
		$('#studentwork-content ul.subnav').children('li').children('a').filter('.' + gal).parent().addClass('here');

		return false;
	}


	//
	// randomGallery
	//
	// select a random gallery and initiate its loading
	//
	function randomGallery() {
		var numGalleries = $('#studentwork-content ul.subnav').children('li').length;
		if( numGalleries > 0 ) {
			var galleryIndex = Math.floor( Math.random() * (numGalleries) );
			var randGallery = $('#studentwork-content ul.subnav').children('li').eq( galleryIndex ).children('a').attr('class');

			switchGallery(randGallery);
		}
	}


	//
	// nextPhoto
	//
	// if we have a next photo in our list fade to it
	// if not, load the next gallery
	//
	function nextPhoto() {
		if(galleryTimeout) clearTimeout(galleryTimeout);
		$photo = $('ul#' + currentGallery).children('li.selected');
//alert('next');
		if( $photo.next('li').length ) {

			$next = $photo.next('li');

			$photo.removeClass('selected').fadeOut('slow');
			$next.addClass('selected').fadeIn('slow');
			galleryTimeout = setTimeout('nextPhoto()', 3000);
		} else {

			galleryTimeout = setTimeout('randomGallery()', 5000);
		}
		return false;
	}


	//
	// prevPhoto
	//
	// find prev photo (with wrap around) in our list and fade to it
	//
	function prevPhoto() {
		if(galleryTimeout) clearTimeout(galleryTimeout);
		$photo = $('ul#' + currentGallery).children('li.selected');
		$prev = ( $photo.prev('li').length ) ? $photo.prev('li') : $photo.parent().children('li:last-child');

		$photo.removeClass('selected').fadeOut('slow');
		$prev.addClass('selected').fadeIn('slow');
		galleryTimeout = setTimeout('nextPhoto()', 3000);
		return false;
	}


	//
	// gotoMajor
	//
	// homepage link to go to the major for the currently displayed gallery
	//
	function gotoMajor() {
		var url = '/programs-of-study/majors/' + currentGallery + '/';
		location.href = url;
		return false;
	}



	var carouselImgWidth = 546;
	var carouselOffset = (1000 - carouselImgWidth) / 2;
	var carouselPosition = 0;
	var carouselIndex = 0;

	//
	// loadCarousel
	//
	// if the photos for this gallery exist in our DOM, slide down the carousel and show them
	// if not, load them via ajax and then display the carousel
	//
	// @param gallery - the code of the gallery to load/show
	// @param img - the image that we should start on.
	//
	function loadCarouselGallery(gallery, img) {
		if( $('#carousel').children('.' + gallery).length ) {
			if($('li#photofull_' + img).length) {
				carouselIndex = $('#slider ul#sliderview').children('li').index( $('li#photofull_' + img) );
			} else if($('#slider ul#sliderview').children('li').length >= img) {
				carouselIndex = img-1;
			}
			else {
				carouselIndex = 0;
			}
			var slideTo = (carouselIndex * carouselImgWidth) - carouselOffset;
			doSlideCarousel(slideTo);
		} else {
			var url = '/gallery/load-carousel';
			url += '?gallery=' + gallery;
			dynamItLoadData(url, gallery, function(data) {
				$('#carousel').html(data);
				if($('li#photofull_' + img).length) {
					carouselIndex = $('#slider ul#sliderview').children('li').index( $('li#photofull_' + img) );
				} else if($('#slider ul#sliderview').children('li').length >= img) {
					carouselIndex = img-1;
				}
				else {
					carouselIndex = 0;
				}
				var slideTo = (carouselIndex * carouselImgWidth) - carouselOffset;
				doSlideCarousel(slideTo);
			});
		}

		$('#carousel').slideDown('slow');
	}



	//
	// slideCarousel
	//
	// scroll the contents of the carousel window
	//
	// @param direction - +1 for right/next, -1 for left/prev
	//
	function slideCarousel(direction) {
		carouselIndex += direction;
		if( carouselIndex < 0 ) {
			carouselIndex = 0;
			return;
		}

		var max = $('#slider ul#sliderview').children('li').length - 1;
		if( carouselIndex > max ) {
			carouselIndex = max;
			return;
		}

		doSlideCarousel(carouselPosition + (direction * carouselImgWidth))
	}


	//
	// doSlideCarousel
	//
	// execute the carousel slide animation.
	//
	// @param val - final left offset of the slide.
	//
	function doSlideCarousel(val) {
		val = -1 * val;
		$('#slider ul#sliderview').children('li').children('div').slideUp('fast');
		$('ul#sliderview').stop().animate(
			{ left: val + 'px' },
			750,
			'swing',
			showCarouselDesc );
		carouselPosition = -1 * val;
	}


	//
	// showCarouselDesc
	//
	// display the description for the active carousel item
	//
	function showCarouselDesc() {
		var $li = $('#slider ul#sliderview').children('li').eq(carouselIndex);
		$li.children('div').slideDown('slow');
	}