/* gallery.js */

var galleryScript = function() {
	
	var setObservers = function() {
		if ($$('#images>a')) $$('#images>a').each(function(image, count) {
			image.observe('click', function(event) {galleryScript.setBig(event, count)});
			image.select('img')[0].observe('mouseover', function(event) {galleryScript.growThumb(event)});
			image.select('img')[0].observe('mouseout', function(event) {galleryScript.shrinkThumb(event)});
		});
		
		if($('preload')) $('preload').observe('load', function(event) {galleryScript.swapImage(event)});
	}
	
	var init = function() {
	setObservers();
	if (!$('preload')) return false;
	$('preload').writeAttribute('src', large_images[0]);
	var newHeight = parseInt($('images').getStyle('height'));
	newHeight += 25;
	$('images').setStyle({'height' : newHeight+'px'});
	MM_preloadImages('images/n1r.jpg','images/n2r.jpg','images/n3r.jpg','images/n4r.jpg','images/n5r.jpg','images/n6r.jpg','images/n7r.jpg','images/n8r.jpg','images/n9r.jpg','images/photo_back_roll.jpg','images/photo_next_roll.jpg');
	}
	
	var setBig = function(event, num) {
		event.stop();
		$('big_image').setAttribute('src', 'images/loading.gif');
		$('big_image').setStyle('width:32px; height:32px;');
		preload = new Element('img', {id: 'preload'});
		$('big_image_wrap').insert(preload);
		$('preload').observe('load', function(event) {galleryScript.swapImage(event)});
		$('preload').writeAttribute('src', large_images[num]);
	}
	
	var growThumb = function(event) {
		event.stop();
		thumb = $(event.target);
		$$('#images>a>img').each(function(image, count) {
			if (parseInt(image.getStyle('width')) > 70) {
				if (image.curEffect) image.curEffect.cancel();
				image.curEffect = new Effect.Morph(image, {
					style:'width:70px; height:45px', 
					duration: .08,
					afterFinish: function(effect) {effect = false},
					queue: {position:'end', limit:2, scope:'grow'}
				});
			}
		});
		if (thumb.curEffect) thumb.curEffect.cancel();
		thumb.curEffect = new Effect.Morph(thumb, {
			style: 'width:105px; height:67px', 
			duration: .08, 
			queue: {position: 'end', limit:2, scope:'grow'}
		});
	}
		
	var shrinkThumb = function(event) {
		thumb = $(event.target);
		if (thumb.curEffect) thumb.curEffect.cancel();
		thumb.curEffect = new Effect.Morph(thumb, {
			style:'width:70px; height:45px', 
			duration: .08,
			afterFinish: function(effect) {effect = false},
			queue: 'end'
		});
	}
	
	var swapImage = function(event) {
		$('big_image').setStyle('width:auto; height:auto;');
		$('big_image').setAttribute('src', $('preload').readAttribute('src'));
		$('preload').remove();
	}
	
	return {
		init: init,
		setBig: setBig,
		growThumb: growThumb,
		shrinkThumb: shrinkThumb,
		swapImage: swapImage
	}
	
}();

window.onload = galleryScript.init;
