// JavaScript Document
Window.onDomReady(
	function() {
		/*var myColorFx = new Fx.Color($E('body'), 'background-color', {duration:2500}).custom('FFFFFF','000000');*/
		$('photoGallery-JSError').remove();
		$('photoGallery').setOpacity(0);

		currentphoto = 0;

		// Prepare Interaction Behavior
		$('photoGallery-Previous').addEvent('click',function() {
			if(currentphoto > 0) {currentphoto = currentphoto-1;}
			else {currentphoto = totalphotos;}
			photoGalleryUpdate();
		});

		$('photoGallery-Next').addEvent('click',function() {
			if(currentphoto < totalphotos) {currentphoto++;}
			else {currentphoto = 0;}
			photoGalleryUpdate();
		});

		function photoGalleryUpdate() {
			var myFx = new Fx.Style($('photoGallery-Photo'), 'opacity').custom(0.5,1);
			$('photoGallery-Photo').setProperty('src',photoGallery[currentphoto][0]);
			$('photoGallery-Photo').setProperty('width',photoGallery[currentphoto][1]);
			$('photoGallery-Photo').setProperty('height',photoGallery[currentphoto][2]);
			$('photoGallery-Caption').setHTML(photoGallery[currentphoto][3]);
		}

		// Prepare Photo Gallery
		i = -1;
		var photoGallery = new Array;
		$('photoGallery').getElements('p').each(function(p) {
			p.getElements('img').each(function(img) {
				if(i > -1) {
					photoGallery[i] = new Array;
					photoGallery[i][0] = img.getProperty('src');
					photoGallery[i][1] = img.getProperty('width');
					photoGallery[i][2] = img.getProperty('height');
					photoGallery[i][3] = img.getProperty('alt');
					if(i == 0) {
						$('photoGallery-Photo').setProperty('src',photoGallery[i][0]);
						$('photoGallery-Photo').setProperty('width',photoGallery[i][1]);
						$('photoGallery-Photo').setProperty('height',photoGallery[i][2]);
						$('photoGallery-Caption').setHTML(photoGallery[i][3]);
					}
					img.setOpacity(0.5);
					img.setProperties({
						height: '30',
						width:  '50',
						id: 'photo'+i
					});
					p.setStyle('display','inline');
					img.setStyle('margin-right','2px');
					img.setStyle('margin-top','4px');
					img.addEvent('mouseover',function() {
						var myFx = new Fx.Style(img, 'opacity').custom(0,1);
						//img.setOpacity(1);
					});
					img.addEvent('mouseout',function() {
						var myFx = new Fx.Style(img, 'opacity').custom(1,0.5);
						//img.setOpacity(0.5);
					});
					img.addEvent('click',function() {
						currentphoto = img.getProperty('id').substr(5);
						photoGalleryUpdate();
					});
					//img.remove();
				}
				i++;
			});
		});
		totalphotos = i-1;

		$('photoGallery-Previous').setOpacity(1);
		$('photoGallery-Next').setOpacity(1);
		$('photoGallery').setOpacity(1);

	}
);