$(document).ready(function(){
	prettyPhoto.init();
});
	prettyPhoto = {
		options : {
			'animationSpeed' : 'normal', /* fast/normal/slow */
		},
		init : function(){
			// Find all the images to overlay
			prettyPhoto.setCount = 0; /* Total images in the set */
			prettyPhoto.arrayPosition = 0; /* Total position in the array */
			prettyPhoto.imagesArray = new Array();
			
			$("a.inflateImage").each(function(){
				prettyPhoto.setCount++;
				prettyPhoto.imagesArray[prettyPhoto.imagesArray.length] =  "../" + $(this).attr('href');
				$(this).bind('click',function(){
					prettyPhoto.open(this); return false;
				});
			});

			(prettyPhoto.setCount > 1) ? prettyPhoto.isSet = true : prettyPhoto.isSet = false;
			
			$(window).scroll(function(){ prettyPhoto.centerPicture(); });
			$(window).resize(function(){ prettyPhoto.centerPicture(); prettyPhoto.resizeOverlay(); })
		},
		open : function(caller) {
			
			prettyPhoto.caller = caller;
			for (i=0;i<prettyPhoto.imagesArray.length;i++) {
				if (prettyPhoto.imagesArray[i] == "../" + $(prettyPhoto.caller).attr('href')) {
					prettyPhoto.arrayPosition = i;
					break;
				}		
			}
			prettyPhoto.buildOverlay(prettyPhoto.isSet);
	
			prettyPhoto.centerPicture();
				
			$('div.pictureHolder #fullResImage').hide();
			
			$('div.pictureHolder span.title').text($(prettyPhoto.caller).attr('title'));
			$('div.pictureHolder span.currentText').text(prettyPhoto.setCount + 1);
			$('div.pictureHolder span.totalText').text(prettyPhoto.imagesArray.length);
			// Preload the neighbour images
			prettyPhoto.preload();
		},
		next : function(){
			if (prettyPhoto.arrayPosition + 1 < prettyPhoto.setCount) {
				// Change the current position
				prettyPhoto.arrayPosition++;

				// Fade out the current picture
				$('div.pictureHolder #fullResImage').fadeOut(prettyPhoto.options['animationSpeed'],function(){
					prettyPhoto.preload();
				});
			}
			prettyPhoto.checkPosition();
		},
		previous: function(){
			if (prettyPhoto.arrayPosition > 0) {
				// Change the current position
				prettyPhoto.arrayPosition--;

				// Fade out the current picture
				$('div.pictureHolder #fullResImage').fadeOut(prettyPhoto.options['animationSpeed'],function(){
					prettyPhoto.preload();
				});

				prettyPhoto.checkPosition();
			}
		},
		checkPosition : function(){
			(prettyPhoto.arrayPosition + 1 == prettyPhoto.setCount) ? $('a.next').addClass('noNext') : $('a.next').removeClass('noNext');
			(prettyPhoto.arrayPosition == 0) ? $('a.previous').addClass('noPrev') : $('a.previous').removeClass('noPrev');
			$('div.pictureHolder span.currentText').text(prettyPhoto.arrayPosition + 1);
		},
		centerPicture : function(){
			//Make sure the gallery is open
			if($('div.pictureHolder').size() > 0){
				
				var scrollPos = prettyPhoto.getScroll();
				
				$('div.pictureHolder').css({
					'top': ($(window).height()/2) + scrollPos['scrollTop'] - ($('div.pictureHolder').height()/2),
					'left': ($(window).width()/2) + scrollPos['scrollLeft'] - ($('div.pictureHolder').width()/2)
				});
			};
		},
		preload : function(){
			// Hide the next/previous links if on first or last images.
			prettyPhoto.checkPosition();
			
			// Set the new image
			imgPreloader = new Image();
			
			$('div.pictureHolder #fullResImage').attr('src', "/" + prettyPhoto.imagesArray[prettyPhoto.arrayPosition]);

			imgPreloader.onload = function(){
				var correctSizes = prettyPhoto.resize(imgPreloader.width,imgPreloader.height);
				imgPreloader.width = correctSizes['width'];
				imgPreloader.height = correctSizes['height'];
				
				// Need that small delay for the anim to be nice
				setTimeout('prettyPhoto.showimage(imgPreloader.width,imgPreloader.height,'+correctSizes["containerWidth"]+','+correctSizes["containerHeight"]+','+correctSizes["contentWidth"]+')',800);
			};
			
			
			imgPreloader.src = "/" + prettyPhoto.imagesArray[prettyPhoto.arrayPosition];
		},
		showimage : function(width,height,containerWidth,containerHeight,contentWidth){
			
			var scrollPos = prettyPhoto.getScroll();
			var newTop = ($(window).height()/2) + scrollPos['scrollTop'] - (containerHeight/2);
			var newLeft = ($(window).width()/2) + scrollPos['scrollLeft'] - (containerWidth/2);
			$('div.pictureHolder').animate({'top':newTop,'left':newLeft},prettyPhoto.options['animationSpeed']);
			
			$('#fullResImageContainer').animate({'width':width,'height':height},prettyPhoto.options['animationSpeed'],function(){
				$('div.pictureHolder #fullResImage').fadeIn(prettyPhoto.options['animationSpeed']);
			});
			
		
		},
		
		buildOverlay : function(){
			
			// Build the background overlay div
			backgroundDiv = "<div class='prettyPhotoOverlay'></div>";
 			$('body').append(backgroundDiv);
			$('div.prettyPhotoOverlay').css('height',$(document).height());
			$('.prettyPhotoOverlay').bind('click',function(){
				prettyPhoto.close();
			});
			
			// Basic HTML for the picture holder
			pictureHolder = '<div class="pictureHolder"><a class="previous" href="#" id="prevLink"><img src="/images/interface/portfolioPrevImage.gif"/></a><div id="photoContent"><div id="fullResImageContainer"><img id="fullResImage" src="" /></div><p class="description"><span class=title></span><br/><span>Image </span><span class="currentText"></span> / <span class="totalText"></span></p><a class="closer"><img src="/images/interface/portfolioCloser.gif" alt="X"></a></div><a class="next" href="#" id="nextLink"><img src="/images/interface/portfolioNextImage.gif"/></a></div>';
			
			$('body').append(pictureHolder);

			$('.pictureHolder').css({'opacity': 0});
			
			$('a.previous').bind('click',function(){
				prettyPhoto.previous();
				$('a.previous').blur();
				return false;
			});
			
			$('a.next').bind('click',function(){
				prettyPhoto.next();
				$('a.next').blur();
				return false;
			});
			
			$('.closer').bind('click',function(){
				prettyPhoto.close();
				return false;
			});

			// Then fade it in
			$('div.prettyPhotoOverlay').css('opacity',0);
			$('div.prettyPhotoOverlay').fadeTo(prettyPhoto.options['animationSpeed'],0.80, function(){
				$('div.pictureHolder').fadeTo(prettyPhoto.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){			
			// Get the container size, to resize the holder to the right dimensions
			contentHeight = 30;
			containerHeight = contentHeight + 40;
			containerWidth = 196;
			
			var newWidth = width;
			var newHeight = height;
			
			if((containerWidth + width) > $(window).width() || (containerHeight + height) > $(window).height()) {
				// Get the original geometry and calculate scales
				var xscale=(width+containerWidth + 100)/$(window).width();
				var yscale=(height+containerHeight + 100)/$(window).height();
			
				// 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;

			return {
				width:newWidth,
				height:newHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight
			};
		},
		resizeOverlay : function() {
			$('div.prettyPhotoOverlay').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').fadeTo(prettyPhoto.options['animationSpeed'],0, function(){
				$('div.prettyPhotoOverlay').fadeTo(prettyPhoto.options['animationSpeed'],0, function(){
					$('div.prettyPhotoOverlay').remove();
					$('div.pictureHolder').remove();
				});
			});
		}
	}