var Sections = (function(){
	
	var currentPhoto;
	var currentSection;
	var defaultSection;
	var doNothing = function(){}
	
	var showFirstPhoto = function() {
		if(!active && Photos.allLoaded(currentSection)){
			visiblePhoto().onload = null;
			visiblePhoto().src = currentSection.files.first();
		} else {
			setTimeout(showFirstPhoto, 1000);
		}
	};
	
	var animateShowNewSection = function() {
		closeAndOpenToReveal(
			currentSection.files.first(),
			showFirstPhoto,
			function() {});
	}
	
	var loadSection = function(section) {
		currentSection = section;
		Photos.loadSection(
				currentSection, 
				animateShowNewSection,
				doNothing);
		currentPhoto = currentSection.files.first();
	};
	
	var highlightSection = function(section) {
			$$('.sub-nav a:visited').each(function(element) {
				element.setStyle({
					color: '#716B5B'
				});
			});
			
			$(section.divId).childElements().first().setStyle({
				color: 'black'
			});
	};
	
	return {

		addSections : function(inSections, inDefaultSection) {
			Sections.sections = $H(inSections);
			defaultSection = currentSection = Sections.sections.get(inDefaultSection);
			currentPhoto = defaultSection.files.first();
			
			Photos.loadSection(
				defaultSection, 
				function(){},
				function(){tlPhoto.showPhoto(currentPhoto)}
			);
			
			if ($H(inSections).keys().length > 1) {
				highlightSection(defaultSection);
				document.observe("dom:loaded", function(){ highlightSection(defaultSection); });
			}
		} ,
		
		goToSection : function(sectionId) {
			var section = Sections.sections.get(sectionId);
			
			if (currentSection != section) {
				loadSection(section);
				highlightSection(section);
			} 
		} ,
		
		showDefaultSection : function() {
			loadSection(defaultSection);
		} ,
		
		highlightSection : function(sectionId) {
			$('section_name_' + sectionId).setStyle({color: 'black'});
		} ,

		removeHighlight : function(sectionId) {
			$('section_name_' + sectionId).setStyle({color: ''});
		} ,
		
		currentPhoto : function() {
			return currentPhoto;
		} ,
		
		currentSection : function() {
			return currentSection;
		} ,
		
		nextPhoto : function(goingUp) {
			var next = currentSection.files.indexOf(currentPhoto) + (goingUp ? 1 : -1);
			
			if (next < 0) next = currentSection.files.length - 1;
			next = next % currentSection.files.length;
			
			return currentSection.files[next];
		} ,
		
		setNextPhoto : function(goingUp) {
			currentPhoto = Sections.nextPhoto(goingUp);
		} 
		
	}
	
})();