window.addEvent('domready', function() {

	var new_to = $('new_to_cghm');

	if(new_to != null) {
	
		new_to.setStyle('opacity', 0);
		
		fade = new Fx.Tween(new_to, {duration: 2000, wait: false});
		
		fade.start.delay(1000, fade, ['opacity', 0, 1]);
		
	}

	var overlay = $('overlay');

	overlay.setStyle('display', 'none');

	photos = $$('.light');
	
	photos.each(addLightbox);
	
	function addLightbox(button) {
	
		button.addEvent('click', function(e) {
		
			e.stop();
		
			photo = button.getProperty('href');
			
			showPhoto(photo);
		
			
		});
	
	}
	
	function showPhoto(photo) {
	
		overlay.setStyle('opacity', 0);
		
		overlay.setStyle('display', '');
		
		overlay.setStyle('margin-top', window.getScroll().y);
			
		overlay.setStyle('height', window.getSize().y);
		
		overlay.setStyle('width', window.getSize().x);
		
		window.addEvent('scroll', function() {
		
			overlay.setStyle('margin-top', window.getScroll().y);
			
		});
		
		new Fx.Tween(overlay, {duration: 500, wait: false}).start('opacity', .6);
	
		var lightbox = new Element('div', {'class': 'lightbox'}).inject($('lightbox_container'));
	
		lightbox.setStyle('width', window.getSize().x - 10);
		
		var photo_box = new Element('div', {id: 'photo_box'}).inject(lightbox);
		
		var close_button = new Element('img', {src: 'close_button.gif', 'class': 'close_button'}).inject(photo_box);
				
		var the_photo = new Element('img', {'src': photo}).inject(photo_box);
		
		new Fx.Tween(photo_box, {duration: 500, wait: false}).start('opacity', 1);
		
		close_button.addEvent('click', function() {
			
			lightbox.dispose();
			
			new Fx.Tween(overlay, {duration: 500, wait: false}).start('opacity', 0).chain(function() {
			
				overlay.setStyle('display', 'none');
				
			});
		
		});
		
		lightbox.setStyle('margin-top', window.getScroll().y + 20);
		
		the_photo.setStyle('visibility', 'hidden');
		
		photo_box.setStyle('background','#FFF');
		
		photo_loader = new Element('img', {'src':'aimages/loading_lightbox.gif'}).inject(photo_box);
		
		photo_loader.setStyle('position', 'absolute');
		
		photo_loader.setStyle('margin-left', -580);
		
		the_photo.addEvent('load', function() {
			
			photo_loader.setStyle('display', 'none');
			
			the_photo.setStyle('visibility', '');
			
			target_width = the_photo.getSize().x;
		
			target_height = the_photo.getSize().y;
			
			the_photo.setStyle('visibility', 'hidden');
			
			//photo_box.setStyle('margin-top', (target_height / 2));
			
			new Fx.Morph(photo_box, {duration: 1000, transition: Fx.Transitions.Quart.easeOut}).start({'width': target_width, 'height': target_height}).chain(function() {
		
				the_photo.setStyle('visibility', '');
				
			});
			
		});
		
	}

});
