// -----------------------------------------------------------------------------------
// 
// This page coded by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// This work is licensed under a Creative Commons License
// Attribution-ShareAlike 2.0
// http://creativecommons.org/licenses/by-sa/2.0/
//
// Associated APIs copyright their respective owners
//
// -----------------------------------------------------------------------------------
// --- version date: 11/28/05 --------------------------------------------------------


// 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 = "photos/04/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
	new Array("101.jpg", "500", "375", "Happy Birthday Charlotte. (Photo: Kate)"),
	new Array("102.jpg", "500", "375", "Britain is no longer a safe place to catch a bus. One must engage in fierce staring to prevent harrassment. (Photo: Kate)"),
	new Array("103.jpg", "500", "375", "And you should look behind you as well. (Photo: Kate)"),
	new Array("104.jpg", "500", "375", "Chelmsford station. (Photo: Kate)"),
	new Array("001.jpg", "500", "332", "First time on a train."),
	new Array("002.jpg", "500", "332", "First time on a train."),
	new Array("105.jpg", "500", "375", "Liverpool St. Station - no Rick Astley fans in sight. (Photo: Kate)"),
	new Array("003.jpg", "500", "332", "Crazy American tourist."),
	new Array("004.jpg", "500", "332", "Silly English tourist"),
	new Array("005.jpg", "332", "500", "St Paul's looms out of the mist."),
	new Array("006.jpg", "332", "500", "England Expects Every Man To Know Who This Is"),
	new Array("007.jpg", "500", "332", "Big Ben. You'll see this again soon."),
	new Array("008.jpg", "500", "332", "Caroline and Daddy at the palace."),
	new Array("009.jpg", "500", "332", "Caroline"),
	new Array("010.jpg", "500", "332", "Charlotte"),
	new Array("011.jpg", "500", "332", "Beatrice and Eugenie?"),
	new Array("106.jpg", "500", "375", "At the palace. (Photo: Kate)"),
	new Array("012.jpg", "500", "332", "Grandma and Grandad at the palace."),
	new Array("013.jpg", "332", "500", "Taking in the scene."),
	new Array("014.jpg", "500", "332", "Daddy and Grandad plan the next phase of the precision operation."),
	new Array("015.jpg", "332", "500", "Just as Grandad planned, the horse guards mark our visit."),
	new Array("107.jpg", "500", "375", "On the district line. (Photo: Kate)"),
	new Array("016.jpg", "332", "500", "We arrived underneath Big Ben right at 1pm, in time to hear the chime."),
	new Array("017.jpg", "500", "332", "Everyone say Big Ben."),
	new Array("018.jpg", "500", "332", "Next stop, the Eye."),
	new Array("108.jpg", "500", "375", "On Westminster bridge. (Photo: Kate)"),
	new Array("019.jpg", "500", "332", "Charing Cross station and Whitehall."),
	new Array("020.jpg", "332", "500", "Most Londoners look like ordinary people, but not all of them."),
	new Array("021.jpg", "500", "332", "Waiting for the Eye."),
	new Array("022.jpg", "332", "500", "Proof that Mummy came too."),
	new Array("023.jpg", "332", "500", "Nice weather for it."),
	new Array("024.jpg", "500", "332", "Uncle Tim arrives."),
	new Array("025.jpg", "500", "332", "Uncle Tim is a natural with children."),
	new Array("026.jpg", "500", "332", "Charlotte spots something in the distance."),
	new Array("027.jpg", "332", "500", "Caroline at about 300 feet high."),
	new Array("028.jpg", "500", "332", "The altitude begins to get to Caroline."),
	new Array("029.jpg", "500", "332", "Hungerford bridge and the Golden Jubilee Bridges."),
	new Array("109.jpg", "500", "375", "Charlotte had the best view. (Photo: Kate)"),
	new Array("030.jpg", "332", "500", "Uncle Tim and Charlotte discuss optics."),
	new Array("031.jpg", "500", "332", "Grandma and Grandad enjoy the flight too."),
	new Array("032.jpg", "332", "500", "Look familiar yet?"),
	new Array("033.jpg", "500", "332", "Spotting the sites."),
	new Array("034.jpg", "500", "332", "Trying to point out St Paul's"),
	new Array("035.jpg", "500", "332", "Caroline at 450 feet high."),
	new Array("036.jpg", "500", "332", "The real site to spot was the ice cream van, 50 feet from the queue to get on."),
	new Array("037.jpg", "500", "332", "Off down the river to the tower. Notice the scaffolding, they're having a sun room put in."),
	new Array("039.jpg", "500", "332", "Stirring patriotic shot for Daddy's benefit."),
	new Array("110.jpg", "500", "375", "Uncle Tim, you clown. (Photo: Kate)"),
	new Array("040.jpg", "500", "332", "Charlotte and her three cakes."),
	new Array("041.jpg", "500", "332", "Happy Third Birthday Charlotte."),
	new Array("042.jpg", "332", "500", "Princess for a day."),
	new Array("043.jpg", "500", "332", "The brothers S."),
	new Array("044.jpg", "500", "332", "Those crazy guys!")
	);

// 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();
}
