// 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("bodyparts001.jpg", "413", "550", "Headlight Surrounds and Grill"),
 new Array("bodyparts002.jpg", "733", "550", "Prep work for painting"),
 new Array("bodyparts003.jpg", "733", "550", ""),
 new Array("bodyparts004.jpg", "413", "550", ""),
 new Array("bodyparts005.jpg", "733", "550", ""),
 new Array("bodyparts006.jpg", "733", "550", "Top coat of Primer before sanding down"),
 new Array("bodyparts007.jpg", "413", "550", ""),
 new Array("bodyparts008.jpg", "733", "550", ""),
 new Array("bodyparts009.jpg", "733", "550", "Matt Black top Coat"),
 new Array("bodyparts010.jpg", "733", "550", ""),
 new Array("bodyparts011.jpg", "688", "550", "New Badge sorted - Strictly speaking this is the wrong badge for the year, they changed the colour of the Ford blue"),
 new Array("bodyparts012.jpg", "688", "550", "Who going to noticed??"),
 new Array("bodyparts013.jpg", "688", "550", "Rear Bumper brackets in a coat of Hammerite"),
 new Array("bodyparts014.jpg", "688", "550", "Project Clear Lenses - &pound;145 for a set of new clear lenses - I had a plan!! It involved this random piece of plastic and a dremel..."),
 new Array("bodyparts015.jpg", "688", "550", "Old 1980's style big orange flashers"),
 new Array("bodyparts016.jpg", "688", "550", "Trial fit of plastic, the plastic is from a strip light cover, sourced from a de-commissioned mine of all places!"),
 new Array("bodyparts017.jpg", "688", "550", "Removed the Orange lens"),
 new Array("bodyparts018.jpg", "688", "550", ""),
 new Array("bodyparts019.jpg", "688", "550", "Fit the new lens"),
 new Array("bodyparts020.jpg", "688", "550", "Trail fit in bumper"),
 new Array("bodyparts021.jpg", "688", "550", "White light, need orange bulbs to complete job"),
 new Array("bodyparts022.jpg", "688", "550", "Orange Bulbs"),
 new Array("bodyparts023.jpg", "733", "550", "Smoothed all the plastic bumpers to take paint better"),
 new Array("bodyparts024.jpg", "733", "550", "Plastic primer"),
 new Array("bodyparts025.jpg", "413", "550", "Removed headlight brackets for a clean up"),
 new Array("bodyparts026.jpg", "733", "550", ""),
 new Array("bodyparts027.jpg", "413", "550", ""),
 new Array("bodyparts028.jpg", "413", "550", ""),
 new Array("bodyparts029.jpg", "733", "550", ""),
 new Array("bodyparts030.jpg", "733", "550", "Healight rings"),
 new Array("bodyparts031.jpg", "733", "550", ""),
 new Array("bodyparts032.jpg", "413", "550", ""),
 new Array("bodyparts033.jpg", "733", "550", "Rear lights removed, picture for reference"),
 new Array("bodyparts034.jpg", "688", "550", "This is the bonnet catch, was extremely rusty and I had to grind it off the bodywork, this is is fully stripped and painted - as ever, I forgot to take any before pictures!"),
 new Array("bodyparts035.jpg", "688", "550", "Assembled and painted"),
 new Array("bodyparts036.jpg", "733", "550", "New passengers wing mirror"),
 new Array("bodyparts037.jpg", "733", "550", "This is the part I wanted, also known as rocking horse shit"),
 new Array("bodyparts038.jpg", "825", "550", "Header tank and washer bottle are now super clean"),
 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();
}