jQuery(document).ready(function() {
jQuery('body').prepend('<div class="modalBackgrounder" style="display: none;height: 100%;left: 0;margin: 0;padding: 0;position: absolute;top: 0;width: 100%;z-index: 99999;"></div>');
jQuery('#casestudy_header_content a').click(function() {
				jQuery('.modalBackgrounder').fadeIn('fast', function() {
				jQuery('.modalBackgrounder').css('background','url("/images/caseStudyTitleBackground.png") repeat scroll center center transparent');
				jQuery('.modalBackgrounder').append('<p id="modalClose" style="position:fixed; cursor:pointer; width:65%">close</p>');
				jQuery('.modalBackgrounder').append(jQuery('#gallery_modal').html());
				jQuery('.modalBackgrounder .gallery').css({
					'height': '500px',
					'list-style-type': 'none',
					'margin': '0',
					'overflow': 'hidden',
					'padding': '0',
					'position': 'fixed',
					'text-align': 'center',
					'width': '100%',
					'top' : (jQuery('.modalBackgrounder').height() / 2) - (jQuery('.gallery').height() / 2) + 'px',
				});
				jQuery('#modalClose').css({'top': jQuery('.modalBackgrounder .gallery').position().top - 20 + 'px','text-align':'right'});
				jQuery('.modalBackgrounder .gallery li').hide();
				jQuery('.modalBackgrounder .gallery li').first().show();

				var imgNavLinks = '';

				jQuery('.modalBackgrounder .gallery li').each(function() {
					imgNavLinks += '<span style="cursor: pointer;">' + (jQuery(this).index() + 1) + '</span> ';
					
				});
				
				//imgNavLinks += '<span id="modal_close" style="cursor: pointer; float:right;">close</span> ';

				jQuery('.modalBackgrounder .gallery').after('<p class="timelineImagesNav" style="position: fixed; width: 100%; text-align: center;">' + imgNavLinks + '</p>');
				jQuery('.timelineImagesNav').css({
					'top' : (jQuery('.modalBackgrounder .gallery').position().top + jQuery('.modalBackgrounder .gallery').height() + 10) + 'px'
				});

				jQuery(document).keydown(function(e) {
					if (e.which == 27) { // Escape key
						jQuery('.modalBackgrounder').fadeOut('fast', function() {
							jQuery('.modalBackgrounder').html('');
						});
					}
				});

				jQuery('.timelineImagesNav span').click(function() {
					var elemLink = jQuery(this);

					jQuery('.modalBackgrounder .gallery li:visible').fadeOut('fast', function() {
						jQuery('.modalBackgrounder .gallery li:eq(' + elemLink.index() + ')').fadeIn('fast');
					});
				});

				jQuery('#modalClose').click(function() {
					jQuery('.modalBackgrounder').fadeOut('fast', function() {
						jQuery('.modalBackgrounder').html('');
					});
				});


				jQuery('.modalBackgrounder img').click(function() {
					jQuery('.modalBackgrounder').fadeOut('fast', function() {
						jQuery('.modalBackgrounder').html('');
					});
				});
			});
		});
});

