

	/************************************************************************
	 * Support functions 
	 ************************************************************************/

	// Load the given image URL into the #imageDisplay section of the page
	function loadImage(URL) {
		var img = new Image();
		jQuery('#imageDisplay').addClass('loading').html('');
		jQuery(img).load(function() {
			jQuery('#imageDisplay').removeClass('loading').html(this);
		})
		.error(function() {
			// Nothing here
		})
		.attr('src', URL);
	}
	
	function registerImageClickActions(selector) {
		
		// Register the click action to load the selected image
		if (selector == null) {
			var s = 'a.aviation';
		} else {
			var s = selector;
		} 
		jQuery('a.aviation').click(function() {
			var URL = jQuery(this).attr('href');
			// Fade out the existing image and then load in the new one
			//jQuery('#imageDisplay img').fadeOut('normal',function() {
				loadImage(URL);
			//});
			return false;
		});
		
	}

	function getChunkStart(obj) {
		if (obj == null) return;
		var firstItemsChunkClasses = jQuery(obj).attr('class');
		if (firstItemsChunkClasses == null) return;
		var classArray = firstItemsChunkClasses.split(' ');
		for( var c = 0; c < classArray.length; c++) {
			if (classArray[c].substring(0,11) == "chunkStart-") {
				return parseInt(classArray[c].substring(11));
			}
		}
	}
	
	function getChunkSize(obj) {
		var firstItemsChunkClasses = jQuery(obj).attr('class');
		if (firstItemsChunkClasses == null) return;
		var classArray = firstItemsChunkClasses.split(' ');
		for( var c = 0; c < classArray.length; c++) {
			if (classArray[c].substring(0,10) == "chunkSize-") {
				return parseInt(classArray[c].substring(10));
			}
		}
	}
	
	function getPreviousChunk(urlSegment,start,obj, callback) {
		jQuery.ajax({
				type: 		'GET',
				url: 			urlSegment+'AjaxPreviousGalleryItems?isAjax=1&start='+start,
				success: 	function(data) {
											jQuery(obj).before(data);
											jQuery(obj).prev().addClass('previous');
											jQuery(obj).prev().removeClass('current');
											registerImageClickActions(jQuery(obj).prev());
											if (callback) {
												callback(obj);
											}
										}
				});
	}
	
	function getNextChunk(urlSegment,start,obj,callback) {
		jQuery.ajax({
				type: 		'GET',
				url: 			urlSegment+'AjaxNextGalleryItems?isAjax=1&start='+start,
				success: 	function(data) {
											if (data) {
												jQuery(obj).after(data);
												jQuery(obj).next().addClass('next');
												jQuery(obj).next().removeClass('current');
												registerImageClickActions(jQuery(obj).next());
											}
											if (callback) {
												callback(obj);
											}
										}
				});
	}
	
	function initGallery(urlSegment) {	
		
		var callingURL = window.location.pathname;
		if(urlSegment != null) {
			var callingURL = urlSegment;
		}
		
	/************************************************************************
	 * Register Action Callbacks 
	 ************************************************************************/
	
	// Register the default click Actions for the first set of images loaded
	registerImageClickActions(null);

	jQuery('.pagination-imagegallery.next').click(function() {
		
		// Record that we are executiing
		var thisRef = jQuery(this);
		if (thisRef.data('executing') || thisRef.data('animating') || thisRef.data('loading')) {
			return false;
		}
		thisRef.data('executing', true);
		
		if (!jQuery('.gallery-layout.current').next().is('.gallery-layout.next')) {
			thisRef.removeData('executing');
			return false;
		}

			// Slide in next chunk in DOM
			jQuery('.gallery-layout.current').animate( { left: -572 }, 1000, function() { 
					// Make this the previous class
					jQuery(this).removeClass('current'); 
					jQuery(this).addClass('previous'); 
					jQuery('.pagination-imagegallery.previous').removeClass('hidden');
				});
			thisRef.data('animating',true);	
			var nextChunk = jQuery('.gallery-layout.current').next();
			jQuery(nextChunk).animate( {left: 0}, 1000, function() { 
					// Make this the current class
					jQuery(this).removeClass('next'); 
					jQuery(this).addClass('current');
					thisRef.removeData('animating');				
				});
			if (!jQuery(nextChunk).next().is('.gallery-layout.next')) {
				var chunkSize = getChunkSize(nextChunk);
				var chunkStart = getChunkStart(nextChunk);
				thisRef.data('loading',true);
				getNextChunk(callingURL,chunkStart,nextChunk, function(obj) {
					// Need to check to see if there was no load in which case disable the next button
					if (!jQuery(obj).next().is('.gallery-layout.next')) {
						jQuery('.pagination-imagegallery.next').addClass('hidden');
					}
					thisRef.removeData('loading');
				});
			}

			thisRef.removeData('executing');

			return false;
	});
	
	jQuery('.pagination-imagegallery.previous').click(function() {
		
		// Record that we are executiing
		var thisRef = jQuery(this);
		if (thisRef.data('executing') || thisRef.data('animating') || thisRef.data('loading')) {
			return false;
		}
		thisRef.removeData('loading');
		thisRef.removeData('animating');
		thisRef.data('executing', true);
		
		// Slide in next chunk in DOM
		if (!jQuery('.gallery-layout.current').prev().is('.gallery-layout.previous')) {
			thisRef.removeData('executing');
			return false;
		}
		jQuery('.gallery-layout.current').animate( { left: 572 }, 1000, function() { 
				// Remove the previous tag from the current previous
				// Make this the previous class
				jQuery(this).removeClass('current'); 
				jQuery(this).addClass('next');
				jQuery('.pagination-imagegallery.next').removeClass('hidden'); 
			});
		thisRef.data('animating',true);
		var previousChunk = jQuery('.gallery-layout.current').prev();
		jQuery(previousChunk).animate( {left: 0}, 1000, function() { 
				// Make this the current class
				jQuery(this).removeClass('previous'); 
				jQuery(this).addClass('current');
				thisRef.removeData('animating');
			});
		if (!jQuery(previousChunk).prev().is('.gallery-layout.previous')) {
			var chunkSize = getChunkSize(previousChunk);
			var chunkStart = getChunkStart(previousChunk);
			thisRef.data('loading',true);
			getPreviousChunk(callingURL,chunkStart,previousChunk, function(obj) {			
					// Need to check to see if there was no load in which case disable the previous button
					if (!jQuery(obj).prev().is('.gallery-layout.previous')) {
						jQuery('.pagination-imagegallery.previous').addClass('hidden');
					}
					thisRef.removeData('loading');
				});
		}
		
		thisRef.removeData('executing');
		return false;
	});

	/************************************************************************
	 * Initialise Everything
	 ************************************************************************/

	
		// Load the first image in the displayed set
		var firstImage = jQuery('.current div#gImage-1 a').attr('href');	
		loadImage(firstImage);

		// Initialise the Ajax Slider
		
		// What is the chunk size and the chunk number
		var firstItemsChunk = jQuery('.gallery-layout:first');
		var chunkSize = getChunkSize(firstItemsChunk);
		var chunkStart = getChunkStart(firstItemsChunk);

		// Load any previous chunks
		getNextChunk(callingURL,chunkStart,firstItemsChunk,null);

		// Work out if there should be any next items
		var nextItemsChunk = jQuery(firstItemsChunk).next();
		if (!nextItemsChunk.is('.gallery-layout')) {
			// There should be a next chunk
			getPreviousChunk(callingURL,chunkStart,firstItemsChunk,null);
		}
	}

