var isSliding = false;
var currentIndex = 0;

window.addEvent('domready', function() {
	ssNext();
	ssPrev();
	ssDetail();
	ssDetailClose();
	window.box = new CeraBox();
	stillLife();
});

function stillLife()
{
	try
	{
		window.box.addItems('#collection-detail a.still-life', { animation: 'ease', displayTitle: false });
	}
	catch(e) { }
}

function ssNext() {
	$$('.ss-next a').addEvents({
		click: function() {
			if (!isSliding) {
				isSliding = true;
				var photo = this.getParent().getParent().getNext();
				var photosNo = photo.getElements('.pic').length;
				var photoWidth = photo.getElement('img').getStyle('width').toInt();
				var photoMargin = photo.getStyle('margin-left').toInt();
				var photoMarginNew = photoMargin - (photoWidth * imgToScroll);
				var photosWidthTotalPond = photoWidth*(photosNo - galleryImagesCircularPadding + 1);
				if(photosWidthTotalPond - (-photoMarginNew) <= 0 ) {
					photo.setStyle('margin-left', 0);
					photoMarginNew = -(photoWidth * imgToScroll)
				}
				new Fx.Tween(photo,{'transition':'quad:out'}).start('margin-left', photoMarginNew).chain(function() {
					isSliding = false;
				});
				// Modifico il layer di dettaglio
				currentIndex = (- photoMarginNew) / photoWidth;
				try {
					var currItem = $$('#collection-detail-hidden div.detail-item')[currentIndex];
					$('collection-detail-inner').set('html', currItem.get('html'));
					//Cufon.replace('#collection-detail-inner', { fontFamily: 'ITC Avant Garde Gothic' });
					$('Print').set('href', currItem.getAttribute('link'));
				} catch(e) {}
				stillLife();
			}
			return false;
		}
	});
}

function ssPrev() {	
	$$('.ss-prev a').addEvents({
		click: function() {
			if (!isSliding) {
				isSliding = true;
				var photo = this.getParent().getParent().getNext();
				var photosNo = photo.getElements('.pic').length;
				var photoWidth = photo.getElement('img').getStyle('width').toInt();
				var photoMargin = photo.getStyle('margin-left').toInt();
				var photoMarginNew = photoMargin + (photoWidth * imgToScroll);
				var photosWidthTotalPond = photoWidth*(photosNo - galleryImagesCircularPadding + 1);
				if(photoMarginNew > 0 ) {
					photo.setStyle('margin-left', -(photosWidthTotalPond-photoWidth));
					photoMarginNew = -(photosWidthTotalPond - (photoWidth * (imgToScroll+1)))
				}
				new Fx.Tween(photo,{'transition':'quad:out'}).start('margin-left', photoMarginNew).chain(function() {
					isSliding = false;
				});
				// Modifico il layer di dettaglio
				currentIndex = (- photoMarginNew) / photoWidth;
				try {
					var currItem = $$('#collection-detail-hidden div.detail-item')[currentIndex];
					$('collection-detail-inner').set('html', currItem.get('html'));
					//Cufon.replace('#collection-detail-inner', { fontFamily: 'ITC Avant Garde Gothic' });
					$('Print').set('href', currItem.getAttribute('link'));
				} catch(e) {}
				stillLife();
			}
			return false;
		}
	});
}

function ssDetail() {	
	$$('.ss-detail a').addEvents({
		click: function() {
			$$('#collection-detail').setStyles({
				'display': 'block'
			});
			$$('.ss-detail').setStyle('display', 'none');
			$$('.ss-detail-close').setStyle('display', 'block');
			return false;
		}
	});
}

function ssDetailClose() {	
	$$('.ss-detail-close a').addEvents({
		click: function() {
			$$('#collection-detail').setStyles({
				'display': 'none'
			});
			$$('.ss-detail-close').setStyle('display', 'none');
			$$('.ss-detail').setStyle('display', 'block');
			return false;
		}
	});
}

