
	$(document).ready(function(){
		galleryImage.init();
	});

	galleryImage = {
		options : {
			'animationSpeed' : 'fast', /* fast/normal/slow */
			'padding' : 40 /* padding for each side of the picture */
		},
		init : function(){
			// Find all the images to overlay
			galleryImage.imagesArray = [];
			$("a[rel^='galleryOverlay']").each(function(){
				galleryImage.imagesArray[galleryImage.imagesArray.length] = this;
				$(this).bind('click',function(){
					galleryImage.open(this); return false;
				});
			});
		},
		open : function(caller) {
			galleryImage.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.
			galleryImage.setCount = 0; /* Total images in the set */
			galleryImage.setPosition = 0; /* Position in the set */
			galleryImage.arrayPosition = 0; /* Total position in the array */
			galleryImage.isSet = false;
			for (i = 0; i < galleryImage.imagesArray.length; i++){
				if($(galleryImage.imagesArray[i]).attr('rel').indexOf(theGallery) != -1){
					galleryImage.setCount++;
					if(galleryImage.setCount > 1) galleryImage.isSet = true;

					if($(galleryImage.imagesArray[i]).attr('href') == $(caller).attr('href')){
						galleryImage.setPosition = galleryImage.setCount;
						galleryImage.arrayPosition = i;
					};
				};
			};
			
			galleryImage.buildOverlay(galleryImage.isSet);

			// Display the current position
			$('div.imageHolder span.currentText').html(' Foto  <span> ' + galleryImage.setPosition   + '</span>' + ' z ' + galleryImage.setCount);

			// Position the picture in the center of the viewing area
			galleryImage.centerPicture();
			
			// Fade out the current picture
			$('div.imageHolder #fullResImage').fadeTo(galleryImage.options['animationSpeed'],0,function(){
				$('.loaderIcon').show();

				// Preload the neighbour images
				galleryImage.preload();
			});
		},
		next : function(){
			// Change the current position
			galleryImage.arrayPosition++;
			galleryImage.setPosition++;

			// Fade out the current picture
			$('div.imageHolder #fullResImage').fadeTo(galleryImage.options['animationSpeed'],0,function(){
				$('.loaderIcon').show();
				
				// Preload the neighbour images
				galleryImage.preload();
			});
			
			$('div.imageHolder .hoverContainer').fadeOut(galleryImage.options['animationSpeed']);
			$('div.imageHolder .details').fadeOut(galleryImage.options['animationSpeed'],function(){
				galleryImage.checkPosition();
			});
		},
		previous: function(){
			// Change the current position
			galleryImage.arrayPosition--;
			galleryImage.setPosition--;

			// Fade out the current picture
			$('div.imageHolder #fullResImage').fadeTo(galleryImage.options['animationSpeed'],0,function(){
				$('.loaderIcon').show();
				
				// Preload the image
				galleryImage.preload();
			});

			$('div.imageHolder .hoverContainer').fadeOut(galleryImage.options['animationSpeed']);
			$('div.imageHolder .details').fadeOut(galleryImage.options['animationSpeed'],function(){
				galleryImage.checkPosition();
			});
		},
		checkPosition : function(){
			// If at the end, hide the next link
			(galleryImage.setPosition == galleryImage.setCount) ? $('div.imageHolder a.next').hide() : $('div.imageHolder a.next').show();
			
			// If at the beginning, hide the previous link
			(galleryImage.setPosition == 1) ? $('div.imageHolder a.previous').hide() : $('div.imageHolder a.previous').show();
			
			// Change the current picture text
			$('div.imageHolder span.currentText span').text(galleryImage.setPosition);
			
			if($(galleryImage.imagesArray[galleryImage.arrayPosition]).attr('title')){
				$('div.imageHolder .description').text($(galleryImage.imagesArray[galleryImage.arrayPosition]).attr('title'));
			}else{
				$('div.imageHolder .description').text('');
			}
		},
		centerPicture : function(){
			var offset = $('div.imageHolder').offset();

			$('div.imageHolder').css({
				'top': offset.top + galleryImage.getScroll() - $('div.imageHolder').height()/2,
				'left': offset.left - $('div.imageHolder').width()/2
			});
		},
		preload : function(){
			// Set the new image
			imgPreloader = new Image();

			$('div.imageHolder .content').css('overflow','hidden');
			$('div.imageHolder #fullResImage').attr('src',$(galleryImage.imagesArray[galleryImage.arrayPosition]).attr('href'));

			imgPreloader.onload = function(){
				// Need that small delay for the anim to be nice
				setTimeout('galleryImage.showimage(imgPreloader.width,imgPreloader.height)',500);
			};
			
			imgPreloader.src = $(galleryImage.imagesArray[galleryImage.arrayPosition]).attr('href');
		},
		showimage : function(width,height){
			// Hide the next/previous links if on first or last images.
			galleryImage.checkPosition();
			
			$('div.imageHolder .details').width(width); /* To have the correct height */
			$('div.imageHolder .details p.description').width(width - parseFloat($('div.imageHolder a.close').css('width'))); /* So it doesn't overlap the button */
			
			// Get the container size, to resize the holder to the right dimensions
			containerHeight = height + parseFloat($('div.imageHolder .details').height()) + parseFloat($('div.imageHolder .top').height()) + parseFloat($('div.imageHolder .bottom').height());
			contentHeight = height + parseFloat($('div.imageHolder .details').height()) + parseFloat($('div.imageHolder .details').css('margin-top')) + parseFloat($('div.imageHolder .details').css('margin-bottom'));
			containerWidth = width + parseFloat($('div.imageHolder .content').css("padding-left")) + parseFloat($('div.imageHolder .content').css("padding-right")) + galleryImage.options['padding'];
			
			$('div.imageHolder .content').animate({'height':contentHeight},galleryImage.options['animationSpeed']);

			// Resize the holder
			$('div.imageHolder').animate({
				'top': galleryImage.getScroll() + (($(window).height()/2) - (containerHeight/2)),
				'left': (($(window).width()/2) - (containerWidth/2)),
				'width':containerWidth,
				'height':containerHeight
			},galleryImage.options['animationSpeed'],function(){
				// Show the nav elements
				galleryImage.shownav();

				$('#fullResImage').show();

				// Fade the new image
				$('div.imageHolder #fullResImage').fadeTo(galleryImage.options['animationSpeed'],1,function(){
					$('div.imageHolder .hoverContainer').height(height);
				});
			});	
		},
		shownav : function(){
			if(galleryImage.isSet) $('div.imageHolder .hoverContainer').fadeIn();
			$('div.imageHolder .details').fadeIn();
			$('.loaderIcon').hide();
		},
		buildOverlay : function(){
			
			// Build the background overlay div
			backgroundDiv = "<div class='galleryImageOverlay'></div>";
 			$('body').append(backgroundDiv);
			$('div.galleryImageOverlay').css('height',$(document).height());
			$('.galleryImageOverlay').bind('click',function(){
				galleryImage.close();
			});
			
			// Basic HTML for the picture holder
			imageHolder = '<div class="imageHolder"><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><img id="fullResImage" src="" style="display:none;" /><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>';
			$('body').append(imageHolder);

			$('.imageHolder').css({'opacity': 0});
			$('a.close').bind('click',function(){ galleryImage.close(); return false; });
			
			$('.imageHolder .previous').bind('click',function(){
				galleryImage.previous();
				return false;
			});
			
			$('.imageHolder .next').bind('click',function(){
				galleryImage.next();
				return false;
			});
			
			// If it's not a set, hide the links
			if(!galleryImage.isSet) {
				$('.hoverContainer').hide();
				$('.currentTextHolder').hide();
			};

			// Then fade it in
			$('div.galleryImageOverlay').css('opacity',0);
			// $('div.galleryImageOverlay').hide();
			$('div.galleryImageOverlay').fadeTo(galleryImage.options['animationSpeed'],0.35, function(){
				$('div.imageHolder').fadeTo(galleryImage.options['animationSpeed'],1,function(){
					// To fix an IE bug
					$('div.imageHolder').attr('style','left:'+$('div.imageHolder').css('left')+';top:'+$('div.imageHolder').css('top')+';');
				});
			});
		},
		getScroll : function(){
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			return scrollTop;
		},
		close : function(){
			$('div.imageHolder').fadeTo(galleryImage.options['animationSpeed'],0, function(){
				$('div.galleryImageOverlay').fadeTo(galleryImage.options['animationSpeed'],0, function(){
					$('div.galleryImageOverlay').remove();
					$('div.imageHolder').remove();
				});
			});
		}
	}
