
	$(document).ready(function(){
		
		/* --------------------------------------------------
		Jumbotron Photo Gallery
		-------------------------------------------------- */
		var animating = false;
		var jumbotron = $('div#jumbotron');
		var photoList = $('div#jumbotron ul');
		var photos = $('div#jumbotron ul li');
		var isSingleDinner = $(document.body).hasClass('single-dinner');
		var currentDinnerIndex = ($('div#checklist ul li.current').is('li')) ? ($('div#checklist ul li').index($('div#checklist ul li.current')) + 1) : (0);
		var activePhoto = (isSingleDinner) ? (currentDinnerIndex) : (photos.length - 2);
		var photoWidth = 480;
		var prevButton = $('div#jumbotron div#leftPhotoShade a');
		var nextButton = $('div#jumbotron div#rightPhotoShade a');
		var checkPagination = function(){
			if (activePhoto == 0) {
				prevButton.addClass('disabled');
				nextButton.removeClass('disabled');
			} else if (activePhoto >= photos.length - 1) {
				nextButton.addClass('disabled');
				prevButton.removeClass('disabled');
			} else {
				prevButton.removeClass('disabled');
				nextButton.removeClass('disabled');
			}
		};
		var centerPhotos = function(){
			photoList.animate({ left: ((230 + (activePhoto * photoWidth) - (jumbotron.outerWidth() / 2)) * -1) }, {
				duration: 500,
				queue: false
			});
		};
		var hideTitle = function(){
			$(photos[activePhoto]).find('span.title').animate({ left: -600 }, {
				duration: 350
			});
		};
		var showTitle = function(){
			$(photos[activePhoto]).find('span.title').animate({ left: 0 }, {
				duration: 650
			});
		};
		if (isSingleDinner) {
			prevButton.attr('href', photoList.find('li:eq(' + (currentDinnerIndex - 1) + ') a').attr('href'));
			nextButton.attr('href', photoList.find('li:eq(' + (currentDinnerIndex + 1) + ') a').attr('href'));
		}
		centerPhotos();
		showTitle();
		$(window).resize(centerPhotos);
		$('div#jumbotron ul, div#jumbotron p').delay(550).fadeIn(1500);
		
		nextButton.click(function(){
			if (!isSingleDinner) {
				if (activePhoto < photos.length - 1 && !animating) {
					animating = true;
					hideTitle();
					activePhoto += 1;
					checkPagination();
					showTitle();
					photoList.animate({ left: ('-=' + photoWidth) }, {
						duration: 650,
						queue: false,
						complete: function(){
							animating = false;
						}
					});
				}
				return false;
			}
		});
		
		prevButton.click(function(){
			if (!isSingleDinner) {
				if (activePhoto >= 1 && !animating) {
					animating = true;
					hideTitle();
					activePhoto -= 1;
					checkPagination();
					showTitle();
					photoList.animate({ left: ('+=' + photoWidth) }, {
						duration: 650,
						queue: false,
						complete: function(){
							animating = false;
						}
					});
				}
				return false;
			}
		});
		
		/* --------------------------------------------------
		Post Gallery
		-------------------------------------------------- */
		$('div.post-gallery').each(function(index, div){
			var div = $(div);
			var div_p = $(div).find('div.image p');
			var div_a = div_p.find('a');
			var div_img = div_p.find('img');
			var div_span = div_p.find('span');
			var div_thumbs_ul = $(div).find('div.thumbs ul');
			var div_thumbs_ul_li = div_thumbs_ul.find('li');
			var thumb_width = 91;
			var selectThumb = function(element){
				var thumb = $(element);
				var thumb_li = $(element).parent();
				var thumb_img = thumb.children();
				
				if (!thumb_li.hasClass('current')) {
					thumb.parent().addClass('current').siblings().removeClass('current');
				
					// Fade out the image and the title
					div_p.animate({
						opacity: 0
					}, 500, function(){
						
						// Change the image src, width, & height
						div_img.attr({ src: thumb_img.attr('m_src') }).animate({ width: thumb_img.attr('m_width'), height: thumb_img.attr('m_height') });
					
						// Change the image hyperlink
						div_a.attr({
							href: thumb.attr('href'),
							title: thumb_img.attr('alt')
						})
					
						// Change the image title
						div_span.html(thumb_img.attr('alt'));
					
						// Fade in the image and the title
						div_p.delay(1250).animate({
							opacity: 1
						});
					});	
				}
			};
			
			div.find('div.thumbs ul li a').click(function(){
				selectThumb(this);
				return false;
			});
			
			div_a.click(function(){
				if (!div_thumbs_ul.find('li.current').is('li:last-child')) {
					selectThumb(div_thumbs_ul.find('li.current + li a'));
				} else {
					selectThumb(div_thumbs_ul.find('li:eq(0) a'));
				}
				return false;
			});
			
			div.find('div.thumbs p.previous a').click(function(){
				if (parseInt(div_thumbs_ul.css('marginLeft')) < 0) {
					div_thumbs_ul.animate({ marginLeft: ('+=' + (thumb_width * 2)) }, {
						duration: 500,
						queue: false
					});
				}
				return false;
			});

			div.find('div.thumbs p.next a').click(function(){
				if (parseInt(div_thumbs_ul.css('marginLeft')) > (thumb_width * (div_thumbs_ul_li.length - 5) * -1)) {
					div_thumbs_ul.animate({ marginLeft: ('-=' + (thumb_width * 2)) }, {
						duration: 500,
						queue: false
					});
				}
				return false;
			});
		});
		
	});
