LINEUKCOM.pages.client = {
	init: function() {
		// Add click event to all image links.
		jQuery('#client_images a').click(LINEUKCOM.pages.client.getClientImage);
		jQuery('#client_info_menu').show(); // Show the copy menu ...
		jQuery('#client_info div').hide(); // ... but hide the copy.
		jQuery('#client_info div h2').hide(); // Hide the h2s.
		if (jQuery.inArray(window.location.hash, ['#background', '#what_we_did', '#results']) > 0) {
			// If a section is referenced in the URL hash, show that first ...
			jQuery('#client_info_menu a[href="' + window.location.hash + '"]').addClass('inuse');
			jQuery(window.location.hash).show();
		}
		else {
			// ... else show the 'Background'
			jQuery('#client_info_menu a[href="#background"]').addClass('inuse');
			jQuery('#background').show();
		}
		jQuery('#client_info_menu a').click(LINEUKCOM.pages.client.showSection);
	},

	getClientImage: function() {
		// Build the dictionary of parameters than need to be sent along with
		// AJAX request.  We need to make sure any existing parameters are
		// included too.
		var params = {'ajax': 'true'};
		var queryString = window.location.search.substr(1); // Remove '?'.
		if (queryString) {
			var pairs = queryString.split('&');
			for (var pair in pairs) {
				var arg = pairs[pair].split('=');
				if (arg[0] != 'img') {
					params[arg[0]] = arg[1];
				}
			}
		}
		// The surrogate is a span that's in place of a link (to stop a
		// page linking to itself).  This needs to be replaced with a
		// real link when another image link is clicked on.
		var surrogate = jQuery('#client_images span.surrogate');
		var location = surrogate.attr('href');
		if (!location) {
			location = window.location.pathname;
		}
		surrogate.replaceWith('<a href="' + location + '" id="just_created">' +
			surrogate.text() + '</a>');
		jQuery('#just_created').click(
			LINEUKCOM.pages.client.getClientImage).removeAttr('id');

		// Change the link just clicked on to a surrogate span.
		var anchor = jQuery(this);
		jQuery.getJSON(anchor.attr('href'), params,
			LINEUKCOM.pages.client.swapClientImage);
		anchor.replaceWith('<span class="surrogate" href="' +
			anchor.attr('href') + '">' + anchor.text() + '</span>');
		return false;
	},

	swapClientImage: function(data, textStatus) {
		// Fade the current image out, replace the image, and fade back in.
		var client_image = jQuery('#client_image img');
		var visit_site = jQuery('#visit_site');
		client_image.fadeOut(400, function() {
			client_image.attr('src', data.source);
			client_image.attr('alt', data.description);
			client_image.fadeIn(400);
		});
		visit_site.fadeOut(400, function() {
			if (data.description && data.url) {
				visit_site.html('<a href="' + data.url + '">' + data.description + '</a>');
			}
			else if (data.description) {
				visit_site.html(data.description);
			}
			else if (data.url) {
				visit_site.html('<a href="' + data.url + '">Visit site</a>');
			}
			else {
				visit_site.text('\xa0');
			}
			visit_site.fadeIn(400);
		});
	},

	showSection: function() {
		var anchor = jQuery(this);
		var section = jQuery(anchor.attr('href'));
		if (section.is(':hidden')) {
			jQuery('#client_info_menu a').removeClass('inuse');
			anchor.addClass('inuse');
			jQuery('#client_info div:visible').slideUp(400, function() {
				section.slideDown(400);
			});
		}
		return false;
	}
}

jQuery(document).ready(LINEUKCOM.pages.client.init);
