var agt				= navigator.userAgent.toLowerCase();
var is_ie    	= ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_major 	= parseInt(navigator.appVersion, 10);
var is_ie6 		= (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_safari = agt.indexOf('safari') != -1;
var active = false;

var tlPhoto = (function(){
	
	var galleryWidth;
	var panels = ['gallery', 'contact', 'about', 'client-list', 'rates'];

	var transitionDone = function(goingUp){
		Sections.setNextPhoto(goingUp);
		tlPhoto.showNavigation();
		active = false;
	};
	
	var transitionSameSize = function(goingUp) {
		active = true;
		
		if (goingUp) new Effect.WipeRight(Sections.nextPhoto(goingUp), function(){transitionDone(goingUp)});	
		else 				 new Effect.WipeLeft(Sections.nextPhoto(goingUp),  function(){transitionDone(goingUp)});	
	};
	
	var transitionDifferentSize = function(goingUp){
		closeAndOpenToReveal(
				Sections.nextPhoto(goingUp), 
				function(){},
				function(){transitionDone(goingUp)}
		);
	}
	
	var hideNavigation = function() {
		$('navigation').hide();
	};
	
	var activePanel = function() {
		return panels.find(function(panel) {
			return $(panel).visible();
		});
	};
	
	var showMenu = function(menu) {
		panels.each(function(panel) {
			$(panel + '-menu').hide();
		});
		$(menu + '-menu').show();
	};
	
	// this fixes firefox not loading undisplayed images
	document.observe('dom:loaded', function() {
		
		['/images/about-background.jpg' , 
			'/images/staff-background.jpg' , 
			'/images/client_list-background.jpg' , 
			'/images/rates-background.jpg' , 
			'/images/contact-background.jpg'
		].each(function(filename) {
			var image = new Image();
			image.src = filename;
		});
		
	});
	
	return { 
		
		showPhoto : function(fileName) {
			visiblePhoto().src = fileName;
			tlPhoto.showNavigation();
		} ,
		
		showNavigation : function() {
			$('navigation').show();
		} ,
		
		hideNavigation : function() {
			$('navigation').hide();
		} ,
				
		transition : function(goingUp) {
			if(active) return;
						
			hideNavigation();
			
			if (Photos.areSameSize(Sections.currentPhoto(), Sections.nextPhoto(goingUp))) {
				transitionSameSize(goingUp);
			} else {
				transitionDifferentSize(goingUp);
			}
		} ,
		
		display : function(frame) {
			frame = $(frame);
			var goingOut = activePanel();

			if (active || frame.id == goingOut) return;
			
			active = true;

			if(goingOut == 'gallery') galleryWidth = $('gallery').getWidth();
			
			$$('.scroll').each(function(el) {
				el.setStyle({overflow: 'hidden'});
			});
			
			$('gallery').setStyle({
				overflow: 'hidden'
			});
			
			var options = Object.extend(wipeOptions(), {
				scaleFrom: 100, 
				sync: false,
				afterFinish: function() {
					$(goingOut).hide();
					showMenu(frame.id);

					new Effect.Scale(frame, 100, 
						Object.extend(options, {
							scaleFrom: 0, 
							afterFinish: function() {
								active = false;
								$$('.scroll').each(function(el) {
									el.setStyle({overflow: 'auto'});
								});
								$('gallery').setStyle({
									overflow: 'visible'
								});
							} ,
							scaleMode: {
								originalHeight: 432, 
								originalWidth: (frame.id == 'gallery') ? galleryWidth - 14 : 290
							}
						})
					);
				}
			});
			new Effect.Scale(goingOut, 0, options);
		} 
	};
	
})();



