var Photographs = {

	currentIndex: 0,

	currentPhotograph: null,

    nextPhotograph: null,

	fadeDuration: 1000,

	list: null,

	timer: null,

	timerDuration: 7000,
	
	init: function() {
		// photograph block exist?
		if ($('photographBlock')) {
			var el = $('thumbnailsControl');
			
			Photographs.list = $('photographBlock').getElements('.bigShow').setStyle('opacity', 0);
			if (Photographs.list.length > 0) {
				// set up controls initially
				Photographs.list[0].setStyle('opacity', 1);
				Photographs.currentPhotograph = Photographs.list[0].get('id');

				if (el) {
					$(el).getElements('a').addEvent('click', function(e) {
						e.stop();

						$clear(Photographs.timer);

						var id = this.get('id').match(/[a-z]+\-(\d+)/i)[1];
						Photographs.showPhotographVague(id);
					});

					if (Photographs.list.length > 1) {
						Photographs.timer = Photographs.switchPhotos.periodical(Photographs.timerDuration);
					}
				}
			}
		}	
	},

	
	showPhotographVague: function(id) {
        Photographs.nextPhotograph = $('bigPhotograph-' + id);
        $(Photographs.nextPhotograph).removeClass('hidden').addClass('show');

 		// swap photo information panel
		$(Photographs.currentPhotograph).getNext('.photographVague').removeClass('show').addClass('hidden');
		$(Photographs.nextPhotograph).getNext('.photographVague').removeClass('hidden').addClass('show');

        var inFx = new Fx.Tween($(Photographs.nextPhotograph), {
            duration: Photographs.fadeDuration,
			link: 'cancel',
			property: 'opacity'
		}).start(1);

        var outFx = new Fx.Tween($(Photographs.currentPhotograph), {
			duration: Photographs.fadeDuration,
			link: 'cancel',
			property: 'opacity',
            onComplete: function(el) {
                el.removeClass('show').addClass('hidden');
            }
		}).start(0);

        Photographs.currentPhotograph = Photographs.nextPhotograph;
	},

	switchPhotos: function() {
		var listLength = Photographs.list.length;
		if (Photographs.currentIndex >= listLength - 1) {
			Photographs.currentIndex = 0;
		} else {
			Photographs.currentIndex++;
		}

		var nextId = Photographs.list[Photographs.currentIndex].get('id').match(/[a-z]+\-(\d+)/i)[1];
		Photographs.showPhotographVague(nextId);
	}
};

window.addEvent('domready', Photographs.init);
