// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;

// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;

// CSS border size x 2
var borderSize = 10;

// Photo directory for this gallery
var photoDir = "images/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
 new Array("bodywork001.jpg", "733", "550", "Remains of side rubbing strips"),
 new Array("bodywork002.jpg", "413", "550", "Lower front O/S wing, only this area was replaced to save the complete wing"),
 new Array("bodywork003.jpg", "413", "550", "Front valence"),
 new Array("bodywork004.jpg", "733", "550", ""),
 new Array("bodywork005.jpg", "733", "550", ""),
 new Array("bodywork006.jpg", "413", "550", ""),
 new Array("bodywork007.jpg", "413", "550", "Trail fit of the rear spoiler, the car is a LS, so didn't come with one from the factory"),
 new Array("bodywork008.jpg", "733", "550", ""),
 new Array("bodywork009.jpg", "688", "550", "Rear lower quarter, this was the worst part on the car, once all rust was cut away to good metal started rebuilding."),
 new Array("bodywork010.jpg", "688", "550", "Although it was the worst part minimal metal was need to be removed"),
 new Array("bodywork011.jpg", "688", "550", "All the rest of the metal was treated and zinc'd before building back up"),
 new Array("bodywork015.jpg", "688", "550", "Rear Valence, minor scab, but a botched job by previous owner"),
 new Array("bodywork016.jpg", "688", "550", ""),
 new Array("bodywork017.jpg", "688", "550", "New sils - blending in - later decided to keep the shut line"),
 new Array("bodywork018.jpg", "688", "550", "Rear lower quarter"),
 new Array("bodywork019.jpg", "688", "550", "Rear valence"),
 new Array("bodywork020.jpg", "688", "550", "Rear valence"),
 new Array("bodywork021.jpg", "688", "550", "Work on the front leading edge of the bonnet"),
 new Array("bodywork025.jpg", "733", "550", "Rear valence, treated, filled and primered to see how much of a bad job I'd done."),
 new Array("bodywork026.jpg", "688", "550", "New sils blended in and primered"),
 new Array("bodywork028.jpg", "688", "550", "My new best friend"),
 new Array("bodywork029.jpg", "688", "550", "treated the inside of the door and started to zinc primer it"),
 new Array("bodywork030.jpg", "688", "550", "Stripped back the sill to bodywork shutline, neatened everything up and treated metal"),
 new Array("bodywork031.jpg", "688", "550", "Primered to see imperfections"),
 new Array("bodywork032.jpg", "688", "550", "Quite a few imperfections, more work needed"),
 new Array("bodywork033.jpg", "413", "550", "Petrol door flap, rusty"),
 new Array("bodywork034.jpg", "733", "550", ""),
 new Array("bodywork035.jpg", "688", "550", "Ground out as much of the crap as I could and flooded it in Zinc primer"),
 new Array("bodywork036.jpg", "688", "550", "It did blast a hole in the outside face"),
 new Array("bodywork037.jpg", "688", "550", "Primered and rubbed down ready for top coat - at least something is!"),
 new Array("bodywork038.jpg", "688", "550", ""),
 new Array("bodywork039.jpg", "733", "550", "Rear lower quater, this had some bubbles of rust, so I striped it back to bare metal to see the damage - not a lot really"),
 new Array("bodywork040.jpg", "413", "550", "slightly worse around the side bumper mounts, thats foam within the hole, not more rust."),
 new Array("bodywork041.jpg", "733", "550", "As I strated to dig further and further I found more and more tin worm, so I got drastic with the angle grinder..."),
 new Array("bodywork042.jpg", "733", "550", "Most of the bent metal removed"),
 new Array("bodywork043.jpg", "733", "550", "Treating the rust"),
 new Array("bodywork044.jpg", "733", "550", ""),
 new Array("bodywork045.jpg", "413", "550", "Removing more trim and more rust"),
 new Array("bodywork046.jpg", "413", "550", "The new pattern wing and original door arn't a perfect match, I'm just raising the top crease line and lowering the bottom crease line on the wing to match the door, not a lot mind."),
 new Array("bodywork047.jpg", "413", "550", "This is the biggest pain, the wing fits perfectly everywhere else (bar the crease lines), except here. Suggested to me weld a bead to close the gap then grind to shape. This is the before shot."),
 new Array("bodywork048.jpg", "733", "550", "Half way through sorting the shut line"),
 new Array("bodywork049.jpg", "413", "550", "The shoulder line now lines up perfectly"),
 new Array("bodywork050.jpg", "413", "550", ""),
 new Array("bodywork051.jpg", "413", "550", "Closed the gap between the door and wing"),
 new Array("bodywork052.jpg", "413", "550", "Sorted, this gap is now the same size as all the others"),
 new Array("bodywork053.jpg", "413", "550", "Front wing now finished and lines up correctly now."),
 new Array("bodywork054.jpg", "413", "550", "Starting work on the rear quarter, the lower sil will be next once I've welded it in place."),
 new Array("bodywork055.jpg", "733", "550", "Rear lower "),
 new Array("bodywork056.jpg", "413", "550", "Flatted half the bonnet and sorted out some dents"),
 new Array("bodywork057.jpg", "733", "550", "New welder for the last few jobs"),
 new Array("bodywork058.jpg", "825", "550", "A new year and a new camera!, this winters been a bit chilly for rolling around the garage floor, it still bloody cold, but if I'm going to have half a chance of finishing it for summer I best get cracking."),
 new Array("bodywork059.jpg", "825", "550", "The following few pictures are a visuals of the main areas I need to fix. The replacement sill was fitting baddly and bucked out, so I've taken the top edge off from the C pillar backwards, I'll weld this to the original sill underneath and profile the top part to make it look seamless."),
 new Array("bodywork060.jpg", "825", "550", "N/S door, a bit crusty this one, also need to watch surface rust on sill, built up over winter"),
 new Array("bodywork061.jpg", "825", "550", "A BIG hole, easy fixed, I also want to sort out the drain hole (read rust collector), and the parts leading onto the rear arch."),
 new Array("bodywork062.jpg", "825", "550", "The rear lower quarter, just some final profiling and preping for paint."),
 new Array("bodywork063.jpg", "825", "550", "Hole in the bonnet, now time to put my newly aquired welding skills to the task..."),
 new Array("bodywork064.jpg", "825", "550", "...yep, maybe a need to practise a little more..! However, there is no longer a hole!"),
 new Array("bodywork065.jpg", "825", "550", "Filler ready for sanding"),
 new Array("bodywork066.jpg", "825", "550", "what a mess."),
 new Array("bodywork067.jpg", "825", "550", "sanded smooth"),
 new Array("bodywork068.jpg", "825", "550", ""),
 new Array("bodywork069.jpg", "825", "550", "flapping rear sill"),
 new Array("bodywork070.jpg", "825", "550", "welded in place, still need more practice before I let myself loose and any other part of the bodywork"),
 new Array("bodywork072.jpg", "367", "550", "door off to gain access to lower parts, don't have massive amounts of space down there!"),
 new Array("bodywork073.jpg", "367", "550", "A-Post in top condition, minor scab to fix"),
 new Array("bodywork074.jpg", "367", "550", "door with glass and frame removed."),
 new Array("bodywork075.jpg", "825", "550", "the hidden rust on the underside..."),
 new Array("bodywork076.jpg", "367", "550", "Glass is now out of the door, ready to butcher it, the dark line to the left is a build up of crap over 25 years."),
 new Array("bodywork077.jpg", "825", "550", "I've knocked through all the small side rubbing mouting holes, these were filled with filler, but I'll weld them up now, as well as sort bottom corner and other rust areas"),
 new Array("bodywork078.jpg", "825", "550", "This door has had a lot of filler in the past, I'm probably going to have to spend a whole day sanding, worried what it might be hiding underneath."),
 new Array("bodywork079.jpg", "367", "550", "Stripped back the door today, man there was a ridiculous amount of filler on it."),
 new Array("bodywork080.jpg", "367", "550", "welded the holes up (instead of filler), starting to get some results with welding now"),
 new Array("bodywork081.jpg", "367", "550", "fully stripped and welded, now sat in primer"),
 new Array("bodywork082.jpg", "367", "550", "took all the grot back to bare metal, treated then primered"),
 new Array("bodywork083.jpg", "825", "550", "After digging out a large hole at the bottom of the passengers door; and cleaning all the rust. I welded in a new sheet of metal, this is a picture of my beautiful welding (!)"),
 new Array("bodywork084.jpg", "825", "550", "After grinding back, bit of paint then more grinding, ready for filler to smooth."),
 new Array("bodywork085.jpg", "825", "550", "Started making up a plate to fill the hole at the rear n/s wheel, then something caught my eye....."),
 new Array("bodywork086.jpg", "825", "550", "....a crack in some filler on the arch, so i started chipping away at it and started to reveal this!!! Eeeek"),
 new Array("bodywork087.jpg", "825", "550", "It looks fairly rusty under all the filler!! You can see 4 drilled holes, as the surface is rockier than the rocky road to dublin, I'm assuming these are pull holes to pull out a dent in the arch. Next job, strip back the whole arch, I've ordered an arch repair panel to fix this lot, wasn't expecting this, gulp!"),
 new Array("bodywork088.jpg", "825", "550", "Taken the arch back to bare metal, it's not had a repair here before, which is a bonus"),
 new Array("bodywork089.jpg", "825", "550", "However the arch lip is in a pretty awful mess with rust, the plan is to fit half of the arch repair panel, to solve all these messy bits."),
 new Array("bodywork090.jpg", "367", "550", "Passengers sill now sorted"),
 new Array("bodywork091.jpg", "825", "550", "Rear Wiper removed, welded off and skimmed"),
 new Array("bodywork092.jpg", "825", "550", ""),
 new Array("bodywork093.jpg", "367", "550", "Passengers door finally smooth and a lot lighter due to the amount of filler removed"),
 new Array("bodywork094.jpg", "825", "550", ""),
 new Array("bodywork095.jpg", "825", "550", "cut back the arch lip to see what lies beneath...."),
 new Array("bodywork096.jpg", "367", "550", "...hmmm, that would be rust then??!"),
 new Array("bodywork097.jpg", "825", "550", ""),
 new Array("bodywork098.jpg", "367", "550", ""),
 new Array("bodywork099.jpg", "825", "550", "ground back all the rust to bare metal"),
 new Array("bodywork100.jpg", "825", "550", "them krust'd it to catch any bits i might have missed"),
 new Array("bodywork101.jpg", "825", "550", "finally zinc 182 to cover, next is cutting the arch repair to fit, welding then smothing, and also removing a large amount of filler from the rear panel (just behind the passengers door)."),
 new Array("bodywork102.jpg", "825", "550", "wheels the old girl out and removed the drivers door"),
 new Array("bodywork103.jpg", "367", "550", "to find a big hole and an even bigger hole! In the foot well you can see the large chuck of gobo removed"),
 new Array("bodywork104.jpg", "825", "550", ""),
 new Array("bodywork105.jpg", "825", "550", "put my wheels on for a perv"),
 new Array("bodywork106.jpg", "367", "550", ""),
 new Array("bodywork107.jpg", "825", "550", ""),
 new Array("end.jpg", "550", "550", "end of images")
	);

// Number of photos in this gallery
var photoNum = photoArray.length;

/*--------------------------------------------------------------------------*/
// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	getWidth: function(element) {
   	element = $(element);
   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   	element = $(element);
    	element.style.height = h +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

/*--------------------------------------------------------------------------*/

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'Photo';
		this.photoBox = 'Container';
		this.prevLink = 'PrevLink';
		this.nextLink = 'NextLink';
		this.captionBox = 'CaptionContainer';
		this.caption = 'Caption';
		this.counter = 'Counter';
		this.loader = 'Loading';
	},
	getCurrentSize: function() {
		// Get current height and width, subtracting CSS border size
		this.wCur = Element.getWidth(this.photoBox) - borderSize;
		this.hCur = Element.getHeight(this.photoBox) - borderSize;
	},
	getNewSize: function() {
		// Get current height and width
		this.wNew = photoArray[photoId][1];
		this.hNew = photoArray[photoId][2];
	},
	getScaleFactor: function() {
		this.getCurrentSize();
		this.getNewSize();
		// Scalars based on change from old to new
		this.xScale = (this.wNew / this.wCur) * 100;
		this.yScale = (this.hNew / this.hCur) * 100;
	},
	setNewPhotoParams: function() {
		// Set source of new image
		Element.setSrc(this.photo,photoDir + photoArray[photoId][0]);
		// Set anchor for bookmarking
		Element.setHref(this.prevLink, "#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
	},
	setPhotoCaption: function() {
		// Add caption from gallery array
		Element.setInnerHTML(this.caption,photoArray[photoId][3]);
		Element.setInnerHTML(this.counter,((photoId+1)+'/'+photoNum));
	},
	resizePhotoBox: function() {
		this.getScaleFactor();
		new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.3, queue: 'front'});
		new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, delay: 0.5, duration: 0.3});
		// Dynamically resize caption box as well
		Element.setWidth(this.captionBox,this.wNew-(-borderSize));
	},
	showPhoto: function(){
		new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
		// Workaround for problems calling object method "afterFinish"
		new Effect.Appear(this.photo, {duration: 0.5, queue: 'end', afterFinish: function(){Element.show('CaptionContainer');Element.show('PrevLink');Element.show('NextLink');}});
	},
	nextPhoto: function(){
		// Figure out which photo is next
		(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;
		this.initSwap();
	},
	prevPhoto: function(){
		// Figure out which photo is previous
		(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;
		this.initSwap();
	},
	initSwap: function() {
		// Begin by hiding main elements
		Element.show(this.loader);
		Element.hide(this.photo);
		Element.hide(this.captionBox);
		Element.hide(this.prevLink);
		Element.hide(this.nextLink);
		// Set new dimensions and source, then resize
		this.setNewPhotoParams();
		this.resizePhotoBox();
		this.setPhotoCaption();
	}
}

/*--------------------------------------------------------------------------*/

// Establish CSS-driven events via Behaviour script
var myrules = {
	'#Photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.showPhoto();
		}
	},
	'#PrevLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.prevPhoto();
			soundManager.play('select');
		}
	},
	'#NextLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.nextPhoto();
			soundManager.play('select');
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};

// Add window.onload event to initialize
Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	var myPhoto = new Slideshow(photoId);
	myPhoto.initSwap();
	soundManagerInit();
}