var photoset = {
	myPhotoTotal: 0,
	myPhotoCurrent: 0,
	myPhotoAnimation: null,
	isAnimating: false,
	isInitialized: false,
	initialize: function() {
		var myPhotos = $$('#photos img');
		photoset.myPhotoTotal = myPhotos.length;
		photoset.myPhotoAnimation = new Fx.Tween('photos',{duration: 300, transition: Fx.Transitions.Quad.easeInOut});
		var myClone = $('current-clone');
		for(i=0; i<myPhotos.length; i++) {
			var myButton = myPhotos[i];
			myButton.id = 'photo' + (i+1);
			var myButtonClone = myClone.clone();
			myButtonClone.id = 'current' + (i+1);
			myButtonClone.inject('current-photo');
			myButtonClone.href = 'javascript:photoset.goto('+ (i+1)*1 +')';
			if (i==0) {
				var myPhotoHeight = myButton.getCoordinates().height;
				$('photos').setStyle('height', myPhotoHeight);
				photoset.myPhotoCurrent = 1;
				myButtonClone.addClass('active');
			} else {
				myButton.setStyle('opacity',0);
				myButton.setStyle('display','none');
			}
		}
		photoset.isInitialized = true;
	},
	next: function() {
		if (photoset.isInitialized) {
			var myPhotoNext = photoset.myPhotoCurrent + 1;
			if (myPhotoNext > photoset.myPhotoTotal) {
				myPhotoNext = 1;
			}
			photoset.goto(myPhotoNext);
		}
		return false;
	},
	previous: function() {
		if (photoset.isInitialized) {
			var myPhotoPrev = photoset.myPhotoCurrent - 1;
			if (myPhotoPrev < 1) {
				myPhotoPrev = photoset.myPhotoTotal;
			}
			photoset.goto(myPhotoPrev);
		}
		return false;
	},
	goto: function(myID) {
		if(photoset.isAnimating == false) {
			photoset.isAnimating = true;
			var myPhotoCurrent = $('photo' + photoset.myPhotoCurrent);
			var myPhotoNew = $('photo' + myID);
			var myButtonCurrent = $('current' + photoset.myPhotoCurrent);
			var myButtonNew = $('current' + myID);
			myPhotoCurrent.set('tween', {
				duration: 200,
				transition: Fx.Transitions.Quad.easeInOut,
				onComplete: function() {
					myButtonCurrent.removeClass('active');
					myPhotoCurrent.setStyle('display','none');
				}
			});
			myPhotoCurrent.tween('opacity', 0);
			function myPhotoResize() {
				myPhotoNew.setStyle('display','block');
				var myPhotoNewSize = myPhotoNew.getCoordinates().height;
				photoset.myPhotoAnimation.start('height', myPhotoNewSize);
			}
			function myPhotoFade() {
				myPhotoNew.set('tween', {
					duration: 200,
					transition: Fx.Transitions.Quad.easeInOut,
					onComplete: function() {
						myButtonNew.addClass('active');
						photoset.isAnimating = false;
					}
				});
				myPhotoNew.tween('opacity', 1);
			}
			myPhotoResize.delay(200);
			myPhotoFade.delay(300);
			photoset.myPhotoCurrent = myID;
		}
	}
}
window.addEvent('load', photoset.initialize);
