$(function () {
	var popupGalShown = false;
	var popupImgShown = false;
	var popupConShown = false;
	$.panelGalShown = false;
	$.panelThumbShown = false;
	var navigation = $('#navigation');
	var actions = $('#actions');
	
	actions.hide();
	
	//Main panel hover
	navigation.hover(
		function() {
			navigation.stop().animate({ width: "534px"}, 600);
			actions.stop(true, true).delay(200).fadeIn(400);
		},
		function() {
			if ($.panelGalShown || $.panelThumbShown) {
				return;
			} else {
				navigation.stop().animate({ width: "355px" }, 600);
				actions.stop(true, true).fadeOut(200);
			}
		}
	);
	
	//Contact bubble hover
	$('#contact').hover(
		function() {
			if (popupConShown) {
				return;
			} else {
				popupConShown = true;
				
				$('#contactBubble').stop().css({
					display: 'block',
					'background-position': '0 0'
				})
				.animate({
					bottom: 107,
					opacity: 1
				}, 300);
			}
		},
		function() {
			$('#contactBubble').stop().css({
				'background-position': '0 -40px'
			})
			.animate({
				bottom: 98,
				opacity: 0
			}, 500, function(){popupConShown = false;});
		}
	);
	
	//Gallery bubble hover
	$('#galleryMeta .gallerySwap').hover(
		function() {
			if (popupGalShown || $.panelGalShown) {
				return;
			} else {
				popupGalShown = true;
				
				$('#galleryBubble').stop().css({
					display: 'block',
				})
				.animate({
					bottom: 105,
					opacity: 1
				}, 300);
			}
		},
		function() {
			if ($.panelGalShown) {
				return;
			} else {
				$('#galleryBubble').stop().animate({
					bottom: 96,
					opacity: 0
				}, 300, function(){popupGalShown = false;});
			}
		}
	);
	
	//Image thumbnail bubble hover
	$('#galleryMeta .imageCount').hover(
		function() {
			if (popupImgShown || $.panelThumbShown) {
				return;
			} else {
				popupImgShown = true;
				$('#copyright').fadeOut(300);
				$('#imageCountBubble').stop().css({
					display: 'block',
				})
				.animate({
					bottom: 5,
					opacity: 1
				}, 300);
			}
		},
		function() {
			if ($.panelThumbShown) {
				return;
			} else {
				$('#imageCountBubble').stop().animate({
					bottom: 15,
					opacity: 0
				}, 300, function(){popupImgShown = false;});
				$('#copyright').fadeIn(300);
			}
		}
	);
	
	// Thumbnail navigation panel
	$('#galleryMeta .imageCount').click(function(event) {
		if ($(this).hasClass("active")) {
			$.panelThumbShown = false;
			$(this).removeClass("active");
			$('#navigation').removeClass("expanded");
			$('#thumbnailNav').fadeOut(300);
			$('#copyright').fadeIn(300);
		} else {
			//check to see if other panel is open
			if ($('#galleryNav').is(':visible')) {
				$.panelGalShown = false;
				$('#galleryMeta .gallerySwap').removeClass("active");
				$('#galleryNav').fadeOut(300);
			}
			
			$.panelThumbShown = true;
			$(this).addClass("active");
			$('#navigation').addClass("expanded");
			
			// handle close animation for bubble
			$('#imageCountBubble').stop().animate({
				bottom: 15,
				opacity: 0
			}, 300, function(){popupImgShown = false;});
			
			$('#copyright').fadeOut(200);
			$('#thumbnailNav').fadeIn(300);
		}
	});
	
	//Gallery navigation panel
	$('#galleryMeta .gallerySwap').click(function(event) {
		if ($(this).hasClass("active")) {
			$.panelGalShown = false;
			$(this).removeClass("active");
			$('#navigation').removeClass("expanded");
			$('#galleryNav').fadeOut(300);
		} 
		else {
			//check to see if other panel is open
			if ($('#thumbnailNav').is(':visible')) {
				$.panelThumbShown = false;
				$('#galleryMeta .imageCount').removeClass("active");
				$('#thumbnailNav').fadeOut(300);
				$('#copyright').fadeIn(300);
			}
			
			$.panelGalShown = true;
			$(this).addClass("active");
			$('#navigation').addClass("expanded");
			
			//handle close animation for bubble
			$('#galleryBubble').stop().animate({
				bottom: 96,
				opacity: 0
			}, 300, function(){popupGalShown = false;});
			
			$('#galleryNav').fadeIn(300);
		}
	});
	
	
	//For usability, close panels by clicking virtually anywhere
	$('#guard').click(function(event) {
		if ($('#thumbnailNav').is(':visible')) {
			$.panelThumbShown = false;
			$('#galleryMeta .imageCount').removeClass("active");
			$('#navigation').removeClass("expanded");
			$('#thumbnailNav').fadeOut(200);
			$('#copyright').fadeIn(300);
			
			//handle close animation for navigation
			navigation.stop().animate({ width: "355px" }, 600);
			actions.stop(true, true).fadeOut(200);
		}
		else if ($('#galleryNav').is(':visible')) {
			$.panelGalShown = false;
			$('#galleryMeta .gallerySwap').removeClass("active");
			$('#navigation').removeClass("expanded");
			$('#galleryNav').fadeOut(300);
			
			//handle close animation for navigation
			navigation.stop().animate({ width: "355px" }, 600);
			actions.stop(true, true).fadeOut(200);
		}
	});
});
