var Gallery, duration = 1, videos = [];

Event.observe(window, 'keydown', function(e) {
	if (e.keyCode == 16) {
		duration = 5;
	}
	Effect.DefaultOptions.duration = duration;
});

Event.observe(window, 'keyup', function(e) {
	if (e.keyCode == 16) {
		duration = 1;
	}
	Effect.DefaultOptions.duration = duration;
});

Event.observe(window, 'load', function() {
	videos.each(function(video) {
		init_swfobject(video);
	});
});

Gallery = Class.create({
	initialize: function(element, images, settings) {
		var self = this;
		
		this.element = $(element);
		
		this.element.innerHTML = '<div class="container"><div class="title"><div class="name"></div><div class="location"></div></div><div class="big"><div class="left"></div><div class="right"></div></div><div class="thumbnails"><div class="wrapper"></div></div></div>';
		this.element.className = 'gallery';
		this.element.hide();
		
		this.container = this.element.down('.container');
		this.thumbnails = this.element.down('.wrapper');
		this.big = this.element.down('.big');
		this.spinner = new Element('img', {'src': 'images/spinner.gif'});
		this.spinner.className = 'spinner';
		
		this.right_button = this.element.down('.right');
		this.left_button = this.element.down('.left');
		this.right_button.observe('click', function() {self.change(1);});
		this.left_button.observe('click', function() {self.change(-1);});
		
		this.current_image = 0;
		this.big_path_template = settings.prefix + "/#i.jpg";
		this.thumbnail_path_template = settings.prefix + "/#ipreview.jpg";
		this.images = images.collect(function(i) {
			var b = self.big_path_template.replace('#i', i);
			var t = self.thumbnail_path_template.replace('#i', i);
			if (b.include('.png')) {
				b = b.replace('.jpg', '');
			}
			if (t.include('.png')) {
				t = t.replace('.png', '');
			}
			return {
				big: b,
				thumbnail: t
			};
		});
		this.thumbnails.hide();
		
		this.lock = false;
		this.settings = settings;
		
		this.element.observe('click', this.click.bind(this));
		Event.observe(window, 'resize', this.resize.bind(this));
		
		this.activator = this.element.previous(settings.activator);
		this.activator.observe('click', function(e) {
			Event.stop(e);
			self.show(parseInt(Event.element(e).alt));
		});
		
		this.build();
	},
	build: function() {
		var self = this, div, width = 0;
		
		this.container.down('.name').innerHTML = this.settings.name;
		if (this.settings.location) {
			this.container.down('.location').innerHTML = "(" + this.settings.location + ")";
		}
		
		this.images.each(function(i, index) {
			this.activator.insert('<img alt="' + index + '" class="thumbnail" src="' + this.images[index].thumbnail + '" />');
		}, this);
		
		//this.thumbnails.style.left = '215px';
	},
	click: function(e) {
		var element = Event.element(e);
		
		if (element.className == 'thumbnail') {
			this.show(parseInt(element.alt, 10), e);
		}
		
		if (element.className == 'gallery' || element.className == 'thumbnails' || element.className == 'image') {
			this.hide();
		}
	},
	show: function(index, e, initial) {
		var self = this;
		
		if (this.lock) {
			return;
		}
		
		if (!initial) {
			var d = $('bgdfbd').getDimensions();
			this.element.style.width = d.width + 'px';
			this.element.style.height = d.height + 'px';
			
			this.container.style.top = scrolltop() + 180 + 'px';
			this.element.appear();
		}
		
		var self = this, image, current = this.big.down('.image'),
			thumbnail = e ? Event.element(e) : this.thumbnails.select('img')[index], move_effect, from, to;
		
		//if (this.previous_thumbnail) {
		//	this.previous_thumbnail.appear();
		//}
		//thumbnail.fade({to: 0.3});
		//this.previous_thumbnail = thumbnail;
		
		this.current_image = index;
		image = new Element('img', {src: this.images[this.current_image].big});
		image = image.wrap('div', {'class': 'image'});
		//thumbnail.up('div').insert(this.spinner);
		//this.spinner.show();
		
		function move() {
			//from = self.thumbnails.positionedOffset()[0];
			//to = -thumbnail.positionedOffset()[0] + 215;
			//self.spinner.hide();
			//move_effect = new Effect.Tween(null, from, to, function(p) {
			//	self.thumbnails.style.left = p + 'px';
			//});
		}
		
		if (current) {
			image.hide();
			current.insert({before: image});
			current.fade({afterFinish:function() {
				current.remove();
			}});
			image.appear();
			if (Prototype.Browser.IE) {
				//move();
				current.fade({afterFinish: function() {
					current.remove();
				}});
				//image.onload = function() {
				//	setTimeout(function() {move();}, 100);
				//	current.fade({afterFinish: function() {
				//		current.remove();
				//	}});
				//}
			} else {
				image.observe('load', function() {
					//setTimeout(function() {move();}, 100);
					current.fade({afterFinish: function() {
						current.remove();
					}});
				});
			}
		} else {
			setTimeout(function() {move();}, 100);
			this.big.insert(image);
		}
		
		this.update(index);
		
		this.lock = true;
		setTimeout(function() {
			self.lock = false;
		}, duration * 1000);
	},
	hide: function() {
		if (this.lock) {
			return;
		}
		
		var self = this;
		this.lock = true;
		this.element.fade({afterFinish: function() {
			self.lock = false;
		}});
	},
	change: function(direction) {
		var new_current = this.current_image + direction;
		this.update(new_current);
		
		if (new_current >= 0 && new_current < this.images.length) {
			this.current_image = new_current;
			this.show(new_current);
		}
	},
	update: function(new_current) {
		this.right_button.show();
		this.left_button.show();
		
		if (new_current > this.images.length - 2) {
			this.right_button.hide();
		}
		
		if (new_current < 1) {
			this.left_button.hide();
		}
	},
	resize: function() {
		var d = $('bgdfbd').getDimensions();
		this.element.style.width = d.width + 'px';
		this.element.style.height = d.height + 'px';
	}
});

function switch_video(img, i) {
	var videos = $$('.video'), images = img.up('div').select('img');
	videos.invoke('hide');
	images.invoke('removeClassName', 'active');
	videos[i].show();
	images[i].addClassName('active');
}

function scrolltop() {
	return (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

function video(id, src, preview, width, height) {
	videos.push({id: id, src: src, preview: preview, width: width, height: height});
}

function init_swfobject(video) {
	var flashvars = {src: 'http://www.urubmx.ru/brezg/' + video.src, preview: 'http://www.urubmx.ru/brezg/' + video.preview};
	var params = {allowfullscreen: "true", wmode: "transparent"};
	swfobject.embedSWF('http://www.justbenicestudio.com/flash/player.swf', video.id, video.width, video.height, "9.0.0", "http://www.justbenicestudio.com/flash/expressInstall.swf", flashvars, params, null);
}
