
window.Clients = {
	
	currentSet: null,
	maxHeight: 0,
	
	convertChip: function () {
		
		// Get the three attributes
		var jthis = $(this);
		var caption = jthis.find("h3").html();
		var text = jthis.find("p").html();
		var url = jthis.find(".url").html();
		
		// Replace chip with the image (path is relative to jquery?)
		var image = $("<img src='images/linked-to/" + url + "' width='355' height='275' />");
		jthis.parent().append(image);
		jthis.remove();
		
		// Store caption and text as attributes
		image.get(0).caption = caption
		image.get(0).text = text
		
	},
	
	layoutSlide: function () {
		
		// Set all the images' positions
		var left = 0;
		var height = 0;
		var num = 0;
		$(this).find("img").each(function (i) {
			$(this).css({
				position: "absolute",
				left: left,
				top: 0
			});
			left += $(this).width();
			height = Math.max(height, $(this).height());
			num++;
		});
		
		// Set the slide height
		$(this).add(".slideInner").css("height", height);
		
		// Store the current position, etc
		this.currentImage = 0;
		this.numImages = num;
		
		var image = $(this).find("img").get(0);
		if (!!image) { // We might have no images
			$(this).parent().find(".extra .caption").html(image.caption);
			$(this).parent().find(".extra .text").html(image.text);
		}
		
		// Store slide max height
		Clients.maxHeight = Math.max(Clients.maxHeight, $(this).height())
		
	},
	
	shiftBy: function (slide, by) {
		
		slide.currentImage = (slide.currentImage + by) % slide.numImages;
		if (slide.currentImage < 0) { // javascript has broken modulo
			slide.currentImage += slide.numImages;
		}
		Clients.shift(slide);
		
	},
	
	shift: function (slide) {
		// Shifts a slide so it matches its attributes
		
		var image = $(slide).find("img").get(slide.currentImage);
		if (!image) { return; } // Might be no images
		var target = 0 - image.offsetLeft;
		$(slide).find(".slideInner").animate({left: target}, "normal", "swing");
		
		// Set captions
		$(slide).parent().find(".extra .caption").html(image.caption);
		$(slide).parent().find(".extra .text").html(image.text);
		
	},
	
	navClicked: function (e) {
		// Get this link's panel ID
		var id = Clients.getClientIdFromHref(this.href);
		// Show it
		Clients.showPanelAndNav(id,this);
		// Change the anchor value
		// This isn't great when the page first loads
		// window.location.hash = id;

		e.preventDefault();
	},

	showPanelAndNav: function(panelId,navElem) {
		// Get element from ID
		var panel = $("#"+panelId).get(0);
		
		// What's its index?
		var index = $(".panel").index(panel);
		
		// Show it
		Clients.showPanel(index);
		
		// Change highlight
		$(".navigation li").removeClass("selected");
		$(navElem).parent().addClass("selected");
	},
	
	getClientIdFromHref: function(href) {
		/* Get a valid client panel ID from the href # anchor */
		var ids = href.split("#");
		var clientId = ""; // Yarrrrrrr anchorrrrrs
		if ((ids.length > 1) && (ids[1].search(/^c-\d+$/) != -1)) {
			clientId = ids[1];
		}
		return clientId;
	},

	getNavLinkFromClientId: function(cid) {
		/* From a client ID, reverse looks up the nav link */
		return $("#slider .navigation a[href$='#"+cid+"']");
	},

	showPanel: function (index) {
		
		// Only if this isn't the current panel...
		if (index != Clients.currentSet) {
			
			var oldpanel = $(".panel").get(Clients.currentSet);
			var newpanel = $(".panel").get(index);
			
			var showfunc = function () {
				$(newpanel).slideDown("normal");
				Clients.currentSet = index;
			}
			
			// Hide the old panel
			if (!!oldpanel) {
				$(oldpanel).slideUp("normal", showfunc);
			} else {
				showfunc();
			}
			
		}
		
	},
	
	go: function () {
		// Add caption stuff to buttons divs
		$(".panel .buttons").append("<div class='extra'><h5 class='caption'></h5><p class='text'></p></div>");
		
		// Convert the slides into actual slides
		$(".panel .slide .chip").each(Clients.convertChip);
		
		// Lay out each lot of slides
		$(".panel .slide").each(Clients.layoutSlide);
		
		// Fix scroll to the right height
		$(".scroll").height(Clients.maxHeight + 130);
		
		// Hook up the buttons
		$(".buttons .right").click(function () {
			Clients.shiftBy($(this).parents(".panel").find(".slide").get(0), 1);
			return false;
		});
		$(".buttons .left").click(function () {
			Clients.shiftBy($(this).parents(".panel").find(".slide").get(0), -1);
			return false;
		});
		
		// Hide all clients
		$(".panel").hide();
		
		// Associate links with selection
		$(".navigation a").click(Clients.navClicked);
		
		// Open first client, if no client chosen on navigation
		var clientId = Clients.getClientIdFromHref(location.href);
		if (clientId == "") {
			$(".navigation a:first").click();
		}
		else {
			var nav = Clients.getNavLinkFromClientId(clientId);
			nav.click();
		}
		
	}
	
}

$(Clients.go);
