if (window.Website == null || window.Website == undefined) {
	Website = { };	
}

Website.Gallery = { };
Website.Gallery.Thumbs = { };
Website.Gallery.Lightbox = { };

Website.Gallery.Cache = new Array();

Website.Gallery.Blank = null;
Website.Gallery.ThumbOverlay = null;

Website.Gallery.Init = function(gallery) {
	var headerLinks = gallery.getElements(".panelMenu A");	
	var location = window.location.pathname.toLowerCase();
	
	var hasActive = false;
	
	// Loop over and initialise the panel header links.
	for (var i = 0; i < headerLinks.length; i++) {
		var link = headerLinks[i];
		var className = link.getProperty("class");
		
		if (location.indexOf(className.toLowerCase()) > -1) {
			link.addClass(className + "Active");
			var hasActive = true;
			
		} else if (i > 0) {
			Website.Animations.Fade(link, 0.1, 1);
		}
	}
	
	if (!hasActive && headerLinks.length > 0) {
		headerLinks[0].addClass(headerLinks[0].getProperty("class") + "Active");
	} else {
		Website.Animations.Fade(headerLinks[0], 0.1, 1);	
	}
	
	var links = gallery.getElements(".thumb");
	var gallery = $("gallery");
	
	Website.Gallery.Blank = Website.URLRoot + "assets/images/template/_.gif";
	Website.Gallery.ThumbOverlay = Website.URLRoot + "assets/images/template/gallery-thumb-hover.png";
	
	// Load blank and thumb overlay images.
	(new Image()).src = Website.Gallery.Blank;
	(new Image()).src = Website.Gallery.ThumbOverlay;
	
	// Loop over the thumbnails and initiate them.
	for (var i = 0; i < links.length; i++) {
		var link = links[i];
		var image = link.getElement("IMG");
		
		if (link && image) {
			var src = image.getProperty("src");
			var id = link.getProperty("href").split("id=")[1];
			
			link.Data = { };
			link.Data.ID = id;
			link.Data.Previous = null;
			link.Data.Next = null;
			
			if (links.length - 2 >= 0) { link.Data.Previous = links[i - 1]; }
			if (i + 1 < links.length) { link.Data.Next = links[i + 1]; }
			
			if (gallery && !Website.Vars.IsMSIE) {
				image.setStyle("background-image", "url('" + src + "')");
				image.setProperty("src", Website.Gallery.Blank);
				
				link.addEvent("mouseover", function() { Website.Gallery.Thumbs.Over(this); });
				link.addEvent("mouseout", function() { Website.Gallery.Thumbs.Out(this); });
			}
			
			link.addEvent("click", function() { return Website.Gallery.Lightbox.Load(this.Data); });
		}
	}
	
	// Setup the overlay items.
	Website.Gallery.InitOverlay();
}

Website.Gallery.InitOverlay = function() {
	var screenOverlay = $("overlayForeground");
	var lightboxImage = $("lightboxImage");
	
	if (lightboxImage && screenOverlay) {
		lightboxImage.inject(screenOverlay);
		
		var close = lightboxImage.getElement(".close");	
		if (close) { close.addEvent("click", function() { Website.Gallery.Lightbox.Close(); }); }
		
		var previous = lightboxImage.getElement(".prev");
		var next = lightboxImage.getElement(".next");
		
		if (previous) { previous.addEvent("click", function() { return Website.Gallery.Lightbox.Load(this.Data); }); }
		if (next) { next.addEvent("click", function() { return Website.Gallery.Lightbox.Load(this.Data); }); }
		
		var comments = lightboxImage.getElement(".comments");
		
		if (comments) {
			comments.DefaultHeight = 36;
			comments.setStyle("height", 0);
		}
	}
}

Website.Gallery.Thumbs.Over = function(thumb) {
	thumb = $(thumb);
	var image = thumb.getElement("IMG");
	
	if (image) {
		if (image.Effect != null) {
			image.Effect.cancel();	
		}
		
		image.Effect = new Fx.Morph(image, {duration: Website.Animations.Duration, transition: Website.Animations.Transition });
		image.Effect.start({ 'margin-top': -4, 'margin-left': -4 });
		
		image.setProperty("src", Website.Gallery.ThumbOverlay);
	}
}

Website.Gallery.Thumbs.Out = function(thumb) {
	thumb = $(thumb);
	var image = thumb.getElement("IMG");
	
	if (image) {
		if (image.Effect != null) {
			image.Effect.cancel();	
		}
		
		image.Effect = new Fx.Morph(image, {duration: Website.Animations.Duration, transition: Website.Animations.Transition });
		image.Effect.start({ 'margin-top': 0, 'margin-left': 0 });
		
		image.setProperty("src", Website.Gallery.Blank);
	}
}

Website.Gallery.Lightbox.Open = function() {
	document.html.style.overflow = "hidden";
	document.body.style.overflow = "hidden";
	
	var overlay = $("overlay");
	Website.Gallery.Lightbox.Reset();
	
	if (overlay) {
		if (overlay.IsOpen != true) {
			overlay.setStyle("display", "block");
			
			overlay.IsOpen = true;
		}
	}
}

Website.Gallery.Lightbox.Close = function() {
	var overlay = $("overlay");
	
	document.html.style.overflow = "auto";
	document.body.style.overflow = "auto";
	
	if (navigator.userAgent.indexOf("Chrome/") > -1) {
		document.html.style.overflowY = "scroll";
	}
	
	if (overlay) {
		if (overlay.IsOpen == true) {
			overlay.setStyle("display", "none");
			Website.Gallery.Lightbox.Reset();
			
			overlay.IsOpen = false;
		}
	}
}

Website.Gallery.Lightbox.Startup = function(id) {
	var link = $(id);
	
	if (link) {
		Website.Gallery.Lightbox.Load(link.Data);
	}
}

Website.Gallery.Lightbox.Load = function(data) {
	var overlay = $("overlay");
	
	var doLoad = function() {

		// If the image has not been previously loaded, then get the data from the webservice.
		if (Website.Gallery.Cache[data.ID] == null) {
			var onSuccess = function(responseText, responseXML) {
				var json = null;
				
				if (responseText && responseText.length > 0) {
					try {
						json = JSON.decode(responseText);
					} catch (ex) { }
				}
				
				if (json == null) {
					alert("Failed to load image details. Please refresh and try again.");
				} else {
					Website.Gallery.Lightbox.Loaded(data, json);
				}
			}
			
			var myRequest = new Request({
				url: Website.URLRoot + "services/gallery.php?getByID",
				method: 'post',
				onComplete: onSuccess
			});
			
			myRequest.send("id=" + data.ID);
	
		// Otherwise, retrieve the data from the cache.
		} else {
			Website.Gallery.Lightbox.Build(data, Website.Gallery.Cache[data.ID]);
		}
	}
	
	if (overlay) {
		if (overlay.IsOpen == true) {
			var lightboxImage = $("lightboxImage");
			
			if (lightboxImage) {
				var image = lightboxImage.getElement(".image");
				
				if (image) {
					var completeHideEffect = function() {
						Website.Gallery.Lightbox.Reset();
						doLoad();
					}
					
					image.Effect = new Fx.Morph(image, {duration: Website.Animations.Duration, transition: Website.Animations.Transition, onComplete: completeHideEffect });
					image.Effect.start({ 'opacity': 0.1 });
				}
				
				Website.Gallery.Lightbox.Text();
				Website.Gallery.Lightbox.NavigationButtons();
			}
			
		} else {
			Website.Gallery.Lightbox.Open();
			doLoad();
		}
	}
	
	return false;
}

Website.Gallery.Lightbox.Loaded = function(data, json) {
	if (json) {
		var record = json[0];
		
		Website.Gallery.Cache[data.ID] = record;
		Website.Gallery.Lightbox.Build(data, record);	
	}
}

Website.Gallery.Lightbox.Build = function(data, record) {
	if (record) {
		var lightboxImage = $("lightboxImage");
		
		if (lightboxImage) {
			var image = lightboxImage.getElement(".image");
			
			if (image) {
				var completeSizeEffect = function() {
					var url = Website.URLRoot + "assets/images/gallery/" + record.imageFileName;
					
					image.setProperty("src", url);
					image.setProperty("title", record.imageTitle);
					
					image.setStyle("opacity", "0.1");
					image.setStyle("visibility", "visible");
					
					var completeShowEffect = function() {
						Website.Gallery.Lightbox.Text(record);
						Website.Gallery.Lightbox.NavigationButtons(data);
					}
					
					image.Effect = new Fx.Morph(image, {duration: Website.Animations.Duration, transition: Website.Animations.Transition, onComplete: completeShowEffect });
					image.Effect.start({ 'opacity': 1 });
				}
				
				image.Effect = new Fx.Morph(image, {duration: Website.Animations.Duration, transition: Website.Animations.Transition, onComplete: completeSizeEffect });
				image.Effect.start({ 'width': record.imageWidth, 'height': record.imageHeight });
			}
		}
	}
}

Website.Gallery.Lightbox.Text = function(record) {
	var lightboxImage = $("lightboxImage");
	
	if (lightboxImage) {
		var comments = lightboxImage.getElement(".comments");
		
		if (comments) {
			var imageTitle = comments.getElement(".imageTitle");
			var imageDetails = comments.getElement(".imageDetails");
			var imageCopyright = comments.getElement(".imageCopyright");
			
			if (comments.Effect != null) {
				comments.Effect.cancel();	
			}
			
			if (record) {
				var completeEffect = function() {
					if (imageTitle) { imageTitle.setProperty("text", record.imageName); }
					if (imageDetails) { imageDetails.setProperty("text", record.imageProject); }
				}
				
				comments.setStyle("display", "block");
				
				comments.Effect = new Fx.Morph(comments, {duration: Website.Animations.Duration, transition: Fx.Transitions.Linear, onComplete: completeEffect });
				comments.Effect.start({ 'height': comments.DefaultHeight });
			
			} else {
				if (imageTitle) { imageTitle.setProperty("text", ""); }
				if (imageDetails) { imageDetails.setProperty("text", ""); }
				
				var completeEffect = function() {
					comments.setStyle("display", "none");
				}
				
				comments.Effect = new Fx.Morph(comments, {duration: Website.Animations.Duration, transition: Fx.Transitions.Linear, onComplete: completeEffect });
				comments.Effect.start({ 'height': 0 });
			}
		}
	}
}

Website.Gallery.Lightbox.NavigationButtons = function(data) {
	var lightboxImage = $("lightboxImage");
	
	if (lightboxImage) {
		var image = lightboxImage.getElement(".image");
			
		if (image) {		
			var previous = lightboxImage.getElement(".prev");
			var next = lightboxImage.getElement(".next");
			
			if (previous) {
				if (data && data.Previous) {
					previous.Data = data.Previous.Data;
					previous.setStyle("display", "block");
				} else {
					previous.Data = null;
					previous.setStyle("display", "none");
				}
			}
			
			if (next) {
				if (data && data.Next) {
					next.Data = data.Next.Data;
					
					next.setStyle("display", "block");
					next.setStyle("margin-left", (image.getWidth() - 11) + "px");
					
				} else {
					next.Data = null;
					next.setStyle("display", "none");
				}
			}
		}
	}
}

Website.Gallery.Lightbox.Reset = function() {
	var lightboxImage = $("lightboxImage");
	
	if (lightboxImage) {
		var image = lightboxImage.getElement(".image");
		
		if (image) {
			image.setStyle("visibility", "hidden");
			image.setProperty("src", Website.Gallery.Blank);	
		}
		
		Website.Gallery.Lightbox.Text();
		Website.Gallery.Lightbox.NavigationButtons();
	}
}