

	$(document).ready(function() {
		var hMax = 0;
		$('#page').children().each(function() {
			var posOfItemBottomEdge = parseInt($(this).position().top, 10) + parseInt($(this).height(), 10);
			hMax = Math.max( hMax,  posOfItemBottomEdge);
		});
		var pagePadding = parseInt( $('#page').css('padding-left').split("px")[0] , 10 );
	//	$('#page').height(hMax-pagePadding);
		if (document.all) { 	// Existence of IE-only function 'document.all' means we need to modify the page width and height
	//		$('#page').height(hMax+pagePadding).css({'width':'960px'});  // HACK!!! Fix the attemped use of addClass('pageIE') to do this
		} 
		
	// -- MOVE TO STANDARD JS FILE WHEN IT EXISTS -- //
		
		// Activate button effect		
		$('.button').click(function() {
			$(this).css({'border':'1px #ccc solid'});
			$(this).animate(
				{opacity:0.9},
				500,
				function(){ 
					$(this).css({'border':'none', 'border-right':'1px solid #ddc', 'border-bottom':'1px solid #ccb', 'opacity':'1.0'});
				}
			);
			window.location.href = "http://www.aispire.co.uk/free/contact-details/";
		});
		
		// get rid of <br>s put in by Wordpress
		$(".imageBlock").find("br").each(function(){ $(this).remove(); });
		
		// Handle click on portfolio image
		function getPhotoIndx(outerFname, listOfPhotos) {
			var indxToReturn=-1;
			listOfPhotos.each(function(indx,value) {
				var innerFname = $(this).attr("src");
				if (innerFname==outerFname) { indxToReturn=indx; }
			});
			return indxToReturn; 
		}
		function makeStrip() {
			var strip = $("<div id='strip' style='opacity:0.0; border-top:3px solid #922; border-bottom:3px solid #922; background-color:#000;'></div>");
			$(".portfolioImage").each(function(indx,value) {
				var src    = $(this).attr("src");
				var title  = $(this).attr("title");
				var newImg = $("<img id='portfolioImg"+indx+"' style='float:left; height:500px; padding:10px; background-color:#000;' src='"+src+"' alt=''  title='"+title+"' />");
				strip.append(newImg);
			});
			return strip;
		}
		function getStripWidth() {
			var requiredWidth = 0;
			$('#strip img').each(function() {
				var w = $(this).width();
				requiredWidth += w+20;
			});
			return requiredWidth;
		}
		function getImgXPos(fnameIndx) {
			var xPos = 0;
			$('#strip img').each(function(indx,value) {
				if (indx<fnameIndx) {
					var w = $(this).width();
					xPos += w;
				}
			});
			return xPos;
		}
		function showChosenImage(imgObj,listOfPhotos) {
			$('#strip img').animate({'opacity':0.5},100);
			// find image one in the list
			var scrollLeft = $(window).scrollLeft();
			var fname     = imgObj.attr("src");
			var fText     = imgObj.attr("title");
			var fnameIndx = getPhotoIndx(fname, listOfPhotos);
			// centre this image
			var imgWidth = $('#strip img')[fnameIndx].width;
			var viewPortwidth  = $(window).width();
			var leftAdjustment = (-20*fnameIndx) + (viewPortwidth-imgWidth)/2;
			var xPos = scrollLeft+getImgXPos(fnameIndx);
			var $strip = $('#strip').animate({
				'left':-xPos, 'margin-left':leftAdjustment
			},
			400);
			imgObj.animate({'opacity':1.0},600);
			$('#haze').html("").append("<p style='position:absolute; top:50px; left:20px; width:100%; color:#fff; font-style:normal; text-align:center; text-shadow:none;'>"+fText+"</p>");		
		}
		$(".portfolioImage").click(function() {
			// get all the portfolio images on the page, making a list
			var listOfPhotos = $(".portfolioImage");
			// find this one in the list
			var fname     = $(this).attr("src");
			var fnameIndx = getPhotoIndx(fname, listOfPhotos);
			// make the portfolio strip and attatch it to the body
			$('body').append( "<div id='haze'> &nbsp; </div>" );
			$('body').append( makeStrip() );
			requiredWidth = getStripWidth();
			var imgWidth = $('#strip img')[fnameIndx].width;
			var viewPortwidth  = $(window).width();
			var viewPortHeight = $(window).height();
			var leftAdjustment = (-20*fnameIndx) + (viewPortwidth-imgWidth)/2;
			var scrollLeft = $(window).scrollLeft();
			var scrollTop  = $(window).scrollTop();
			$('#haze').css({'top':scrollTop+'px', 'left':scrollLeft+'px'});
			var xPos = scrollLeft+getImgXPos(fnameIndx);
			var yPos = scrollTop+(viewPortHeight/2);
			$strip = $('#strip').css({
				'position':'absolute', 'z-index':'1000',
				'left':'-'+xPos+'px', 'margin-left':leftAdjustment+'px', 
				'top':yPos+'px', 'margin-top':'-250px', 
				'width':requiredWidth+'px'
			}).animate(
				{'opacity':1.0},
				400,
				function() {
					// Handle click on strip image
					$('#strip img').click(function() {
						showChosenImage($(this), listOfPhotos);
					});				
				}
			);
			$('#strip img').animate({'opacity':0.5},400);
			showChosenImage($('#strip img').eq(fnameIndx),listOfPhotos);
			// quit
			$('#haze').click(function() {
				$('#strip').remove();
				$('#haze').animate({'opacity':'0.0'},500,function(){ $('#haze').remove(); });
			});
		});
		
			
	// -- END --	
		
	});
