// -----------------------------------------------------------------------------
//
//	nlightbox v1.0
//	by Nathan Kelly - http://www.nathan-kelly.com
//	Last Modification: 19th July 2008
//	Change List:
//	(Coming soon)
//
//	How is nlightbox.js different to nlightbox.js?
//	nlightbox.js incorporates approximately twice the animation and transition effects
//	of nlightbox. Additionally the hoverNav is replaced with a solid and consistent
//	navigation element within the image-data-container that also switches the next
//	button with a restart button once the last image in a group has been reached.
//	There is also an additional image preloader for all of the images used inside the
//	nlightbox interface which can be set up in the configuration object.
//
//	For the full list of differences go to:
//	(Coming soon)
//
//
// -----------------------------------------------------------------------------
//
//	Credits Due (and much deserved too please read):
//	Based on nLightbox v2.04
//
//	nLightbox v2.04
//	by Lokesh Dhakar - http://www.lokeshdhakar.com
//	Last Modification: 2/9/08
//
//	For more information, visit:
//	http://lokeshdhakar.com/projects/nlightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//  	- Free for use in both personal and commercial projects
//	- Attribution requires leaving author name, author link, and the license info intact.
//
//	Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
//  		Artemy Tregubenko (arty.name) for cleanup and help in updating to latest ver of proto-aculous.
//
// -----------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration

	nLightbox Class Declaration
	- initialize()
	- updateImageList()
	- start()
	- firstImage()
	- changeImage()
	- resizeImageContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadInterface()
	- preloadNeighborImages()
	- end()

	Function Calls
	- document.observe()

*/
// -----------------------------------------------------------------------------------

//
//  Configuration
//
nLightboxOptions = Object.extend({
	// set the base url to your nLightbox "Interface Images" directory here
	// eg: http://www.mysite.com/images/nlightbox/ (be sure to include the trailing slash)
	// Note: this is not the directory for images that are displayed in the nlightbox
	baseUrl: 'http://www.teenspirit.info/images/nlightbox/',
	// if you use images in your nlightbox interface list them here for preloading
	// make sure you add your throbber (loading image) so that it is preloaded before
	// the nlightbox is opened.
	interfaceImages: [
		'ui-throbber.gif',
		'ui-nlightbox-nav.png'
	],
	// the image used for your loading throbber
	throbber: 'ui-throbber.gif',
	// the text for your image data (navigation and image count)
	// do not change the labels, place the required text inside the ['']	
	imageDataLabels: {
		'labelPrev':	['Previous'],
		'labelClose':	['Close'],
		'labelNext':	['Next'],
		'labelRestart': ['Restart'],
		'labelImage':	['Image'],
		'labelOf':	['of']
	},
	// Caption Settings
	// showCaptions [true|false], pretty self explanitary, if false no captions will be
	// displayed, if true captions will be displayed according to extend and append
	// caption settings.
	showCaptions: true,
	// extendedCaptions [true|false], if true enables ability to append
	// HTML elements to the "Regular Caption" or replace the "Regular Caption".
	// The element to be matched is a div with the class "nlbext-caption" this
	// div must be contained within the same parent node as the nlightbox image link
	// and it must be the nextSibling, for example:
	// <li>
	// 	<a href="#" title="Regular Caption" rel="nlightbox">
	// 		<img src="#" alt="#" />
	//	</a>
	//	<div class="nlbext-caption">This is an Extended Caption that can contain HTML elements</div>
	// </li>
	extendedCaptions: true,
	// Append Captions [true|false] default [false], extendedCaptions must be true
	// for appendCaptions to have any effect. if true the "Extended Caption"
	// will be appended to the "Regular Caption" as in the above example. If
	// false only the "Extended Caption" will be used and the "Regular Caption"
	// will be ignored.
	// If appendCaptions is true the following output will be produced:
	// 	Regular Caption
	// 	This is an Extended Caption that can contain HTML elements
	// If appendCaptions is false the following output will be produced:
	// 	This is an Extended Caption that can contain HTML elements
	appendCaptions: true,
	// End Caption Settings
	//
	// nlightbox start size, the default size that the nlightbox should always start at
	// start size is in pixels so (150 = 150px)
	nlightboxStartSize: 150,
	// controls transparency of the overlay
	overlayOpacity: 0.7,
	// toggles resizing animations
	animate: true,
	 // controls the speed of the image resizing animations (1=slowest and 10=fastest)
	resizeSpeed: 7,
	 //if you adjust the padding in the CSS, you will need to update this variable
	borderSize: 10
},window.nLightboxOptions || {});

// -----------------------------------------------------------------------------------

var nLightbox = Class.create();

nLightbox.prototype = {
	imageArray: [],
	activeImage: undefined,

	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow
	// overlay and the image container.
	//
	initialize: function() {
	// first preload the interface images
	//this.preloadInterface();
		this.updateImageList();

		this.keyboardAction = this.keyboardAction.bindAsEventListener(this);

		if (nLightboxOptions.resizeSpeed > 10) nLightboxOptions.resizeSpeed = 10;
		if (nLightboxOptions.resizeSpeed < 1)  nLightboxOptions.resizeSpeed = 1;

		this.resizeDuration = nLightboxOptions.animate ? ((11 - nLightboxOptions.resizeSpeed) * 0.15) : 0;
		this.overlayDuration = nLightboxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

		// When nLightbox starts it will resize itself from
		// nLightboxOptions.nlightboxStartSize by nLightboxOptions.nlightboxStartSize
		// to the current image dimension.
		// If animations are turned off, it will be hidden as to prevent a flicker of a
		// nLightboxOptions.nlightboxStartSize by nLightboxOptions.nlightboxStartSize box.
		var size = (nLightboxOptions.animate ? nLightboxOptions.nlightboxStartSize : 1) + 'px';

		// Code inserts html at the bottom of the page that looks similar to this:
		//
		//  <div id="overlay"></div>
		//  <div id="nlightbox">
		//	  <div id="nlightbox-container">
		//		  <div id="image-container">
		//			  <img id="nlightbox-image">
		//			  <div id="loading">
		//				  <a href="#" id="loading-link">
		//					  <img src="images/loading.gif">
		//				  </a>
		//			  </div>
		//		  </div>
		//	  </div>
		//	  <div id="image-data-container">
		//		  <div id="image-data">
		//			  <div id="image-details">
		//				  <span id="caption"></span>
		//				  <span id="number-display"></span>
		//			  </div>
		//			  <ul id="bottom-nav">
		//				<li id="prev-link"><a id="prev-link-anchor" href="#"></a></li>
		//				<li id="close-link"><a id="close-link-anchor" href="#"></a></li>
		//				<li id="next-link"><a id="next-link-anchor" href="#"></a></li>
		//			  </ul>
		//		  </div>
		//	  </div>
		//  </div>


		var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div', {id:'overlay'}));

		objBody.appendChild(Builder.node('div', {id:'nlightbox'}, [
			Builder.node('div', {id:'nlightbox-container'},
				Builder.node('div', {id:'image-container'}, [
					Builder.node('img', {id:'nlightbox-image'}),
					Builder.node('div', {id:'loading'},
						Builder.node('a', {id:'loading-link', href: '#' },
							Builder.node('img', {src: nLightboxOptions.baseUrl + nLightboxOptions.throbber})
						)
					)
				])
			),
			Builder.node('div', {id:'image-data-container', className: 'closed'},
				Builder.node('div', {id:'image-data'}, [
					Builder.node('div', {id:'image-details'}, [
						Builder.node('div', {id:'caption'}),
						Builder.node('span', {id:'number-display'})
					]),
					Builder.node('ul', {id:'bottom-nav'}, [
				Builder.node('li', {id:'prev-link', className: 'disabled'}, [
						Builder.node('a', {id:'prev-link-anchor', href: '#' }, [
					Builder._text(nLightboxOptions.imageDataLabels.labelPrev[0])
				])
			]),
			Builder.node('li', {id:'close-link'}, [
						Builder.node('a', {id:'close-link-anchor', href: '#' }, [
					Builder._text(nLightboxOptions.imageDataLabels.labelClose[0])
				])
			]),
			Builder.node('li', {id:'next-link', className: 'disabled'}, [
						Builder.node('a', {id:'next-link-anchor', href: '#' }, [
					Builder._text(nLightboxOptions.imageDataLabels.labelNext[0])
				])
			])
					])
				])
			)
		]));
		
		$('prev-link').setOpacity(0.1);
		$('next-link').setOpacity(0.1);

		// Safari 2.0.3 back to 1.3.2 (and possibly lower) doesn't honor event.stop();
		// or event.preventDefault(); So far there is no work around that I can find
		// for this issue so for now tough luck to users of these browsers, I'm not
		// going to start browser sniffing for old browsers.
		$('overlay').hide().observe('click', (
			function(event) {
				this.end();
				event.stop();
			}
		).bind(this));
		
		$('nlightbox').hide().observe('click', (
			function(event) {
				if (event.element().id == 'nlightbox') {
					this.end();
				}
				event.stop();
			}
		).bind(this));
		
		$('nlightbox-container').setStyle({ width: size, height: size });
		$('nlightbox-image').hide();
		$('image-data-container').hide();
		
		$('prev-link-anchor').observe('click', (
			function(event) {
				if ($('prev-link').className != 'disabled') {
					this.changeImage(this.activeImage - 1);
				}
				event.stop();
			}
		).bindAsEventListener(this));

		$('next-link-anchor').observe('click', (
			function(event) {
				if ($('next-link').className != 'disabled') {
					if (this.imageArray.length > 1 && this.activeImage == (this.imageArray.length -1)) {
						this.changeImage(this.activeImage - this.imageArray.length + 1);
					} else {
						this.changeImage(this.activeImage + 1);
					}
				}
				event.stop();
			}
		).bindAsEventListener(this));

		$('loading-link').observe('click', (
			function(event) {
				this.end();
				event.stop();
			}
		).bind(this));
		
		$('close-link-anchor').observe('click', (
			function(event) {				
				this.end();
				event.stop();
			}
		).bind(this));

		var th = this;
		(function(){
			var ids =
				'overlay nlightbox nlightbox-container image-container nlightbox-image prev-link prev-link-anchor close-link close-link-anchor next-link next-link-anchor loading loading-link ' +
				'image-data-container image-data image-details caption number-display bottom-nav';
			$w(ids).each(function(id){ th[id] = $(id); });
		}).defer();
	},

	//
	// updateImageList()
	// Loops through anchor tags looking for 'nlightbox' references and applies onclick
	// events to appropriate links. You can rerun after dynamically adding images w/ajax.
	//
	updateImageList: function() {
		this.updateImageList = Prototype.emptyFunction;

		document.observe('click', (
			function(event){
				//if (event && event.preventDefault) event.preventDefault();
				var target = event.findElement('a[rel^=nlightbox]') || event.findElement('area[rel^=nlightbox]');
				if (target) {				
					this.start(target);
					event.stop();
				}					
			}
		).bind(this));
	},

	//
	//  start()
	//  Display overlay and nlightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function(imageLink) {

		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

		// stretch overlay to fill page and fade in
		var arrayPageSize = this.getPageSize();
		$('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

		new Effect.Appear(this.overlay, {
			duration: this.overlayDuration,
			from: 0.0,
			to: nLightboxOptions.overlayOpacity 
		});

		this.imageArray = [];
		var imageNum = 0;
		var captionNode;		
			
		if ((imageLink.rel == 'nlightbox')){
			// Check if we are using extended captions
			if ( nLightboxOptions.showCaptions === true ){
				if ( nLightboxOptions.extendedCaptions === true ){				
					captionNode = imageLink.nextSibling;
					while ( captionNode && captionNode.nodeType == "3" ){
						captionNode = captionNode.nextSibling;
					}
					// make sure we found an extended caption
					if (!captionNode) {					
						captionNode = imageLink.title;
					} else {
						if (nLightboxOptions.appendCaptions === true){
							captionNode = imageLink.title + ' ' + captionNode.innerHTML;
						}
					}
				} else {
					captionNode = imageLink.title;	
				}
			}
			// if image is NOT part of a set, add single image to imageArray
			this.imageArray.push([imageLink.href, captionNode]);			
		} else {
			// if image is part of a set..			
			this.imageArray =
				$$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').collect(function(anchor){
					// Check if we are using extended captions			
					if ( nLightboxOptions.showCaptions === true ){
						if ( nLightboxOptions.extendedCaptions === true ){				
							captionNode = anchor.nextSibling;
							while ( captionNode && captionNode.nodeType == "3" ){
								captionNode = captionNode.nextSibling;
							}
							// make sure we found an extended caption
							if (!captionNode) {
								captionNode = anchor.title;
							} else {
								if (nLightboxOptions.appendCaptions === true){
									captionNode = anchor.title + ' ' + captionNode.innerHTML;
								}
							}
						} else {
							captionNode = anchor.title;
						}
					}
					return [anchor.href, captionNode]; 
				}).uniq();			
			while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
		}

		// calculate top and left offset for the nlightbox
		var arrayPageScroll = document.viewport.getScrollOffsets();
		var nlightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
		var nlightboxLeft = arrayPageScroll[0];
		$('nlightbox').setStyle({ top: nlightboxTop + 'px', left: nlightboxLeft + 'px' }).show();

		if ($('image-data-container').className.match(/closed/)) {
			this.firstImage(imageNum);
		} else {
			this.changeImage(imageNum);
		}
	},
	//
	//  firstImage()
	//  different to changeImage() we don't want the nav to slide up yet
	//
	firstImage: function(imageNum) {
		this.activeImage = imageNum;	// update global var
		imgPreloader = new Image();
		new Effect.Parallel(
		[
			new Effect.Appear( 'loading', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.Fade( 'nlightbox-image', {
				sync: true,
				duration: this.resizeDuration
			})
		],
		{
			duration: this.resizeDuration,
			afterFinish: (function() {
				imgPreloader.onload = (function(){
					$('nlightbox-image').src = this.imageArray[this.activeImage][0];
					// workaround for Safari - make sure the image dimentions are set
					$('nlightbox-image').setStyle({ width: imgPreloader.width + 'px', height: imgPreloader.height + 'px' });				
					this.resizeImageContainer(imgPreloader.width, imgPreloader.height);
				}).bind(this);
				imgPreloader.src = this.imageArray[this.activeImage][0];
			}).bind(this)
		});

	},
	//
	//  changeImage()
	//  Hide most elements and preload image in preparation for resizing image container.
	//
	changeImage: function(imageNum) {
		this.activeImage = imageNum;
		var imgPreloader = new Image();
		new Effect.Parallel(
		[
			new Effect.Appear( 'loading', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.Fade( 'nlightbox-image', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.Fade( 'image-data-container', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.SlideUp( 'image-data-container', {
				sync: true,
				duration: this.resizeDuration
			})
		],
		{
			duration: this.resizeDuration,
			afterFinish: (function() {
				imgPreloader.onload = (function(){
					$('nlightbox-image').src = this.imageArray[this.activeImage][0];
					// workaround for Safari - make sure the image dimentions are set
					$('nlightbox-image').setStyle({ width: imgPreloader.width + 'px', height: imgPreloader.height + 'px' });				
					this.resizeImageContainer(imgPreloader.width, imgPreloader.height);					
				}).bind(this);
				imgPreloader.src = this.imageArray[this.activeImage][0];
			}).bind(this)
		});
	},

	//
	//  resizeImageContainer()
	//
	resizeImageContainer: function(imgWidth, imgHeight) {

		// get current width and height
		var widthCurrent  = $('nlightbox-container').getWidth();
		var heightCurrent = $('nlightbox-container').getHeight();

		// get new width and height
		var widthNew  = (imgWidth  + nLightboxOptions.borderSize * 2);
		var heightNew = (imgHeight + nLightboxOptions.borderSize * 2);

		// scalars based on change from old to new
		var xScale = (widthNew  / widthCurrent)  * 100;
		var yScale = (heightNew / heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		var wDiff = widthCurrent - widthNew;
		var hDiff = heightCurrent - heightNew;

		// this part of the script is being executed too soon, image is being
		// shown before container has finished resizing. Need to try running
		// a parallel effect and showImage() afterFinish.

		new Effect.Parallel(
		[
			new Effect.Scale('nlightbox-container', yScale, {
				scaleX: false,
				duration: this.resizeDuration,
				queue: 'front'
			}),
			new Effect.Scale('nlightbox-container', xScale, {
				scaleY: false,
				duration: this.resizeDuration,
				delay: this.resizeDuration
			})
		],
		{
			duration: this.resizeDuration,
			afterFinish: (function() {
				$('image-data-container').setStyle({ width: widthNew + 'px' });
				this.showImage();
			}).bind(this)
		});
	},
	//
	//  showImage()
	//  Display image and begin preloading neighbors.
	//
	showImage: function(){
		new Effect.Parallel(
		[
			new Effect.Fade( 'loading', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.Appear( 'nlightbox-image', {
				sync: true,
				duration: this.resizeDuration,
				queue: 'end'
			})
		],
		{
			duration: this.resizeDuration,
			afterFinish: (function() {
				this.updateDetails();
				this.preloadNeighborImages();
			}).bind(this)
		});
	},
	//
	//  updateDetails()
	//  Display caption, image number, and bottom nav.
	//
	updateDetails: function() {
		// if caption is not null
		if (this.imageArray[this.activeImage][1] != ""){
			this.caption.update(this.imageArray[this.activeImage][1]).show();
		}
		// if image is part of set display 'Image x of x'
		if (this.imageArray.length > 1){
			$('number-display').update( nLightboxOptions.imageDataLabels.labelImage[0] + ' ' + (this.activeImage + 1) + ' ' + nLightboxOptions.imageDataLabels.labelOf[0] + '  ' + this.imageArray.length).show();
		}

		new Effect.Parallel(
		[
			new Effect.SlideDown( 'image-data-container', {
				sync: true,
				duration: this.resizeDuration,
				from: 0.0,
				to: 1.0,
				afterFinish: function() {
					$('image-data-container').className = 'open';
				}
			}),
			new Effect.Appear( 'image-data-container', {
				sync: true,
				duration: this.resizeDuration
			})
		],
		{
			duration: this.resizeDuration,
			afterFinish: (function() {
				// update overlay size and update nav
				var arrayPageSize = this.getPageSize();
				this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
				this.updateNav();
			}).bind(this)
		});
	},
	//
	//  updateNav()
	//  Display appropriate previous and next hover navigation.
	//
	updateNav: function() {
		// if not first image in set, display prev image button
		if(this.activeImage != 0) {
			new Effect.Appear( 'prev-link', {
				from: $('prev-link').getOpacity(),
				to: 1.0,
				duration: 0.5,
				afterFinish: (function() {
					$('prev-link').className = 'enabled';
					this.enableKeyboardNav();
				}).bind(this)
			});
		} else {
			new Effect.Fade( 'prev-link', {
				from: $('prev-link').getOpacity(),
				to: 0.1,
				duration: 0.5,
				afterFinish: (function() {
					$('prev-link').className = 'disabled';
					this.enableKeyboardNav();
				}).bind(this)
			});
		}
		// if last image in set, and more than one image in the set
		// display back to start image button
		if (this.imageArray.length > 1 && this.activeImage == (this.imageArray.length -1)){
			new Effect.Fade( 'next-link', {
				from: $('next-link').getOpacity(),
				to: 0.0,
				duration: 0.5,
				afterFinish: (function() {
					$('next-link').className = 'restart';
					new Effect.Appear( 'next-link', {
						from: 0.0,
						to: 1.0,
						duration: 0.5
					})
					this.enableKeyboardNav();
				}).bind(this)
			});
		} else if(this.activeImage != (this.imageArray.length - 1)){
			// if not last image in set, display next image button
			if ($('next-link').className.match(/restart/)) {
				new Effect.Fade( 'next-link', {
					from: $('next-link').getOpacity(),
					to: 0.0,
					duration: 0.5,
					afterFinish: (function() {
						$('next-link').className = 'enabled';
						new Effect.Appear( 'next-link', {
							from: 0.0,
							to: 1.0,
							duration: 0.5
						})
						this.enableKeyboardNav();
					}).bind(this)
				});
			} else {
				new Effect.Appear( 'next-link', {
					from: $('next-link').getOpacity(),
					to: 1.0,
					duration: 0.5,
					afterFinish: (function() {
						$('next-link').className = 'enabled';
						this.enableKeyboardNav();
					}).bind(this)
				});
			}
		} else {
			new Effect.Fade( 'next-link', {
				from: $('next-link').getOpacity(),
				to: 0.1,
				duration: 0.5,
				afterFinish: (function() {
					$('next-link').className = 'disabled';
					this.enableKeyboardNav();
				}).bind(this)
			});
		}
	},
	//
	//  enableKeyboardNav()
	//
	enableKeyboardNav: function() {
		document.observe('keydown', this.keyboardAction);
	},
	//
	//  disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.stopObserving('keydown', this.keyboardAction);
	},
	//
	//  keyboardAction()
	//
	keyboardAction: function(event) {
		var keycode = event.keyCode;

		var escapeKey;
		if (event.DOM_VK_ESCAPE) {  // mozilla
			escapeKey = event.DOM_VK_ESCAPE;
		} else { // ie
			escapeKey = 27;
		}

		var key = String.fromCharCode(keycode).toLowerCase();

		if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close nlightbox
			this.end();
		} else if ((key == 'p') || (keycode == 37)){ // display previous image
		if ($('prev-link').className != 'disabled'){
			if (this.activeImage != 0){
				this.disableKeyboardNav();
				this.changeImage(this.activeImage - 1);
			}
		} else {
			return false;
		}
		} else if ((key == 'n') || (keycode == 39)){ // display next image
		if ($('next-link').className != 'disabled'){
			if (this.imageArray.length > 1 && this.activeImage == (this.imageArray.length -1)) {
				mynLightbox.disableKeyboardNav();
				mynLightbox.changeImage(this.activeImage - this.imageArray.length + 1);
			} else if (this.activeImage != (this.imageArray.length - 1)) {
				this.disableKeyboardNav();
				this.changeImage(this.activeImage + 1);
			}
		} else {
			return false;
		}
		}
	},
	//
	//  preloadInterface()
	//  preloads any images used as a part of the nlightbox interface including the throbber
	//
	preloadInterface: function() {
		var images = [];
		for (var i = 0; i<nLightboxOptions.interfaceImages.length; i++) {
			images[i] = new Image();
			images[i].src = nLightboxOptions.baseUrl + nLightboxOptions.interfaceImages[i];
		}
	},
	//
	//  preloadNeighborImages()
	//  Preload previous and next images.
	//
	preloadNeighborImages: function(){
		var preloadNextImage, preloadPrevImage;
		if (this.imageArray.length > this.activeImage + 1){
			preloadNextImage = new Image();
			preloadNextImage.src = this.imageArray[this.activeImage + 1][0];
		}
		if (this.activeImage > 0){
			preloadPrevImage = new Image();
			preloadPrevImage.src = this.imageArray[this.activeImage - 1][0];
		}

	},
	//
	//  end()
	//
	end: function() {
		this.disableKeyboardNav();

		// get current width and height
		var widthCurrent  = $('nlightbox-container').getWidth();
		var heightCurrent = $('nlightbox-container').getHeight();
		// set reverse scale dimentions
		var xScale = (nLightboxOptions.nlightboxStartSize / widthCurrent) * 100;
		var yScale = (nLightboxOptions.nlightboxStartSize / heightCurrent) * 100;
		
		new Effect.Parallel(
		[
			new Effect.SlideUp( 'image-data-container', {
				sync: true,
				duration: this.resizeDuration
			}),
			new Effect.Fade( 'nlightbox-image', {
				sync: true,
				duration: 0.5,
				afterFinish: (function() {
					new Effect.Parallel(
					[					
						new Effect.Scale('nlightbox-container', xScale, {
							sync: true,
							//scaleX: false,
							duration: this.resizeDuration,
							queue: 'front'
						}),
						new Effect.Scale('nlightbox-container', yScale, {
							sync: true,
							scaleY: false,
							duration: this.resizeDuration,
							delay: this.resizeDuration
						}),
						new Effect.Fade( 'nlightbox', {
							sync: true,
							duration: 0.5				
						}),
						new Effect.Fade('overlay', {
							sync: true,
							duration: 0.5
						})
					],
					{
						duration: this.resizeDuration,
						afterFinish: function() {
							// still got a couple of issues here but this is good for now
							$('nlightbox-container').setStyle({
								width: '',
								height: ''
							});
							$('prev-link').className = 'disabled';
							$('next-link').className = 'disabled';
							$('image-data-container').className = 'closed';	
						}
					})
				}).bind(this)
			})
		],
		{
			duration: this.resizeDuration
		});					
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	},

	//
	//  getPageSize()
	//
	getPageSize: function() {

		 var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			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
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				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;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}

document.observe('dom:loaded', function () { new nLightbox(); });