




var zoomAnimationWaitTimer = null, zoomAnimationTimer = null, zoomAnimationFrame = 0;
var isFirstZoom = true;

function getScrollY() {
	if (self.pageYOffset) { return self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop; }
	else if (document.body) { return document.body.scrollTop; }
	return 0;
}

function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// showZoom()
// Preloads images. Places new image in lightbox then centers and displays.
//
function showZoom(objLink)
{
	// prep objects
	var overlay = document.getElementById('overlay');
	var zoomer = document.getElementById('zoom');
	var zoomedImage = document.getElementById('zoom-image');
	var loadIndicator = document.getElementById('zoom-load');
	
	var arrayPageSize = getPageSize();
	var scrollY = getScrollY();

	// set height of Overlay to take up whole page and show
	overlay.style.height = (arrayPageSize[1] + 'px');
	overlay.style.display = 'block';

	// center loadingImage if it exists
	if (loadIndicator) {
		loadIndicator.style.top = (scrollY + ((arrayPageSize[3] - 35 - 48) / 2) + 'px');
		loadIndicator.style.left = (((arrayPageSize[0] - 20 - 48) / 2) + 'px');
	}

	// Preload image
	var zoomPreload = new Image();
	zoomPreload.onload = function() {

		// No need to animate anymore
		clearInterval(zoomAnimationWaitTimer);
		clearInterval(zoomAnimationTimer);
		if (loadIndicator)
			loadIndicator.style.display = 'none';

		// Center zoomed image
		var y = scrollY + ((arrayPageSize[3] - 35 - zoomPreload.height) / 2);
		var x = ((arrayPageSize[0] - 20 - zoomPreload.width) / 2);
		zoomer.style.top = (y < 0) ? '0' : y + 'px';
		zoomer.style.left = (x < 0) ? '0' : x + 'px';
		
		// Show/hide caption
		var captionContainer = document.getElementById('zoom-captioncontainer');
		if (captionContainer != null) {
			if (objLink.getAttribute('title')) {
				captionContainer.style.display = 'block';
				document.getElementById('zoom-caption').innerHTML = objLink.getAttribute('title');
			} else
				captionContainer.style.display = 'none';
		}

		// After image is loaded, update the overlay height as the new image might have increased the overall page height.
		arrayPageSize = getPageSize();
		overlay.style.height = (arrayPageSize[1] + 'px');
		
		// Listen to escape
		document.onkeypress = Zoomie.keyDown;
		
		// Actually set the image
		if (navigator.appVersion.indexOf("MSIE")!=-1) // Small pause between the image loading and displaying prevents flicker in IE.
			pause(250);
		
		zoomedImage.src = objLink.href;
		zoomer.style.display = 'block';

		return false;
	}

	if (document.getElementById('zoom-load') != null)
		zoomAnimationWaitTimer = setInterval(delayAnimateLoad, 500);
		
	// Show the zoomer somewhere offscreen to preload its images
	if (isFirstZoom == true) {
		zoomer.style.top = '-10000px';
		zoomer.style.display = 'block';
		isFirstZoom = false;
	}
	
	zoomPreload.src = objLink.href;
}

function delayAnimateLoad()
{
	clearInterval(zoomAnimationWaitTimer);
	document.getElementById('zoom-load').style.display = 'block';
	zoomAnimationTimer = setInterval(animateLoad, 66);
}

function animateLoad()
{
	var loadIndicator = document.getElementById('zoom-load');
	loadIndicator.style.backgroundPosition = '0 -'+(zoomAnimationFrame * 48)+'px';
	zoomAnimationFrame = (zoomAnimationFrame + 1) % 12;
}
















var Zoomie = new Hash({
	hide									: function() {
		clearInterval(zoomAnimationWaitTimer);
		clearInterval(zoomAnimationTimer);
		
		var loadIndicator = $('zoom-load');
		if (loadIndicator != null) {
			loadIndicator.setStyle('display', 'none');
		};
		$('overlay').setStyle('display', 'none');
		$('zoom').setStyle('display', 'none');
		document.onkeypress = '';
	},
	keyDown									: function(event) {
		var esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var c = (window.event) ? event.keyCode : e.keyCode;
		if (c == esc) {
			Zoomie.hide();
		};
	},
	events									: {
		'a[rel=zoom]'							: {
			domready							: function() {
				// Load in the zoom stylesheet
				var css = new Asset.css('/application/css/zoom.css', { });
			
				var overlay = new Element('div', {
					id					: 'overlay',
					styles				: {
						'display'		: 'none',
						'position'		: 'absolute',
						'top'			: '0',
						'left'			: '0',
						'zIndex'		: '90',
						'width'			: '100%'
					},
					events				: {
						click			: function(event) {
							new Event(event).stop();
							Zoomie.hide();
						}
					}
				}).injectInside($(document.body), 'top');
			
				var loadPreloader = new Asset.image('/assets/images/zoom/ZoomProgress.png', {
					onload				: function() {
						var objLoadingImageLink = new Element('a', {
							href		: '#',
							events		: {
								click	: function(event) {
									new Event(event).stop();
									Zoomie.hide();
								}
							}
						}).injectInside(overlay);
					
						var loadIndicator = new Element('span', {
							id			: 'zoom-load',
							styles		: {
								'position'	: 'absolute',
								'zIndex'	: '150'
							}
						}).injectInside(objLoadingImageLink);
					
						loadPreloader.addEvent('load', function() {});
					}
				});
			
				var zoomContainer = new Element('div', {
					id					: 'zoom'
				});
				$(document.body).insertBefore(zoomContainer, overlay.nextSibling);
			
				// Top shadow
				if (!Browser.Engine.trident) {
					var topShadow = new Element('div', {
						'class'				: 'top',
						'html'				: '<div></div>'
					}).injectInside(zoomContainer);
				};
			
				// Inner shadow
				var elem = new Element('div');
			
				var innerOne = new Element('div', {'class': 'i1'}).injectInside(zoomContainer);
				var innerTwo = new Element('div', {'class': 'i2'}).injectInside(innerOne);
				var innerThree = new Element('div', {'class': 'i3'}).injectInside(innerTwo);
				var contentContainer = new Element('div', {'id': 'zoom-content'}).injectInside(innerThree);
			
				// Bottom shadow
				if (!Browser.Engine.trident) {
					var bottomShadow = new Element('div', {
						'class'				: 'bottom',
						'html'				: '<div></div>'
					}).injectInside(zoomContainer);
				};
			
				// Close button
				var closeButton = new Element('a', {
					'id'				: 'zoom-close',
					'href'				: '#',
					'html'				: 'Close',
					events				: {
						click			: function(event) {
							new Event(event).stop();
							Zoomie.hide();
						}
					}
				}).injectInside(contentContainer);
			
				// create link
				var objLink = new Element('a', {
					'href'				: '#',
					'title'				: 'Click to close',
					events				: {
						click			: function(event) {
							new Event(event).stop();
							Zoomie.hide();
						}
					}
				}).injectInside(contentContainer);
			
				// create image
				var objImage = new Element('img', {
					'id'				: 'zoom-image'
				}).injectInside(objLink);
			
				// create caption
				var captionContainer = new Element('div', {
					'id'				: 'zoom-captioncontainer'
				}).injectInside(contentContainer);
			
				var innerCaptionOne = new Element('div').injectInside(captionContainer);
				var innerCaptionTwo = new Element('div').injectInside(innerCaptionOne);
			
				var objCaption = new Element('span', {
					'id'				: 'zoom-caption'
				}).injectInside(innerCaptionTwo);
			
			},
			click								: function(event) {
				new Event(event).stop();
				showZoom(this);
			}
		}
	}
});

window.addEvent('domready', function() {
	var behaviour = new Behaviour(Zoomie.events);
});
