/* 
The following information must not be removed:
Awesome Box v2
Written by: Paul Armstrong, Paul Armstrong Designs
Site: http://paularmstrongdesigns.com
Idea and some functions from "LightBox" http://www.huddletogether.com
Example & Documentation: http://paularmstrongdesigns.com/awesome/box/
Last Updated: Friday, February 2, 2007 at 12:31:10

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
http://creativecommons.org/licenses/by-sa/2.5/
	
Required Yahoo! UI Files:
* yahoo.js
* dom.js
* event.js
* [ or: yahoo-dom-event.js ]
* animation.js
* [ or: animation-min.js ]
*/

var aLoadImgSrc = '/images/ajax_busy.gif'; // where is the loading image? (recommend absolute)

var aImgTypes = new Array('jpg', 'gif', 'png', 'bmp'); // types of images to place in Awesome Box

//
//	variables used for keeping history either anchor hashes or yui history querystring
var awseomePrefix = 'h';
var hashPrefix = '#';
//
// 	store the timer id that watches and tracks browser history. each timer will be destroyed and recreated after each awesomebox load event
var timerId = 0;
//
//  store the current image hash that awesomebox is showing the user to compare against whats in the url
var currentImageHash = '';
//
//  does the browser hash differ from what the awesome box is showing. if so user is clicking back
//  in the browser and so can assume they want to go back to the home display so close awesomebox
function TrackUrlAndCloseOnHistoryChange(e, that) {

	if (currentImageHash != '') {
		if (window.location.href.indexOf(hashPrefix) != -1) {
			var browserHash = YAHOO.util.History.getCurrentState(awseomePrefix)
			if (browserHash != currentImageHash && browserHash != '' && browserHash != null) {

				// clear the timer for this timer
				clearInterval(timerId);

				var imgRefs = aClient.allImgs();
				var photoSet = new Array();
				for (j = 0; j < imgRefs.length; j++) {
					if (imgRefs[j].getAttribute('rel') && imgRefs[j].getAttribute('rel') == that.getAttribute('rel')) {
						photoSet.push(imgRefs[j]);
					}
				}

				var key = photoSet.findKeyFromHistoryValue(photoSet, browserHash);
				currentImageHash = browserHash;
				YAHOO.awesomebox.load(e, new Array(photoSet, key, false), true);

			}
		} else {
			clearInterval(timerId);
			YAHOO.awesomebox.close();
		}
	}
}


//
//	not ideal but until we upgrade to yui 3.2+ or something that handles
//  browser history really well then...
function isInternetExplorer() {
	return YAHOO.env.ua.ie > 0;
}

//
// find the new key because of the history change
Array.prototype.findKeyFromHistoryValue = function(value, key) {

	if (key && key != undefined) {
		if (key.indexOf(hashPrefix) != -1) {
			key = key.replace(hashPrefix, '');
		}
	}

	if (value && value != undefined) {
		for (i = 0; i < this.length; i++) {

			var href = this[i].getAttribute('href');
			if (href && href != undefined) {
				if (href.indexOf(key) != -1) {
					return i;
				}
			}
		}
	}

	return null;
}


/*
*  ***************************************
*  EDIT BEYOND THIS POINT AT YOUR OWN RISK (I'm doing it!!)
*  ***************************************
*/

var aClient = new Object();
/* 
*  aClient.allImgs()
*    Find all links going to an image with an aImgTypes and return it as an array.
*/
aClient.allImgs = function() {
	var links = document.getElementsByTagName('a');
	var photos = new Array();
	for (i = 0; i < links.length; i++) {
		for (j = 0; j < aImgTypes.length; j++) {
			if (links[i].href.indexOf(aImgTypes[j]) != -1) {
				photos.push(links[i])
			}
		}
	}
	return photos;
}

/* 
*  aClient.pageWidth() and aClient.pageHeight()
*    Returns the width and height of the content in the document.
*/
aClient.pageWidth = function() {
	var xScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
	}
	if (xScroll < YAHOO.util.Dom.getViewportWidth()) {
		pageWidth = YAHOO.util.Dom.getViewportWidth();
	} else {
		pageWidth = xScroll;
	}
	return pageWidth;
};
aClient.pageHeight = function() {
	var yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}
	if (yScroll <= YAHOO.util.Dom.getViewportHeight()) {
		pageHeight = YAHOO.util.Dom.getViewportHeight();
	} else {
		pageHeight = yScroll;
	}
	return pageHeight;
};

/* 
*  aClient.xScroll(), aClient.yScroll()
*    Returns the position of the X and Y scrollbars.
*/
aClient.xScroll = function() {
	var xScroll = window.scrollX || document.documentElement.scrollLeft;
	return xScroll;
}
aClient.yScroll = function() {
	var yScroll = window.scrollY || document.documentElement.scrollTop;
	return yScroll;
}

/* 
*  array.inArray(value)
*    Returns the key of the array that value is assigned to if true, null if false.
*/
Array.prototype.inArray = function(value) {
	for (i = 0; i < this.length; i++) { if (this[i] == value) { return i; } }
	return null;
}

/*
*  AnimMgr
*    Sets the default frames per second higher so Safari will display animations
*    at the correct speed. Degrades for slower browsers.
*/
YAHOO.util.AnimMgr.fps = 500;

YAHOO.widget.aEffect = function(el) { this.oEl = YAHOO.util.Dom.get(el); };

/*
*  YAHOO.widget.aEffect.aShowLoad()
*    Animation widget for transition period on Awesome Box initialize or between images.
*/
YAHOO.widget.aEffect.prototype.aShowLoad = function() {
	var showLoad = new YAHOO.util.Anim('aLoadImg', { opacity: { to: 1} }, 0.2);
	showLoad.onStart.subscribe(function() {
		YAHOO.util.Dom.setStyle('aLoadImg', 'display', 'block');
		YAHOO.util.Dom.setXY('aLoadImg', [
			(YAHOO.util.Dom.getViewportWidth() / 2) - 45 + aClient.xScroll(),
			(YAHOO.util.Dom.getViewportHeight() / 2) - 11 + aClient.yScroll()
		]);
		if (YAHOO.util.Dom.hasClass('aBoxMeta', 'aOpen')) {
			var hideMeta = new YAHOO.util.Anim('aBoxMeta', { opacity: { to: 0 }, height: { to: 0} }, 0.2);
			hideMeta.onStart.subscribe(function() {
				YAHOO.util.Dom.setStyle('aImg', 'opacity', '0');
				YAHOO.util.Dom.setStyle('aImg', 'visibility', 'hidden');
			});
			hideMeta.animate();
		}
	});
	showLoad.animate();
};

/*
*  YAHOO.widget.aEffect.aShowOverlay
*    Fades in #aOverlay and #aBox.
*    Initializes YAHOO.widget.aEffect.aShowLoad()
*/
YAHOO.widget.aEffect.prototype.aShowOverlay = function() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		YAHOO.util.Dom.setStyle(selects[i], 'visibility', 'hidden');
	}

	var fadeOverlay = new YAHOO.util.Anim('aOverlay', { opacity: { to: 0.5} }, 0.3);
	fadeOverlay.onStart.subscribe(function() {
		YAHOO.util.Dom.setStyle('aOverlay', 'display', 'block');
		YAHOO.util.Dom.setStyle('aOverlay', 'height', aClient.pageHeight() + 'px');
	});
	var showBox = new YAHOO.util.Anim('aBox', { opacity: { to: 1} }, 0.3);
	showBox.onStart.subscribe(function() {
		YAHOO.util.Dom.setStyle('aBox', 'display', 'block');
		var showLoad = new YAHOO.widget.aEffect();
		showLoad.aShowLoad();
	});
	showBox.animate();
	fadeOverlay.animate();
};

/*
*  YAHOO.widget.aEffect.aResizeBox()
*    Initialized after new image is completed loading. Resizes and moves #aBox, times out
*    and displays #aBoxMeta information
*/
YAHOO.widget.aEffect.prototype.aResizeBox = function(aPreload) {
	var imgScale = YAHOO.awesomebox.scaleImage(aPreload);

	var moveBox = new YAHOO.util.Motion('aBox', {
		width: { to: (imgScale[0] + 20) },
		height: { to: (imgScale[1] + 20) },
		points: { to: [
			(YAHOO.util.Dom.getViewportWidth() / 2) - (imgScale[0] / 2) + aClient.xScroll() - 10,
			(YAHOO.util.Dom.getViewportHeight() / 2) - (imgScale[1] / 2) + aClient.yScroll() - 10
			]
		}
	}, 0.3);
	moveBox.onStart.subscribe(function() {
		YAHOO.util.Dom.setStyle('aBox', 'display', 'block');

		var hideLoad = new YAHOO.util.Anim('aLoadImg', { opacity: { to: 0} }, 0.3);
		var sizeImage = new YAHOO.util.Anim('aImg', { width: { to: imgScale[0] }, height: { to: imgScale[1]} }, 0.3);

		hideLoad.onComplete.subscribe(function() { YAHOO.util.Dom.setStyle('aLoadImg', 'display', 'none'); });
		hideLoad.animate();
		sizeImage.animate();
	});
	moveBox.onComplete.subscribe(function() {
		YAHOO.util.Dom.setStyle('aBox', 'height', imgScale[1] + 70 + 'px');
		YAHOO.util.Dom.setStyle('aBoxMeta', 'width', imgScale[0] + 'px');

		// Dynamically resize the DIV containing the photo title according
		// to the width of the photo
		YAHOO.util.Dom.setStyle('aInfoTitle', 'width', imgScale[0] - 28 - 5 - 80 - 10 + 'px');

		YAHOO.util.Dom.addClass('aBoxMeta', 'aOpen');
		YAHOO.util.Dom.get('aImg').setAttribute('width', imgScale[0]);
		YAHOO.util.Dom.get('aImg').setAttribute('height', imgScale[1]);
		YAHOO.util.Dom.setStyle('aImg', 'visibility', 'visible');

		var fadeImg = new YAHOO.util.Anim('aImg', { opacity: { from: 0, to: 1} }, 0.3);

		fadeImg.onComplete.subscribe(function() {
			var showMeta = new YAHOO.util.Anim('aBoxMeta', { opacity: { from: 0, to: 1 }, height: { to: 40} }, 0.2);
			showMeta.animate();
		});
		fadeImg.animate();
	});
	if (YAHOO.util.Dom.hasClass('aBoxMeta', 'aOpen')) {
		setTimeout(function() {
			YAHOO.util.Dom.get('aImg').setAttribute('src', aPreload.src);
			moveBox.animate();
		}, 300);
	} else {
		YAHOO.util.Dom.get('aImg').setAttribute('src', aPreload.src);
		moveBox.animate();
	}
};

/*
*  YAHOO.widget.aEffect.aClose
*    Closes #aBox and #aOverlay. Invoked via pressing 'x', clicking the 'X' image or #aOverlay
*/
YAHOO.widget.aEffect.prototype.aClose = function() {
	var that = YAHOO.util.Dom.get('aImg');
	var hideMeta = new YAHOO.util.Anim('aBoxMeta', { height: { to: 0 }, opacity: { to: 0} }, 0.2);
	var fadeBox = new YAHOO.util.Motion('aBox', { opacity: { to: 0} }, 0.3);
	fadeBox.onComplete.subscribe(function() {
		var hideOverlay = new YAHOO.util.Anim('aOverlay', { opacity: { to: 0} }, 0.3);
		hideOverlay.onStart.subscribe(function() {
			selects = document.getElementsByTagName("select");
			for (i = 0; i != selects.length; i++) {
				YAHOO.util.Dom.setStyle(selects[i], 'visibility', 'visible');
			}
			YAHOO.util.Dom.removeClass('aBoxMeta', 'aOpen');
			YAHOO.util.Dom.setStyle('aBox', 'display', 'none');
			YAHOO.util.Dom.setStyle('aImg', 'opacity', '0');
			YAHOO.util.Dom.setStyle('aImg', 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle('aOverlay', 'display', 'none');
			YAHOO.util.Dom.setStyle('aOverlay', 'height', '0px');
			YAHOO.util.Dom.setStyle('aLoadImg', 'display', 'none');
		});
		hideOverlay.animate();
	});
	hideMeta.animate();
	fadeBox.animate();
	YAHOO.util.Event.removeListener('aNextButtonBlock', 'click');
	YAHOO.util.Event.removeListener('aPrevButtonBlock', 'click');
	YAHOO.util.Event.removeListener(document, 'keypress');
};

// A global function that takes an event, e, and returns true if the mouse cursor is on the rightmost 50% of the image and vice versa
function isNextButton(e) {
	if (!e) e = window.event
	var targetElem = (e.target || e.srcElement);
	//The actual width and height is set at the grandparent node, so we have to find our way up that path
	if (targetElem && targetElem != undefined) {
		if (targetElem.parentNode && targetElem.parentNode != undefined) {
			if (targetElem.parentNode.parentNode && targetElem.parentNode.parentNode != undefined) {
				var x = e.clientX - targetElem.parentNode.parentNode.offsetLeft;
				return (x > (targetElem.offsetWidth / 2) + 10); // +10 because that's the size of the border of the holding DIV
			}
		}
	}
	return true;
}

/*
* YAHOO.widget.aEffect.aAwesome
*   This is awesome. Pay no attention to it.
*/
YAHOO.widget.aEffect.prototype.aAwesome = function() {
	if (!YAHOO.util.Dom.get('aAwesome')) {
		var aBody = document.getElementsByTagName("body").item(0);
		var aAwesome = document.createElement('div');
		
		YAHOO.util.Dom.get(aAwesome).setAttribute('id', 'aAwesome');
		YAHOO.util.Dom.setStyle(aAwesome, 'background', "url(/images/hl-awesome.png)");
		YAHOO.util.Dom.setStyle(aAwesome, 'display', 'block');
		YAHOO.util.Dom.setStyle(aAwesome, 'width', '73px');
		YAHOO.util.Dom.setStyle(aAwesome, 'height', '108px');
		YAHOO.util.Dom.setStyle(aAwesome, 'position', 'absolute');
		YAHOO.util.Dom.setStyle(aAwesome, 'z-index', '999');
		aBody.appendChild(aAwesome);
		
		var aMove = new YAHOO.util.Motion('aAwesome', { points: { from: [-100, ((YAHOO.util.Dom.getViewportHeight() / 2) + aClient.yScroll() - 50)], to: [0, ((YAHOO.util.Dom.getViewportHeight() / 2) + aClient.yScroll() - 50)]} }, 0.1);
		aMove.animate();
		setTimeout(function() { var aMove = new YAHOO.util.Motion('aAwesome', { points: { to: [-100, ((YAHOO.util.Dom.getViewportHeight() / 2) + aClient.yScroll() - 50)]} }, 0.1);
		aMove.onComplete.subscribe(function() { aBody.removeChild(aAwesome); }); aMove.animate(); }, 2000);
	}
};

// Boolean variables that tells us if we're at the beginning or end of the photo set
var isFirstImage;
var isLastImage;

/*
*  YAHOO.awesomebox()
*    Non-animating functions, initialized on window load completion.
*/
YAHOO.awesomebox = function() {
	return {

		/*
		*  init()
		*    Creates the markup, checks for direct image link, adds event listeners.
		*/
		init: function() {
			if (!document.getElementsByTagName) { return; }

			var aBody = document.getElementsByTagName("body").item(0);

			var aOverlay = document.createElement('div');
			YAHOO.util.Dom.get(aOverlay).setAttribute('id', 'aOverlay');
			YAHOO.util.Dom.get(aOverlay).setAttribute('title', 'Click to Close');
			// Trap any double clicks setting the focus somewhere harmless to prevent some browsers putting an ugly highlight on an image if it has the focus.
			YAHOO.util.Dom.get(aOverlay).setAttribute('ondblclick', 'YAHOO.util.Dom.get("aInfoTitle").focus();');
			aBody.appendChild(aOverlay);
			YAHOO.util.Dom.setStyle('aOverlay', 'opacity', '0');

			var aBox = document.createElement('div');
			YAHOO.util.Dom.get(aBox).setAttribute('id', 'aBox');
			aBody.appendChild(aBox);
			YAHOO.util.Dom.setStyle('aBox', 'opacity', '0');
			YAHOO.util.Dom.setXY('aBox', [(YAHOO.util.Dom.getViewportWidth() / 2) - 21, (YAHOO.util.Dom.getViewportHeight() / 2) - 21])
			YAHOO.util.Dom.setStyle('aBox', 'display', 'none');

			var aPrevButtonBlock = document.createElement('div');
			YAHOO.util.Dom.get(aPrevButtonBlock).setAttribute('id', 'aPrevButtonBlock');

			var aNextButtonBlock = document.createElement('div');
			YAHOO.util.Dom.get(aNextButtonBlock).setAttribute('id', 'aNextButtonBlock');

			//Todo - change these image paths to actual arrows
			var aPrevButtonImg = document.createElement('img');
			YAHOO.util.Dom.get(aPrevButtonImg).setAttribute('id', 'aPrevButtonImg');
			YAHOO.util.Dom.get(aPrevButtonImg).setAttribute('src', '/images/previous.png');

			var aNextButtonImg = document.createElement('img');
			YAHOO.util.Dom.get(aNextButtonImg).setAttribute('id', 'aNextButtonImg');
			YAHOO.util.Dom.get(aNextButtonImg).setAttribute('src', '/images/next.png');

			var aImgHolder = document.createElement('div');
			YAHOO.util.Dom.get(aImgHolder).setAttribute('id', 'aImgHolder');
			// Trap any double clicks setting the focus somewhere harmless to prevent some browsers putting an ugly highlight on an image if it has the focus.
			YAHOO.util.Dom.get(aImgHolder).setAttribute('ondblclick', 'YAHOO.util.Dom.get("aInfoTitle").focus();');
			aBox.appendChild(aImgHolder);

			var aImg = document.createElement('img');
			YAHOO.util.Dom.get(aImg).setAttribute('id', 'aImg');
			aImgHolder.appendChild(aImg);

			YAHOO.util.Dom.setStyle('aImg', 'opacity', '0');
			YAHOO.util.Dom.setStyle('aImg', 'visibility', 'hidden');

			aImgHolder.appendChild(aPrevButtonImg);
			aImgHolder.appendChild(aNextButtonImg);
			aImgHolder.appendChild(aPrevButtonBlock);
			aImgHolder.appendChild(aNextButtonBlock);

			YAHOO.util.Dom.setStyle('aPrevButtonImg', 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle('aNextButtonImg', 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle('aPrevButtonImg', 'position', 'absolute');
			YAHOO.util.Dom.setStyle('aNextButtonImg', 'position', 'absolute');
			YAHOO.util.Dom.setStyle('aPrevButtonImg', 'left', '10px');
			YAHOO.util.Dom.setStyle('aNextButtonImg', 'right', '10px');
			YAHOO.util.Dom.setStyle('aPrevButtonImg', 'top', '40px');
			YAHOO.util.Dom.setStyle('aNextButtonImg', 'top', '40px');
			YAHOO.util.Dom.setStyle('aPrevButtonImg', 'opacity', '.8');
			YAHOO.util.Dom.setStyle('aNextButtonImg', 'opacity', '.8');

			//aPrevButtonBlock and aNextButtonBlock are used to block the region of the image which isn't clickable when we're at the beginning or end of the photo list
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'position', 'absolute');
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'width', '50%');
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'height', '100%');
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'left', '0px');
			YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'top', '0px');

			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'position', 'absolute');
			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'width', '50%');
			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'height', '100%');
			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'right', '0px');
			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'top', '0px');
			YAHOO.util.Dom.setStyle('aNextButtonBlock', 'visibility', 'hidden');

			var aBoxMeta = document.createElement('div');
			YAHOO.util.Dom.get(aBoxMeta).setAttribute('id', 'aBoxMeta');
			aBox.appendChild(aBoxMeta);

			var aCloseButton = document.createElement('a');
			YAHOO.util.Dom.get(aCloseButton).setAttribute('id', 'aCloseButton');
			aBoxMeta.appendChild(aCloseButton);
			YAHOO.util.Dom.get(aCloseButton).setAttribute('href', 'javascript:void(0)');
			YAHOO.util.Dom.addClass(aCloseButton, 'aButton');

			var aInfoTitle = document.createElement('div');
			YAHOO.util.Dom.get(aInfoTitle).setAttribute('id', 'aInfoTitle');
			aBoxMeta.appendChild(aInfoTitle);

			var aCount = document.createElement('div');
			YAHOO.util.Dom.get(aCount).setAttribute('id', 'aCount');
			aBoxMeta.appendChild(aCount);

			var aInfo = document.createElement('p');
			YAHOO.util.Dom.get(aInfo).setAttribute('id', 'aInfo');
			aBoxMeta.appendChild(aInfo);

			var preloadLoading = new Image();
			preloadLoading.src = aLoadImgSrc;

			var aLoadImg = document.createElement('img');
			YAHOO.util.Dom.get(aLoadImg).setAttribute('id', 'aLoadImg');
			aBody.appendChild(aLoadImg);
			YAHOO.util.Dom.get(aLoadImg).setAttribute('src', preloadLoading.src);
			YAHOO.util.Dom.setStyle('aLoadImg', 'opacity', '0');

			/*
			*  Hash Check
			*    Checks the URL on page load for #filename and attempts to load it
			*/


			if (window.location.href.indexOf(hashPrefix) != -1) {
				// generate an ID for the body if it doesn't exist
				if (document.body.id != '' || document.body.id != null) {
					var body = YAHOO.util.Dom.generateId(document.body);
				} else {
					var body = document.body.id;
				}
				var imgArray = aClient.allImgs();
				var loadImg;
				if (isInternetExplorer()) {
					loadImg = window.location.href.split(hashPrefix)[1];
				} else {
					loadImg = YAHOO.util.History.getCurrentState(awseomePrefix).replace(hashPrefix, '');
				}

				for (i = 0; i < imgArray.length; i++) {
					var imgUrl = imgArray[i].getAttribute('href').split('/');
					if (imgUrl[imgUrl.length - 1].split('.')[0] == loadImg) {
						YAHOO.util.Event.onAvailable(body, this.load, YAHOO.util.Dom.get(imgArray[i]), true);

					}
				}
			}

			YAHOO.util.Event.on(aCloseButton, 'click', this.close);

			YAHOO.util.Event.on(aClient.allImgs(), 'click', this.load);

			// The lightbox has now initialised. Replace the lightbox loading
			// graphics with magnifying glasses.
			var arrImages = document.getElementsByTagName("img");
			for (var i = 0; i < arrImages.length; i++) {
				if (arrImages[i].className == "magnifying_glass") { arrImages[i].src = "/images/magnify.png"; }
			}
		},

		direction: function(e) {
			return isNextButton(e, null, null) ? 1 : -1;
		},

		mouseOver: function(e) {
			if (!isLastImage) { YAHOO.util.Dom.setStyle('aNextButtonImg', 'visibility', isNextButton(e, null) ? 'visible' : 'hidden'); }
			if (!isFirstImage) { YAHOO.util.Dom.setStyle('aPrevButtonImg', 'visibility', isNextButton(e, null) ? 'hidden' : 'visible'); }
		},

		mouseOut: function(e) {
			if (!e) e = window.event
			var prevXY = YAHOO.util.Dom.getXY('aPrevButtonImg');
			var nextXY = YAHOO.util.Dom.getXY('aNextButtonImg');
			var hide = true;
			if ((e.clientX - nextXY[0] < 27 && e.clientX - nextXY[0] > 0) ||
				(e.clientX - prevXY[0] < 27 && e.clientX - prevXY[0] > 0)) {
				hide = false;
			}
			var isNextButton = ((e.target || e.srcElement) == 'aNextButtonBlock');
			if (hide) {
				YAHOO.util.Dom.setStyle('aNextButtonImg', 'visibility', 'hidden');
				YAHOO.util.Dom.setStyle('aPrevButtonImg', 'visibility', 'hidden');
			}
		},

		/*
		*  load()
		*    Gets information for new image and invokes appropriate actions.
		*/
		load: function(e, photoSetAndKey, loadKnownImage) {

			// Take the listener that fires the close event off the overlay to prevent an inadvertant second click immediately closing the awesomebox again.
			YAHOO.util.Event.removeListener(YAHOO.util.Dom.get('aOverlay'), 'click', YAHOO.awesomebox.close);

			var newImg;
			var key;

			var doAnims = true;
			if (photoSetAndKey) {
				//This is all to make it work in nasty IE. 
				//Check that we're not on the first or last pic and clicking out of range. 
				//All because IE can't handle transparent DIVs on top of opacity 100 images
				var photoSetLength = photoSetAndKey[0].length - 1;
				doAnims = (photoSetAndKey[1] == null) ||
							((photoSetAndKey[1] > 0 || ((photoSetAndKey[1] == 0 && isNextButton(e)))) &&
							(photoSetAndKey[1] < photoSetLength || (photoSetAndKey[1] == photoSetLength && !isNextButton(e))));

				if (loadKnownImage != null && loadKnownImage != undefined && loadKnownImage == true) {
					key = photoSetAndKey[1];
				} else {
					key = photoSetAndKey[1] + (isNextButton(e) ? 1 : -1);
					if (photoSetAndKey[2] != null) {
						key = photoSetAndKey[1] + (photoSetAndKey[2] ? 1 : -1);
					} else {
						key = photoSetAndKey[1] + (isNextButton(e) ? 1 : -1);
					}
				}

				if (key < 0) { key = 0; }
				if (key > photoSetLength) { key = photoSetLength; }
				newImg = photoSetAndKey[0][key];
			}
			if (doAnims) {
				var fadeOverlayIn = new YAHOO.widget.aEffect();
				fadeOverlayIn.aShowOverlay();
			}
			if (newImg) {
				var that = newImg;
			} else { var that = this; }

			try {
				// ie6 doesnt like this
				YAHOO.util.Event.stopEvent(e);
			} catch (e) {
				// ie6 doesnt like this so ignore
			}

			YAHOO.util.Event.removeListener(document, 'keypress');

			aPreload = new Image();
			aPreload.onload = function() {
				if (doAnims) {
					var resizeBox = new YAHOO.widget.aEffect(that);
					resizeBox.aResizeBox(aPreload);
				}

				if (!that.getAttribute('rel')) {
					YAHOO.util.Dom.setStyle('aNextButtonBlock', 'visibility', 'hidden');
					YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'visibility', 'hidden');
				} else {
					YAHOO.util.Event.removeListener('aNextButtonBlock', 'click');
					YAHOO.util.Event.removeListener('aPrevButtonBlock', 'click');
				}
				var aInfoTitle = '';
				var aInfo = '';

				if (that.getAttribute('title') != null) {
					var aInfoTitle = that.getAttribute('title');
				}
				if (
					that.getElementsByTagName('img')[0] &&
					that.getElementsByTagName('img')[0].getAttribute('title') != null
				) {
					var aInfoTitle = that.getElementsByTagName('img')[0].getAttribute('alt');
				}
				if (
					that.getElementsByTagName('img')[0] &&
					that.getElementsByTagName('img')[0].getAttribute('alt') != null
				) {
					//var aInfo = that.getElementsByTagName('img')[0].getAttribute('alt');
				} else {
					var aInfoTitle = that.innerHTML;
				}

				if (YAHOO.util.Dom.hasClass('aBoxMeta', 'aOpen')) {
					setTimeout(function() {
						YAHOO.util.Dom.get('aInfoTitle').innerHTML = aInfoTitle;
						YAHOO.util.Dom.get('aInfo').innerHTML = aInfo;
					}, 250);
				} else {
					YAHOO.util.Dom.get('aInfoTitle').innerHTML = aInfoTitle;
					YAHOO.util.Dom.get('aInfo').innerHTML = aInfo;
				}


				var urlSplit = that.getAttribute('href').split('/');
				var filename = urlSplit[urlSplit.length - 1].split('.')[0];

				if (isInternetExplorer()) {
					if (window.location.href.indexOf(hashPrefix) != -1) {
						var url = window.location.href.split(hashPrefix)[0];
					} else {
						var url = window.location.href;
					}
					window.location = url + hashPrefix + filename;
				} else {
					try {
						currentImageHash = hashPrefix + filename;
						YAHOO.util.History.navigate(awseomePrefix, hashPrefix + filename);

					} catch (ex) {
						window.location = url + hashPrefix + filename;
					}
				}

				if (that.getAttribute('rel')) {
					YAHOO.awesomebox.loadNeighbors(that, e);
				}

				YAHOO.util.Event.addListener(document, 'keypress', function(e) {
					YAHOO.util.Event.stopEvent(e);
					switch (YAHOO.util.Event.getCharCode(e)) {
						case (120): YAHOO.awesomebox.close();
							break;
						case (97): YAHOO.awesomebox.awesome();
							break;
					}
				});
			};

			aPreload.src = that.getAttribute('href');

			// One second after the load is finished, add a listener to the overlay to close the awsomebox when clicked. 
			// The delay prevents inadvertant second clicks immediately closing the awesomebox just after it's been opened. 
			setTimeout("YAHOO.util.Event.on(YAHOO.util.Dom.get('aOverlay'), 'click', YAHOO.awesomebox.close)", 1000);

			// load has fired again so clear and close the previous timer
			if (timerId > 0) {
				clearInterval(timerId);
			}

			// set up a timer that tracks and compares the browser hash and the file hash used by awesomebox
			timerId = setInterval(function() { TrackUrlAndCloseOnHistoryChange(e, that); }, 200);
		},

		/*
		*  loadNeighbors()
		*    Invoked via load(), finds and preloads next and previous image.
		*/
		loadNeighbors: function(that, e) {
			var imgRefs = aClient.allImgs();
			var photoSet = new Array();
			for (j = 0; j < imgRefs.length; j++) {
				if (
					imgRefs[j].getAttribute('rel') &&
					imgRefs[j].getAttribute('rel') == that.getAttribute('rel')
				) {
					photoSet.push(imgRefs[j]);
				}
			}
			var key = photoSet.inArray(that);
			setTimeout(function() {
				YAHOO.util.Dom.get('aCount').innerHTML = 'Image ' + (key + 1) + ' of ' + photoSet.length;
			}, 300);

			if (key != photoSet.length - 1) {
				var next = new Image();
				next.src = photoSet[key + 1].getAttribute('href');
				YAHOO.util.Dom.setStyle('aNextButtonBlock', 'visibility', 'hidden');
				isLastImage = false;
			} else {
				isLastImage = true;
				YAHOO.util.Dom.setStyle('aNextButtonBlock', 'visibility', 'visible');
			}
			if (key != 0) {
				var prev = new Image();
				prev.src = photoSet[key - 1].getAttribute('href');
				YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'visibility', 'hidden');
				isFirstImage = false;
			} else {
				YAHOO.util.Dom.setStyle('aPrevButtonBlock', 'visibility', 'visible');
				isFirstImage = true;
			}
			YAHOO.util.Event.addListener('aImg', 'mousemove', this.mouseOver);
			YAHOO.util.Event.addListener('aImg', 'mouseout', this.mouseOut);

			YAHOO.util.Event.removeListener('aImg', 'click');
			YAHOO.util.Event.removeListener('aPrevButtonImg', 'click');
			YAHOO.util.Event.removeListener('aNextButtonImg', 'click');

			YAHOO.util.Event.addListener('aImg', 'click', YAHOO.awesomebox.load, new Array(photoSet, key, null), true);
			YAHOO.util.Event.addListener('aPrevButtonImg', 'click', YAHOO.awesomebox.load, new Array(photoSet, key, false), true);
			YAHOO.util.Event.addListener('aNextButtonImg', 'click', YAHOO.awesomebox.load, new Array(photoSet, key, true), true);

			YAHOO.util.Event.addListener(document, 'keypress', function(e) {
				YAHOO.util.Event.stopEvent(e);
				switch (YAHOO.util.Event.getCharCode(e)) {
					// 110 = n, 112 = p                                      
					case (110):
						if (key != photoSet.length - 1) {
							YAHOO.awesomebox.load('', photoSet[key + 1]);
						}
						break;
					case (112):
						if (key != 0) {
							YAHOO.awesomebox.load('', photoSet[key - 1]);
						}
						break;
				}
			});
		},

		/*
		*  scaleImage()
		* Returns proportional values for height and width of image, scaled if necessary.
		*/
		scaleImage: function(aImage) {
			var sHeight = aImage.height;
			var sWidth = aImage.width;

			if (aImage.width > YAHOO.util.Dom.getViewportWidth()) {
				sWidth = YAHOO.util.Dom.getViewportWidth() - 20;
				sHeight = aImage.height * (sWidth / aImage.width);
				if (sHeight + 100 > (YAHOO.util.Dom.getViewportHeight() - 20)) {
					sHeight = YAHOO.util.Dom.getViewportHeight() - 120;
					sWidth = aImage.width * (sHeight / aImage.height);
				}
			} else if (aImage.height + 100 > YAHOO.util.Dom.getViewportHeight()) {
				sWidth = aImage.width * ((YAHOO.util.Dom.getViewportHeight() - 120) / aImage.height);
				sHeight = YAHOO.util.Dom.getViewportHeight() - 120;
				if (sWidth > (YAHOO.util.Dom.getViewportWidth() - 20)) {
					sWidth = aImage.width * (sHeight / aImage.height);
					sHeight = aImage.height * (sWidth / aImage.width);
				}
			}

			var scaleAtts = new Array(Math.floor(sWidth), Math.floor(sHeight));
			return scaleAtts;
		},

		/*
		*  close()
		*    Calls YAHOO.widget.aEffect.aClose() and resets the #filename to #close.
		*    Not the most elegant solution.
		*/
		close: function() {
			var aClose = new YAHOO.widget.aEffect();
			aClose.aClose();
			if (window.location.href.indexOf(hashPrefix) != -1) {
				var url = window.location.href.split(hashPrefix)[0];
			} else {
				var url = window.location.href;
			}
			window.location = url
		},

		awesome: function() {
			var aAwesome = new YAHOO.widget.aEffect();
			aAwesome.aAwesome();
		}
	}
} ();

var whenReady = function() {

	if (isInternetExplorer()) {
		YAHOO.awesomebox.init();
	} else {
		// Get the initial state of the "awesome" module:
		initialState = YAHOO.util.History.getBookmarkedState(awseomePrefix) || "";

		// Register the "awesome" module:
		YAHOO.util.History.register(awseomePrefix, initialState, function(state) {
		});


		// When the YUI Browser History Manager, initialize the slide show:
		YAHOO.util.History.onReady(function() {
			YAHOO.awesomebox.init();
		});

		// Initialize the YUI Browser History Manager instead of calling
		// init() directly, except in the catch block...
		try {
			YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
		} catch (e) {
			YAHOO.awesomebox.init();
		}
	}
};

/*
*  Start up the processes when the dom is ready and not when all the images have loaded
*/
YAHOO.util.Event.onDOMReady(whenReady);







