/**
 * Examines the current URL to determine whether it specifies a gallery image
 * and loads the image in the gallery if so
 */
function galleryLoader()
{
	var galleryUrlPrefix = "/gallery/";
	var hasMatchingHash = location.hash.match(/#gallery-image-[\d\w]+-\d+$/);
	
	// The number of gallery/lightbox links on the page that need gallery scripts
	// to be preloaded. If there are no such links, it is assumed that the
	// necessary scripts have already been loaded
	var preloaderLinkCount = $('.needs_gallery_preloader').length;
	var isBaseGalleryUrl = location.pathname.toLowerCase() === "/gallery";
	
	if (location.pathname.substr(0, galleryUrlPrefix.length).toLowerCase() === galleryUrlPrefix.toLowerCase() ||
		hasMatchingHash || preloaderLinkCount > 0 || isBaseGalleryUrl) {
		// Dynamically load the gallery JS files
		if (typeof Gallery === "undefined" || typeof $.galleriffic == "undefined") {
			var scripts = ['/components/jquery/jquery.galleriffic-2.0.1.js',
							'/components/jquery/jquery.history-20091014.js',
							'/components/jquery/jquery.opacityrollover-2.0.1.js',
							'/components/jquery/jquery.form.js',
							'/components/jquery/jquery.validate.min.js',
							'/templates/default/gallery/js/gallery.js',
							'/templates/default/js/Login.js'
							];
			
			var options = {
				globals:		["Gallery"],
				waitObjects:	[$],
				waitMembers:	[["historyInit","galleriffic"]]
			};
			
			// If the preload link count is 0, we can safely load the gallery
			if (preloaderLinkCount === 0) {
				options.onLoad = function () {
					var path = location.pathname.substr(1);
					
					// Strip trailing slash from path
					if (path[path.length - 1] == '/') {
						path = path.substr(0, path.length -1);
					}
					
					path = path.split('/');
					
					if (path.length == 1) {
						// Display a list of collections if a picture has not been specified
						// This list can be disabled only on "/gallery". This is accomplished
						// by setting the global disableCollectionListingPopups flag to true.
						var disableCollectionList = (typeof disableCollectionListingPopups !== "undefined" && disableCollectionListingPopups);
						
						if (!disableCollectionList || (disableCollectionList && !isBaseGalleryUrl)) {
							Gallery.loadCollectionList();
							return;
						}
						
						if (disableCollectionList && isBaseGalleryUrl) {
							// Don't attempt to load the gallery from /gallery if
							// the disableCollectionList flag is set (this flag
							// is effectively an isJos01 flag)
							return;
						}
					}
					
					var pic;
					if (hasMatchingHash) {
						pic = location.hash.substr(location.hash.lastIndexOf('-') + 1);
					}
					
					// The gallery at /gallery/collection-name breaks (thumbnails are not "clickable")
					// in Firefox (tested on 3.6.3) without this code being in a timeout
					setTimeout(function () {
						Gallery.load(null, pic);
						
						// Update the current image
						Gallery.onHashChange(location.hash);
					}, 100);
				};
			} else {
				options.onLoad = function () {
					// Attach the onclick event handlers
					$('.needs_gallery_preloader').click(function () {
						fnLoadLightbox({ initDialog: true});
					});
				};
			}
			
			loadScripts(scripts, options);
		}
	}
}

$(document).ready(galleryLoader);
