var gallery;

window.addEvent ('domready', function () {

	//try {
		gallery = new Gallery();

//	} catch (e) {}	
})


var Gallery = new Class ({
	initialize: function () {
		this.busy = false;
		var colors = $$('.color_center');
		this.photo_cont = $$('.prod_photo');
		this.lupa = $('lupa_a');
		this.fx = Array ();
		this.fx_div = Array();
		this.photo = $('big_photo');
		this.photo_cont.each (function (item, i) {
			
			this.fx[i] = new Fx.Elements (item.getElements('img'));
			//this.fx_div[i] = new Fx.Elements(item.getElements('div'));

			
			item.getElements('img').each (function (img, k) {
				if (k != 0) {
					img.setStyle('opacity', 0);
				}
				
				img.addEvent('click', function () {
					if (!this.busy) {
						this.busy = true;
						this.photo.set('tween', {
							onComplete: function(){
								
								this.photo.setProperty('src', img.getProperty('src').replace('96x116', '260x314'));
								//this.photo.getParent('a').setProperty('href', img.getProperty('src').replace('96x116', '650x785'));

								milkbox.reloadGalleries();
								
								this.photo.set ('tween', {
									onComplete: function () {
										
									}
								}).tween('opacity', 1)
								
								this.busy = false;
								
							}.bind(this)
						}).tween('opacity', 0)
					}
				}.bind(this))
			}.bind(this))
		}.bind(this))
		
		colors.each (function (item, i) {
			item.addEvent('click', function () {
				this.changePhotos (i);
			}.bind(this))
		}.bind(this))	
	},
	
	changePhotos: function (index) {
		var i_img = null
		
		this.photo_cont.each (function (item, i) {
			var obj = {}
			
			item.getElements('img').each (function (img, k) {
				
				if (k == index) {
					obj[k] = { 
						'opacity': 1
					}
					if (!i_img) {
						i_img = img
					} 
						
					this.photo.set('tween', {
						onComplete: function(){
							
							this.photo.setProperty('src', i_img.getProperty('src').replace('96x116', '260x314'));
							//this.photo.getParent('a').setProperty('href', img.getProperty('src').replace('96x116', '650x785'));

							
							milkbox.reloadGalleries();
							
							this.photo.set ('tween', {
								onComplete: function () {
									
								}
							}).tween('opacity', 1)
							
							this.busy = false;
					
						}.bind(this)
					}).tween('opacity', 0)					
					
					
				} else {
					obj[k] = { 
						'opacity': 0
					}						
				}
			}.bind(this))		
				
			this.fx[i].start(obj)
			//this.fx_div[i].start(obj)
			
		}.bind(this))
	}
})

