var application = {
	initImageFading: function(selector, options) {
		var images = $$(selector),
			options = options || {};
		
		if(options.excludeSelector) {
			var exclude_images = $$(options.excludeSelector);
			
			images = images.reject(function(image){
				return exclude_images.include(image);
			});
		}
	
		images.each(function(image) {
			var overEffect, outEffect;
			
			image.setOpacity(options.from);
			
			image.observe('mouseover', function(event) {
				overEffect = new Effect.Appear(image, { 
					from: options.from, 
					duration: options.fadeDuration, 
					beforeStart: function(){ 
						if (outEffect) outEffect.cancel(); 
					}
				});
			});
			
			image.observe('mouseout', function(event) {
				outEffect = new Effect.Fade(image, { 
					to: options.from, 
					duration: options.resetDuration, 
					beforeStart: function(){ 
						if (overEffect) overEffect.cancel();
					} 
				});
			});
		});
	},
	
	initSlideshow: function(selector, options) {
		var slideshows = $$(selector);
		
		slideshows.each(function(slideshow){
			var image = slideshow.down('.image'),
				slideLinks = slideshow.select('.slides .image a');
			
			slideLinks.each(function(slideLink) {
				var preLoad = new Element('img', { src: slideLink.readAttribute('href') }).hide();
				slideshow.insert({ bottom: preLoad });
							
				slideLink.observe('click', function(event) {
					event.stop();
					
					new Effect.Opacity(image, {
						to: 0, 
						duration: 0.25,
						afterFinish: function(){
							image.down('img').replace(preLoad.show());
							
							new Effect.Opacity(image, {
								to: 1,
								duration: 0.5
							});
						}
					});
						
				});
			});
		});
	},
	
	initMinHeight: function(selector, minHeight, options) {
		var elements = $$(selector);
		
		elements.each(function(element){
			if (element.getHeight() < minHeight) {
				element.setStyle({ height: minHeight + 'px' });
			}
		});
	}
}

document.observe('dom:loaded', function() {
	application.initImageFading('.gallery .image img', { 
		from: 0.85, 
		fadeDuration: 0.25, 
		resetDuration: 0.10 
	});
	
	application.initImageFading('.slideshow .slides .image img', { 
		from: 0.85, 
		fadeDuration: 0.25, 
		resetDuration: 0.10 
	});
	
	application.initSlideshow('.slideshow');
});