window.addEvent('domready',function(){
	var debug = '';
	var altImages = $$('#alternative-images li');
	// Only if there are more than two alternative images
	if (altImages.length > 2) {
		// Creates the Image Gallery Options
		var galleryOptions = new Element('ul', {
			'id':'image-gallery-options'
		});
		var galleryOptionsHTML =
			'<li class="first-child"><a href="#images-recommendations" class="view-gallery">Click here to view the entire gallery</a></li>';
		galleryOptions.setHTML(galleryOptionsHTML);
		// Adds the Image Gallery Options into the Image Gallery
		$('image-gallery').adopt(galleryOptions);

		// Sets the top position of the Image Gallery Options
		$('image-gallery-options').setStyle('top',$('alternative-images').offsetTop + $('alternative-images').getStyle('padding-top').toInt() + altImages[0].scrollHeight + altImages[0].getStyle('margin-bottom').toInt());

		// Needed because of certain browsers changing scrollHeight value once height is set
		var galleryHeightOffset = 0;
		galleryHeightOffset = $('image-gallery').scrollHeight;
//debug += 'galleryHeightOffset' + galleryHeightOffset;
		// Creates effect for revealing rest of gallery
		var showImage = new Fx.Style('image-gallery','height',{duration: 700});
		// Sets starting height of Image Gallery
		showImage.set($('alternative-images').offsetTop + $('alternative-images').getStyle('padding-top').toInt() + altImages[0].scrollHeight + $('image-gallery-options').clientHeight + altImages[0].getStyle('margin-bottom').toInt());
//debug += 'alternative-images.offsetTop' + $('alternative-images').offsetTop + ',' + $('alternative-images').getStyle('padding-top').toInt() + ',' + altImages[0].scrollHeight + ',' + $('image-gallery-options').clientHeight + altImages[0].getStyle('margin-bottom').toInt();
//debug += 'galleryScrollHeight' + $('image-gallery').scrollHeight;

		// Checks for discrepancy between scrollHeights before and after height change
		if (galleryHeightOffset > $('image-gallery').scrollHeight) {
			galleryHeightOffset -= $('image-gallery').scrollHeight;
		} else {
			galleryHeightOffset = 0;
		}
//debug += 'gallery.scroll-height' + $('image-gallery').scrollHeight + 'galleryHeightOffset' + galleryHeightOffset;
		// Creates onClick event for the View Gallery link
		$$('#images-recommendations a.view-gallery').each(function(link){
			link.addEvent('click',function(e){
				var ev = new Event(e).stop(); // instead of return false
				var fadeGalleryOptions = new Fx.Style('image-gallery-options','opacity').addEvent('onComplete', function(){
					showImage.addEvent('onComplete', function(){
						$$('#images-recommendations p.instructions').setStyle('display','block');
					});
					showImage.start($('image-gallery').scrollHeight + galleryHeightOffset);
				});
				fadeGalleryOptions.start(0);
			});
		});
	}
//	alert(debug);
//	alert($('alternative-images').offsetTop);
});

