/*
######################################################################################
######                                                                        #######
######     EXHIBITION SCRIPTS			                                     #######
######                                                                      #######
##################################################################################
*/

	var Exhibition = new Object();


/********************************************************************************/
/***** EXHIBITION FUNCTIONALITY ************************************************/
/******************************************************************************/

/***** OPEN THE ZOOM WINDOW *****/

	Exhibition.Open = function(popupId) {
		var bodyTag = document.getElementsByTagName('body')[0];
			bodyTag.appendChild($('popup_area'));

	// Open the popup
		popupActive = popupId;
		
	// Reveal the outermost area of the popup
		$('popup_area').show();
		$('popup_area').style.width		= '100%';
		$('popup_area').style.height	= getTotalHeight() + 'px';
		
	// Begin to show the faded popup background
		new Effect.Appear('popup_bg', { beforeStart: function() {
		// Set the background width and height
			$('popup_bg').style.width	= '100%';
			$('popup_bg').style.height	= getTotalHeight() + 'px';
			
			new Effect.Appear('popup_content', { beforeStart: function() {
			// Centre the content area in the window
			// Timeout allows the element a chance to activate, otherwise positioning would be impossible
				if($('audio_tour')) {
					$('audio_tour').hide();
				}
				setTimeout('Exhibition.Content()', 50);
			},
			duration: 0.5, from: 0, to: 1 })
		},
		duration: 0.5, from: 0, to: 0.999, afterFinish: function() {
			// If anywhere outside of the content area is clicked, close the popup
				Event.observe($('popup_bg'), 'click', Exhibition.BgClose);
			}
		});
		popupState = 'open';
	}
	
/***** CLOSE THE POPUP *****/

	Exhibition.Close = function() {
		new Effect.Fade('popup_content', { beforeStart: function() {
			new Effect.Fade('popup_bg', { duration: 0.5, from: 0.999, to: 0 })
		}, duration: 0.5, from: 1, to: 0, afterFinish: function() {
			$(popupActive).hide();
			$('popup_area').hide();
			popupActive = '';
			popupState = 'closed';
			Event.stopObserving($('popup_bg'), 'click');
			activeFullImage = 0;
			if($('audio_tour')) {
				$('audio_tour').show();
			}
		}});
	}

	Exhibition.BgClose = function(e) {
		var theElement 	= Event.element(e);
	
	// Rerun the main function to close the popup only if the background is clicked
		if(theElement.id == 'popup_bg') {
			Exhibition.Close();
		}
	}

/***** PRELOAD THE ZOOM FUNCTIONS AND SETTINGS *****/

	Exhibition.Content = function() {
	// Reset all globals
		$(popupActive).show();
		
	// How far down the content area needs to go
		var windowHeightHalf	= (getWindowHeight() / 2);
		var contentHeightHalf	= (getOffsetHeight($('popup_content')) / 2);
		var windowWidthHalf		= (getWindowWidth() / 2);
		var contentWidthHalf	= (getOffsetWidth($('popup_content')) / 2);
		$('popup_content').style.top = (windowHeightHalf - contentHeightHalf) + 'px';
		$('popup_content').style.left = (windowWidthHalf - contentWidthHalf) + 'px';
	
	// Get the dimensions of the starting image size
		//var photoShell		= $$('#slideshow_popup .exhibit_photo')[0];
		//var photoWidth		= getOffsetWidth($(photoShell));
		//var photoHeight		= getOffsetHeight($(photoShell));
	
	// Photo list
		//var photoList		= $$('#slideshow_popup .exhibit_photo img');
		//for(var i = 0; i < photoList.length; i++) {
		//	photoList[i].hide();
		//}
		//photoList[0].show();
	
	// Content
		//var photoContent	= $$('#slideshow_popup .exhibit_content .div_cell');
		//for(var j = 0; j < photoContent.length; j++) {
		//	photoContent[j].hide();
		//}
		//photoContent[0].show();
	}



/********************************************************************************/
/***** EXHIBITION IMAGE FLIPPER ************************************************/
/******************************************************************************/

	var fullScreenChange	= 'false';
	var activeFullImage		= 0;
	
	Exhibition.Slider = function(direction) {
		if(fullScreenChange == 'false') {
			var imageContainer	= $$('#slideshow_popup .exhibit_photo')[0];
			var imageList		= imageContainer.getElementsByTagName('img');
	
			if(direction == 'prev') {
				if(activeFullImage == 0) {
					imageId = (imageList.length - 1);
				} else {
					imageId = activeFullImage - 1;
				}
			
			// Flip the images
				new Effect.Parallel([
					new Effect.BlindUp($(imageList[activeFullImage]), { scaleX: true, scaleY: false, sync: true }),
					new Effect.Fade($(imageList[activeFullImage]), { sync: true })
				], {
					beforeStart: function() {
						fullScreenChange = 'true';
						setTimeout('Exhibition.ScreenPos(\'prev\',\'' + imageId + '\')', 50);
					},
					duration: 0.5,
					afterFinish: function() {
						activeFullImage = imageId;
						$(imageList[activeFullImage]).style.height 	= '384px';
						$(imageList[activeFullImage]).height 		= '384';
						$(imageList[activeFullImage]).style.width 	= '533px';
						$(imageList[activeFullImage]).width 		= '533';
						$(imageList[imageId]).style.zIndex = '100';
						fullScreenChange = 'false';
					}
				});

			// Switch the content
				var contentList = $$('#content_repeater_shell .div_cell');
				Generic.Switcher(contentList[imageId], { transition: 'switcher', duration: 0.7 });
			} else {
				if(activeFullImage == (imageList.length - 1)) {
					imageId = 0;
				} else {
					imageId = activeFullImage + 1;
				}
				
			// Flip the images
				new Effect.Parallel([
					new Effect.BlindDown($(imageList[imageId]), { scaleX: true, scaleY: false, sync: true }),
					new Effect.Appear($(imageList[imageId]), { sync: true })
				], {
					beforeStart: function() {
						fullScreenChange = 'true';
						setTimeout('Exhibition.ScreenPos(\'next\',\'' + imageId + '\')', 50);
					},
					duration: 0.5,
					afterFinish: function() {
						$(imageList[activeFullImage]).style.zIndex = '50';
						$(imageList[activeFullImage]).hide();
						activeFullImage = imageId;
						fullScreenChange = 'false';
					}
				});
			// Switch the content
				var contentList = $$('#content_repeater_shell .div_cell');
				Generic.Switcher(contentList[imageId], { transition: 'switcher', duration: 0.7 });
			}
		}
	}

	Exhibition.ScreenPos = function(direction,imageId) {
		var imageContainer	= $$('#slideshow_popup .exhibit_photo')[0];
		var imageList		= imageContainer.getElementsByTagName('img');
		
		if(direction == 'next') {
			$(imageList[activeFullImage]).style.zIndex = '75';
			$(imageList[imageId]).style.zIndex 	= '100';
			$(imageList[imageId]).style.height 	= '384px';
			$(imageList[imageId]).height 		= '384';
			$(imageList[imageId]).style.width 	= '533px';
			$(imageList[imageId]).width 		= '533';
		} else {
			$(imageList[imageId]).show();
			$(imageList[activeFullImage]).style.zIndex 	= '100';
			$(imageList[imageId]).style.zIndex 	= '75';
		}
	}