$(function(){

	$('ul.classList > li > a').bind('click', function(){
		$(this).parents('li').find('div.details').slideToggle();
	});
	
	$('#sidebar ul.features .gallery').map(function(index){

		/*
		 * Photo gallery dialog
		 */
		var photoGalleryId = 'photoGallery' + index;
		var photoGallery = $('#' + photoGalleryId);
		var photoGallerySpeed = 4;			// Time in seconds to display each slide
		var photoGalleryCarousel = null;	// jCarousel object
		var isMaskInitalized = false;
		
		// Initalize YUI dialog widget
		var photoGalleryDialog = new YAHOO.widget.Dialog(photoGalleryId, {
			modal: true,
			underlay: 'none',
			zIndex: 10000,
			visible: false
		});
		
		// Render dialog
		photoGalleryDialog.render();
			
		// Initalize slideshow carousel
		$(photoGallery).find('.carousel').jcarousel({
			scroll: 1,
			auto: 0,
			wrap: 'both',
			initCallback: function(o){
				photoGalleryCarousel = o;
			}				
		});	
		
		// Wire up slideshow controls
		$(photoGallery).find('.controls .back').bind('click', function(){
			photoGalleryCarousel.prev();
			pauseSlideshow();
		});
		$(photoGallery).find('.controls .next').bind('click', function(){
			photoGalleryCarousel.next();
			pauseSlideshow();
		});
		$(photoGallery).find('.controls .play').bind('click', function(){
			photoGalleryCarousel.next();
			playSlideshow();
		});
		$(photoGallery).find('.controls .pause').bind('click', function(){
			pauseSlideshow();
		});
		$('a.container-close').bind('click', function(){
			pauseSlideshow();
		});
									
		function playSlideshow() {
			// Re-enable automatic scrolling
			photoGalleryCarousel.options.auto = photoGallerySpeed;
			photoGalleryCarousel.startAuto();
			$(photoGallery).find('.controls .pause').show()
			$(photoGallery).find('.controls .play').hide();						
		}
		
		function pauseSlideshow() {
			// Pause slideshow by disabling automatic scrolling
			photoGalleryCarousel.options.auto = 0;
			photoGalleryCarousel.stopAuto();
			$(photoGallery).find('.controls .pause').hide()
			$(photoGallery).find('.controls .play').show();
		}
		
		$(this).find('a').bind('click', function(){
			photoGalleryDialog.show();
			playSlideshow();
			if (!isMaskInitalized) {
				// Bind click function to overlay mask to close photo gallery when clicked
				$('div.mask').bind('click', function(){
					photoGalleryDialog.hide();
					pauseSlideshow();
				});
				isMaskInitalized = true;
			}		
			return false;				
		});
			
	});
			
});
