	$(document).ready(function(){
		enlarge.init();
	});

	enlarge = {
		options : {
			'animationSpeed' : 'fast', /* fast/normal/slow */
			'padding' : 40, /* padding for each side of the picture */
			'opacity' : 0.35, /* Value betwee 0 and 1 */
			'showTitle' : true
		},
		init : function(){
			// Find all the images to overlay
			enlarge.imagesArray = [];
			$("a[rel^='prettyOverlay'],a[rel^='enlarge']").each(function(){
				enlarge.imagesArray[enlarge.imagesArray.length] = this;
				$(this).bind('click',function(){
					enlarge.open(this); return false;
				});
			});
			
			$(window).scroll(function(){ enlarge.centerPicture(); });
			$(window).resize(function(){ enlarge.centerPicture(); enlarge.resizeOverlay(); })
		},
		open : function(caller) {
			enlarge.caller = caller;
			
			// Find out if the picture is part of a set
			theRel = $(caller).attr('rel');
			galleryRegExp = /\[(?:.*)\]/;
			theGallery = galleryRegExp.exec(theRel);
			
			// Calculate the number of items in the set, and the position of the clicked picture.
			enlarge.setCount = 0; /* Total images in the set */
			enlarge.setPosition = 0; /* Position in the set */
			enlarge.arrayPosition = 0; /* Total position in the array */
			enlarge.isSet = false;
			for (i = 0; i < enlarge.imagesArray.length; i++){
				if($(enlarge.imagesArray[i]).attr('rel').indexOf(theGallery) != -1){
					enlarge.setCount++;
					if(enlarge.setCount > 1) enlarge.isSet = true;

					if($(enlarge.imagesArray[i]).attr('href') == $(caller).attr('href')){
						enlarge.setPosition = enlarge.setCount;
						enlarge.arrayPosition = i;
					};
				};
			};
			
			enlarge.buildOverlay(enlarge.isSet);

			// Display the current position
			$('div.pictureHolder span.currentText').html('<span>' + enlarge.setPosition + '</span>' + '/' + enlarge.setCount);

			// Position the picture in the center of the viewing area
			enlarge.centerPicture();
			
			$('div.pictureHolder #fullResImageContainer').hide();
			$('.loaderIcon').show();

			// Preload the neighbour images
			enlarge.preload();
		},
		next : function(){
			// Change the current position
			enlarge.arrayPosition++;
			enlarge.setPosition++;

			// Fade out the current picture
			$('div.pictureHolder #fullResImageContainer').fadeOut(enlarge.options['animationSpeed'],function(){
				$('.loaderIcon').show();
				
				// Preload the neighbour images
				enlarge.preload();
			});
			
			enlarge.hideTitle();
			$('div.pictureHolder .hoverContainer').fadeOut(enlarge.options['animationSpeed']);
			$('div.pictureHolder .details').fadeOut(enlarge.options['animationSpeed'],function(){
				enlarge.checkPosition();
			});
		},
		previous: function(){
			// Change the current position
			enlarge.arrayPosition--;
			enlarge.setPosition--;

			// Fade out the current picture
			$('div.pictureHolder #fullResImageContainer').fadeOut(enlarge.options['animationSpeed'],function(){
				$('.loaderIcon').show();
				
				// Preload the image
				enlarge.preload();
			});

			enlarge.hideTitle();
			$('div.pictureHolder .hoverContainer').fadeOut(enlarge.options['animationSpeed']);
			$('div.pictureHolder .details').fadeOut(enlarge.options['animationSpeed'],function(){
				enlarge.checkPosition();
			});
		},
		checkPosition : function(){
			// If at the end, hide the next link
			(enlarge.setPosition == enlarge.setCount) ? $('div.pictureHolder a.next').hide() : $('div.pictureHolder a.next').show();
			
			// If at the beginning, hide the previous link
			(enlarge.setPosition == 1) ? $('div.pictureHolder a.previous').hide() : $('div.pictureHolder a.previous').show();
			
			// Change the current picture text
			$('div.pictureHolder span.currentText span').text(enlarge.setPosition);
			
			if (enlarge.isSet) {
				if($(enlarge.imagesArray[enlarge.arrayPosition]).attr('title')){
					$('div.pictureHolder .description').html(unescape($(enlarge.imagesArray[enlarge.arrayPosition]).attr('title')));
				}else{
					$('div.pictureHolder .description').text('');
				};
				
				if($(enlarge.imagesArray[enlarge.arrayPosition]).find('img').attr('alt') && enlarge.options['showTitle']){
					enlarge.hasTitle = true;
					$('div.enlargeTitle .enlargeTitleContent').html(unescape($(enlarge.imagesArray[enlarge.arrayPosition]).find('img').attr('alt')));
				}else{
					enlarge.hasTitle = false;
				};
			}else{
				if($(enlarge.imagesArray[enlarge.arrayPosition]).attr('title')){
					$('div.pictureHolder .description').html(unescape($(enlarge.caller).attr('title')));
				}else{
					$('div.pictureHolder .description').text('');
				};
				
				if($(enlarge.imagesArray[enlarge.arrayPosition]).find('img').attr('alt') && enlarge.options['showTitle']){
					enlarge.hasTitle = true;
					$('div.enlargeTitle .enlargeTitleContent').html(unescape($(enlarge.caller).find('img').attr('alt')));
				}else{
					enlarge.hasTitle = false;
				};
			};
		},
		centerPicture : function(){
			//Make sure the gallery is open
			if($('div.pictureHolder').size() > 0){
				
				var scrollPos = enlarge.getScroll();
				
				if($.browser.opera) {
					windowHeight = window.innerHeight;
					windowWidth = window.innerWidth;
				}else{
					windowHeight = $(window).height();
					windowWidth = $(window).width();
				};
				
				$('div.pictureHolder').css({
					'top': (windowHeight/2) + scrollPos['scrollTop'] - ($('div.pictureHolder').height()/2),
					'left': (windowWidth/2) + scrollPos['scrollLeft'] - ($('div.pictureHolder').width()/2)
				});
				
				$('div.enlargeTitle').css({
					'top' : $('div.pictureHolder').offset().top - 22,
					'left' : $('div.pictureHolder').offset().left + (enlarge.options['padding']/2)
				});
			};
		},
		preload : function(){
			// Hide the next/previous links if on first or last images.
			enlarge.checkPosition();
			
			// Set the new image
			imgPreloader = new Image();
			
			// Preload the neighbour images
			nextImage = new Image();
			if(enlarge.isSet) nextImage.src = $(enlarge.imagesArray[enlarge.arrayPosition + 1]).attr('href');
			
			prevImage = new Image();
			if(enlarge.isSet && enlarge.imagesArray[enlarge.arrayPosition - 1]) prevImage.src = $(enlarge.imagesArray[enlarge.arrayPosition - 1]).attr('href');

			$('div.pictureHolder .content').css('overflow','hidden');
			
			(enlarge.isSet) ? $('div.pictureHolder #fullResImage').attr('src',$(enlarge.imagesArray[enlarge.arrayPosition]).attr('href')) : $('div.pictureHolder #fullResImage').attr('src',$(enlarge.caller).attr('href'));

			imgPreloader.onload = function(){
				var correctSizes = enlarge.resize(imgPreloader.width,imgPreloader.height);
				imgPreloader.width = correctSizes['width'];
				imgPreloader.height = correctSizes['height'];
				
				// Need that small delay for the anim to be nice
				setTimeout('enlarge.showimage(imgPreloader.width,imgPreloader.height,'+correctSizes["containerWidth"]+','+correctSizes["containerHeight"]+','+correctSizes["contentWidth"]+')',500);
			};
			
			(enlarge.isSet) ? imgPreloader.src = $(enlarge.imagesArray[enlarge.arrayPosition]).attr('href') : imgPreloader.src = $(enlarge.caller).attr('href');
		},
		showimage : function(width,height,containerWidth,containerHeight,contentWidth){
			$('.loaderIcon').hide();
			
			$('div.pictureHolder .content').animate({'height':contentHeight},enlarge.options['animationSpeed']);

			var scrollPos = enlarge.getScroll();

			if($.browser.opera) {
				windowHeight = window.innerHeight;
				windowWidth = window.innerWidth;
			}else{
				windowHeight = $(window).height();
				windowWidth = $(window).width();
			};

			// Resize the holder
			$('div.pictureHolder').animate({
				'top': scrollPos['scrollTop'] + ((windowHeight/2) - (containerHeight/2)),
				'left': ((windowWidth/2) - (containerWidth/2)),
				'width':containerWidth
			},enlarge.options['animationSpeed'],function(){
				$('#fullResImage').attr({
					'width':width,
					'height':height
				});

				// Show the nav elements
				enlarge.shownav();

				$('div.pictureHolder .hoverContainer').height(height).width(width);

				// Fade the new image
				$('div.pictureHolder #fullResImageContainer').fadeIn(enlarge.options['animationSpeed']);
			});	
		},
		shownav : function(){
			if(enlarge.isSet) $('div.pictureHolder .hoverContainer').fadeIn(enlarge.options['animationSpeed']);
			$('div.pictureHolder .details').fadeIn(enlarge.options['animationSpeed']);

			enlarge.showTitle();
		},
		showTitle : function(){
			if(enlarge.options['showTitle'] && enlarge.hasTitle){
				$('div.enlargeTitle').css({
					'top' : $('div.pictureHolder').offset().top,
					'left' : $('div.pictureHolder').offset().left + (enlarge.options['padding']/2),
					'display' : 'block'
				});
				
				$('div.enlargeTitle div.enlargeTitleContent').css('width','auto');
				
				if($('div.enlargeTitle').width() > $('div.pictureHolder').width()){
					$('div.enlargeTitle div.enlargeTitleContent').css('width',$('div.pictureHolder').width() - (enlarge.options['padding'] * 2));
				}else{
					$('div.enlargeTitle div.enlargeTitleContent').css('width','');
				};
				
				$('div.enlargeTitle').animate({'top':($('div.pictureHolder').offset().top - 22)},enlarge.options['animationSpeed']);
			};
		},
		hideTitle : function() {
			$('div.enlargeTitle').animate({'top':($('div.pictureHolder').offset().top)},enlarge.options['animationSpeed'],function() { $(this).css('display','none'); });
		},
		buildOverlay : function(){
			
			// Build the background overlay div
			backgroundDiv = "<div class='enlargeOverlay'></div>";
 			$('body').append(backgroundDiv);
			$('div.enlargeOverlay').css('height',$(document).height());
			$('.enlargeOverlay').bind('click',function(){
				enlarge.close();
			});
			
			// Basic HTML for the picture holder
			pictureHolder = '<div class="pictureHolder"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content"><div class="loaderIcon"></div><div class="hoverContainer"><a class="next" href="#">next</a><a class="previous" href="#">previous</a></div><div id="fullResImageContainer"><img id="fullResImage" src="" /></div><div class="details clearfix"><a class="close" href="#">Close</a><p class="description"></p><p class="currentTextHolder"><span class="currentText"><span>0</span>/<span class="total">0</span></span></p></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
			
			// Basic html for the title holder
			
			// Title text indication
			// titleHolder = '<div class="enlargeTitle"><div class="enlargeTitleLeft"></div><div class="enlargeTitleContent"></div><div class="enlargeTitleRight"></div></div>';

			// Title text fault indication
			titleHolder = '';

			$('body').append(pictureHolder).append(titleHolder);

			$('.pictureHolder,.titleHolder').css({'opacity': 0});
			$('a.close').bind('click',function(){ enlarge.close(); return false; });
			
			$('.pictureHolder .previous').bind('click',function(){
				enlarge.previous();
				return false;
			});
			
			$('.pictureHolder .next').bind('click',function(){
				enlarge.next();
				return false;
			});

			$('.hoverContainer').css({
				'margin-left':enlarge.options['padding']/2,
				'margin-right':enlarge.options['padding']/2
			});
			
			// If it's not a set, hide the links
			if(!enlarge.isSet) {
				$('.hoverContainer').hide();
				$('.currentTextHolder').hide();
			};


			// To fix the bug with IE select boxes
			if($.browser.msie && $.browser.version == 6){
				$('select').css('visibility','hidden');
			};

			// Then fade it in
			$('div.enlargeOverlay').css('opacity',0);
			$('div.enlargeOverlay').fadeTo(enlarge.options['animationSpeed'],enlarge.options['opacity'], function(){
				$('div.pictureHolder').fadeTo(enlarge.options['animationSpeed'],1,function(){
					// To fix an IE bug
					$('div.pictureHolder').attr('style','left:'+$('div.pictureHolder').css('left')+';top:'+$('div.pictureHolder').css('top')+';');
				});
			});
		},
		resize : function(width,height){
			$('div.pictureHolder .details').width(width); /* To have the correct height */
			$('div.pictureHolder .details p.description').width(width - parseFloat($('div.pictureHolder a.close').css('width'))); /* So it doesn't overlap the button */
			
			// Get the container size, to resize the holder to the right dimensions
			contentHeight = parseFloat($('div.pictureHolder .details').height()) + parseFloat($('div.pictureHolder .details').css('margin-top')) + parseFloat($('div.pictureHolder .details').css('margin-bottom'));
			containerHeight = contentHeight + parseFloat($('div.pictureHolder .top').height()) + parseFloat($('div.pictureHolder .bottom').height());
			containerWidth = parseFloat($('div.pictureHolder .content').css("padding-left")) + parseFloat($('div.pictureHolder .content').css("padding-right")) + enlarge.options['padding'];
			
			var newWidth = width;
			var newHeight = height;
			
			// If there's a title, take it into consideration unpon resizing
			if(enlarge.options['showTitle']) containerHeight -= 22;

			if($.browser.opera) {
				windowHeight = window.innerHeight;
				windowWidth = window.innerWidth;
			}else{
				windowHeight = $(window).height();
				windowWidth = $(window).width();
			};
			
			if((containerWidth + width) > windowWidth || (containerHeight + height) > windowHeight) {
				// Get the original geometry and calculate scales
				var xscale=(width+containerWidth + 100)/windowWidth;
				var yscale=(height+containerHeight + 100)/windowHeight;
			
				// Recalculate new size with default ratio
				if (yscale>xscale){
					newWidth = Math.round(width * (1/yscale));
					newHeight = Math.round(height * (1/yscale));
				} else {
					newWidth = Math.round(width * (1/xscale));
					newHeight = Math.round(height * (1/xscale));
				};
			};

			// Get the container size, to resize the holder to the right dimensions
			containerHeight += newHeight;
			contentHeight += newHeight;
			containerWidth += newWidth;
		
			$('div.pictureHolder .details').width(newWidth); /* To have the correct height */
			$('div.pictureHolder .details p.description').width(newWidth - parseFloat($('div.pictureHolder a.close').css('width'))); /* So it doesn't overlap the button */

			return {
				width:newWidth,
				height:newHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight
			};
		},
		resizeOverlay : function() {
			$('div.enlargeOverlay').css({
				'height':$(document).height(),
				'width':$(window).width()
			});
		},
		getScroll : function(){
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		},
		close : function(){
			$('div.pictureHolder,div.enlargeTitle').fadeTo(enlarge.options['animationSpeed'],0, function(){
				$('div.enlargeOverlay').fadeTo(enlarge.options['animationSpeed'],0, function(){
					$('div.enlargeOverlay').remove();
					$('div.pictureHolder').remove();
					
					// To fix the bug with IE select boxes
					if($.browser.msie && $.browser.version == 6){
						$('select').css('visibility','visible');
					};
				});
			});
		}
	}