window.addEvent('domready', function(){
	$$('div.product-detail .image-thumbnails img').each(function(img){
		var gallery = $(img.getProperty('rel'));
		if(gallery){
			var slider = new GallerySlider(gallery);
			img.addEvent('click', function(){
					$$('.product-detail .image-preview .gallery').each(function(gal){
						gal.setStyle('display', 'none');
					});
					gallery.setStyle('display', '');
					slider.reset();
			});
		}
	});

	$$('.product-detail .image-preview .gallery').each(function(gal, idx){
		if(idx>0){
			gal.setStyle('display', 'none');
		}
	});
	
	/* initialize images */
	var box = new Element('div', {
		'id': 'product_image_lightbox'
	});
	var overlay = new Element('div', {
		'class': 'overlay',
		'styles':{
			'opacity': 0.9
		}
	}).addEvent('click', function(){
		box.setStyle('display', 'none');
	});
	var lightbox = new Element('div', {
		'class': 'lightbox'
	});
	box.adopt([overlay,lightbox]);
	$(document.body).adopt(box);
	
	$$('.product-detail .image-preview .gallery img').each(function(img){
		img.addEvent('click', function(){
			lightbox.empty();
			var detail = new Element('div', {
				'class':'detail',
			});
			
			detail.adopt(new Element('div', {
				'class':'close'
			}).addEvent('click', function(){
				box.setStyle('display', 'none');
			}));
			var imgclone = img.clone();
			detail.adopt(imgclone);
			lightbox.adopt(detail);
			box.setStyle('display', 'block');
			
			overlay.setStyles({
				'width': '100%',
				'min-height': $(document.body).getScrollSize().y,
				'height': '100%'
			});
			
			var diff = detail.getSize().y-imgclone.getSize().y-20;
			imgclone.setStyle('margin-top', (diff>0)?diff/2:0);
		});
	})
	
	// Currency selection
	var prices = $$('.product-detail .prices .price');
	var currencies = $$('.product-detail .change-currency .currency');
	currencies.each(function(cur){
		cur.addEvent('click', function(){
			prices.setStyle('display', 'none');
			$(cur.getProperty('id')+'_price').setStyle('display', 'block');
			currencies.removeClass('selected');
			cur.addClass('selected');
			//Cookie.write('tx_xcibshop_pi[currency]', cur.getProperty('rel'), {duration: 30, path: '/'});
			new Request({
				'url': cur.getProperty('rel'),
				'method': 'get'
			}).send('tx_xcibshop_pi1[exit]=1');
		});
	});
	
	// Attributes selection
	$$('.product-detail .attribute').each(function(attr){
		var input = attr.getElement('input');
		var values = attr.getElements('.values .value');
		var button = attr.getElements('.button .color');
		if(input && values){
			values.addEvent('click', function(){
				input.setProperty('value', this.getProperty('rel'));
				values.removeClass('selected');
				this.addClass('selected');
				if(button){
					button.set('text', this.get('text'));
				}
			});
		}
	});
});

var GallerySlider = new Class({
	Implements: [Options],
	
	options:{
		controllers:{
			next: '.next',
			previous: '.previous'
		},
		imageHandler: '.image'
	},

	initialize: function(gal, options){
		this.setOptions(options);
		
		this.gallery = $(gal);
		this.images = this.gallery.getElements(this.options.imageHandler);

		this.gallery.getElements(this.options.controllers.next).each(function(next){
			next.addEvent('click', function(){
				this.next();
			}.bind(this));
		}.bind(this));
		this.gallery.getElements(this.options.controllers.previous).each(function(previous){
			previous.addEvent('click', function(){
				this.previous();
			}.bind(this));
		}.bind(this));
		
		this.reset();
	}, 
	
	reset: function(){
		this.current = 0;
		this.showImage(this.current);
	},
	
	next: function(){
		if((this.current+1)<this.images.length){
			this.current++;
		}
		this.showImage(this.current);
	},
	
	previous: function(){
		if(this.current>0){
			this.current--;
		}
		this.showImage(this.current);
	},
	
	showImage: function(idx){
		this.images.setStyle('display', 'none');
		this.images[idx].setStyle('display', '');
	}
});
