/*-----------------------------------------------------------------------------
	Gallery
-----------------------------------------------------------------------------*/
	
	$(document).ready(function() {
		$('.gallery').each(function() {
			var gallery = $(this), mode = 'videos';
			var scroll = gallery.find('.gallery-thumbs');
			var thumbs = scroll.find('.gallery-thumb');
			var buttonPrev = gallery.find('.gallery-previous');
			var buttonNext = gallery.find('.gallery-next');
			
			// Show a playlist:
			var showVideo = function(playlist) {
				var emp = new embeddedMedia.Player();
				
				$('#video-player embed, #video-player img').remove();
				
				emp.setWidth('445');
				emp.setHeight('286');
				emp.setDomId('video-player');
				emp.setPlaylist('http://www.bbc.co.uk/musictv/maestro/emp/' + playlist);
				emp.set('config_settings_skin', 'black');
				emp.set('autoplay', 'true');
				emp.set('autoPlay', 'true');
				emp.set('auto_play', 'true');
				emp.write();
			}
			
			var showImage = function(source) {
				$('#video-player embed, #video-player img').remove();
				$('#video-player').append('<img src="' + source + '" alt="" width="445" height="251" />');
			}
			
			// Switch between videos and images:
			var switchMode = function(from, to) {
				if (mode == to) return false;
				
				scroll.find('.gallery-group-' + from).fadeOut(250, function() {
					$(this).removeClass('current');
					scroll
						.find('.gallery-group-' + to + ':first')
						.fadeIn(750).addClass('current');
				});
				
				mode = to;
				return false;
			}
			
			// Find default mode:
			if (gallery.hasClass('mode-videos')) mode = 'videos';
			if (gallery.hasClass('mode-images')) mode = 'images';
			
			$('#gallery-mode-videos').click(function() {
				return switchMode('images', 'videos');
			});
			
			$('#gallery-mode-images').click(function() {
				return switchMode('videos', 'images');
			});
			
			// Track thumbnail clicks:
			thumbs.unbind('click').click(function() {
				var description = $(this).attr('alt');
				
				thumbs.removeClass('selected');
				$(this).addClass('selected');
				
				// Update description:
				gallery.find('p').remove();
				gallery.append('<p>' + description + '</p>');
				
				// Show content:
				if (mode == 'videos') showVideo($(this).parents('span').attr('title'));
				if (mode == 'images') showImage($(this).parents('a').attr('href'));
				
				return false;
			});
			
			// Show previous thumbnails:
			buttonPrev.unbind('click').click(function() {
				var group = scroll.find('.gallery-group-' + mode + '.current');
				var prev = group.prev('.gallery-group-' + mode);
				
				if (!prev.length) return false;
				
				group.fadeOut(250, function() {
					group.removeClass('current');
					prev.fadeIn(750).addClass('current');
				});
				
				return false;
			});
			
			// Show next thumbnails:
			buttonNext.unbind('click').click(function() {
				var current = scroll.find('.gallery-group-' + mode + '.current');
				var next = current.next('.gallery-group-' + mode);
				
				if (!next.length) return false;
				
				current.fadeOut(250, function() {
					current.removeClass('current');
					next.fadeIn(750).addClass('current');
				});
				
				return false;
			});
		});
	});
	
/*---------------------------------------------------------------------------*/