var Photos = (function(){
	var currentSection;

	var photos =$H({});
	
	var unloadedFilesFor = function(section) {
		return section['files'].findAll(function(filename) {
			return photos.keys().indexOf(filename) == -1; 
		});
	};

	var whenDone;
	
	var imageLoaded = function(image) {
		var element = image;
		var key = element.getAttribute('rawSrc');

		photos.set(key, element);
		if(Photos.allLoaded(currentSection) && whenDone) {
			whenDone();
			whenDone = null;
		}
	};

	return {
				
		loadSection : function(section, beforeEvent, afterEvent) {
			currentSection = section;
			beforeEvent();
			whenDone = afterEvent;
			
			if (!this.allLoaded(section)) {
				section.files.each(function(filename){
					var image = new Image();
					
					Event.observe(image, 'load', function(){ imageLoaded(image); });
					image.writeAttribute({rawSrc: filename});
					image.src = filename;
				});
			}
		} ,
		
		allLoaded : function(section) {
			return (section['files'].all(function(filename) {
				return photos.keys().indexOf(filename) != -1;
			}));
		} ,
		
		areSameSize : function(inFirstPhoto, inSecondPhoto) {
			var firstPhoto = photos.get(inFirstPhoto);
			var secondPhoto = photos.get(inSecondPhoto);
			
			return ((firstPhoto.height == secondPhoto.height) && (firstPhoto.width == secondPhoto.width))
		} ,
		
		getWidth : function(photo) {
			return photos.get(photo).width;
		}
		
	};
	
}());
