// -----------------------------------------------------------------------------------
// 
// 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 = 20;

// Photo directory for this gallery
var photoDir = "../China2004/Foto2004/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
	new Array("Beijing/IMG_0769.JPG", "614", "410", "De brug met de 17 bogen bij het zomerpaleis, in Beijing"),
	new Array("Beijing/IMG_0770.JPG", "614", "410", "De brug met de 17 bogen bij het zomerpaleis, in Beijing"),
	new Array("Xiahe/IMG_1183.JPG", "614", "410", "Brug tussen Tangwangchuan en Xiahe"),
	new Array("Lijiang/IMG_1517.JPG", "410", "614", "Baisui brug, in Lijiang"),
	new Array("Lijiang/IMG_1464.JPG", "614", "410", "Noordelijk van Lijiang in de richting van de Jade Dragon Snow Mountain"),
	new Array("Lijiang/IMG_1513.JPG", "410", "614", "Tussen de Dashi en de Baisui brug, in Lijiang"),
	new Array("Lijiang/IMG_1511.JPG", "614", "410", "Baisui brug in Lijiang"),
	new Array("Lijiang/IMG_1560.JPG", "410", "614", "Lijiang"),
	new Array("Lijiang/IMG_1569.JPG", "614", "410", "Baisui brug in Lijiang"),
	new Array("Lijiang/IMG_1571.JPG", "614", "410", "In Lijiang"),
	new Array("Yangshou/IMG_2070.JPG", "410", "614", "Brugje in de rijstvelden bij Yangshou"),
	new Array("Lijiang/IMG_1600.JPG", "614", "410", "Helongtan, bij Lijiang"),
	new Array("Yangshou/IMG_2078.JPG", "614", "410", "Nieuwe brug in aanbouw bij Yangshou"),
	new Array("Longsheng/IMG_2192.JPG", "614", "410", "Brug bij Dragon's Backbone Rijst Terrassen bij Longsheng"),
	new Array("Longsheng/IMG_2306.JPG", "614", "410", "Hangbrug tussen Longsheng en San Jing"),
	new Array("Longsheng/IMG_2319.JPG", "614", "410", "Chengyang brug bij San Jing"),
	new Array("Longsheng/IMG_2402.JPG", "614", "410", "De eenvoudigste soort brug in San Jing"),
	new Array("Longsheng/IMG_2420.JPG", "614", "410", "Chengyang brug bij San Jing"),
	new Array("Longsheng/IMG_2328.JPG", "614", "410", "Brug bij Sanjiang"),
	new Array("Longsheng/IMG_2433.JPG", "614", "410", "Hangbrug tussen Longsheng en San Jing"),
	new Array("Suzhou/125_2553.JPG", "614", "410", "Souzhou in de binnenstad"),
	new Array("Suzhou/IMG_2451.JPG", "614", "410", "Zhujiajiao"),
	new Array("Suzhou/IMG_2454.JPG", "614", "410", "Zhujiajiao"),
	new Array("Suzhou/125_2559.JPG", "614", "410", "Souzhou Tuin van de Nederige Ambtenaar"),
	new Array("Suzhou/125_2561.JPG", "614", "410", "Souzhou Tuin van de Nederige Ambtenaar"),
	new Array("Suzhou/125_2573.JPG", "614", "410", "Souzhou Tuin van de Nederige Ambtenaar"),
	new Array("Shanghai/125_2586.JPG", "614", "410", "Zigzagbrug bij het theehuis in Shanghai"),
	new Array("Shanghai/127_2798.JPG", "614", "410", "Nanpu brug in Shanghai"),
	new Array("Shanghai/127_2799.JPG", "410", "614", "Nanpu brug in Shanghai"),
	new Array("Lijiang/imm035_36a.JPG", "715", "1075", "Moeders trots, Lijiang"),
	new Array("Lijiang/IMG_1485.JPG", "410", "614", "Moeders trots, Lijiang"),
	new Array("Lijiang/imm019_20.JPG", "410", "614", "Moeders trots, Lijiang"),
	new Array("Lijiang/imm034_35c.JPG", "410", "614", "Moeders trots, Lijiang"),
	new Array("Chengdu/imm003_4.JPG", "410", "614", "Moeders trots, Chengdu"),
	new Array("Lijiang/IMG_1486.JPG", "410", "614", "Moeders trots, Lijiang"),
	new Array("Beijing/imm017_18.JPG", "305", "201", "Moeders trots, Beijing"),
	new Array("Beijing/IMG_0672.JPG", "614", "410", "Blinde muzikanten, Beijing"),
	new Array("Xian/IMG_1167.JPG", "614", "410", "Conversatie met behulp van een eigengemaakt woordenboekje, Xian"),
	new Array("Beijing/imm014_15.JPG", "305", "201", "Slapend langs een van de boulevaars van Beijing"),
	new Array("Beijing/IMG_0799.JPG", "410", "614", "Moeders trots, Beijing"),
	new Array("Beijing/IMG_0544.JPG", "410", "614", "Keizer van China voor 5 minuten"),
	new Array("Beijing/IMG_0851.JPG", "410", "614", "Een oude kamaraad, Beijing"),
	new Array("Xiahe/IMG_1178.JPG", "410", "614", "Moeders trots, Xiahe"),
	new Array("Beijing/IMG_0719.JPG", "410", "614", "Een vriendelijke man op de Muur, Beijing"),
	new Array("Longsheng/IMG_2366.JPG", "410", "614", "Moeders trots, Longshen"),
	new Array("Xian/IMG_1010.JPG", "410", "614", "Tibetaanse danseres in Xian"),
	new Array("Xian/IMG_1121.JPG", "410", "614", "Muzikant bij de stadsmuur van Xian"),
	new Array("Xian/IMG_1122.JPG", "410", "614", "Muzikante bij de stadsmuur van Xian"),
	new Array("Xian/IMG_1165.JPG", "410", "614", "Bedelaar bij poort naar de Moslimwijk in Xian"),
	new Array("Xian/IMG_1027.JPG", "410", "614", "Een van de boeren die het Terracotta Leger ontdekte bij Xian"),
	new Array("Xiahe/IMG_1239.JPG", "410", "614", "Boedistische monnik met moderene technologie bij het Labrang klooster in Xiahe"),
	new Array("Xiahe/IMG_1286.JPG", "410", "614", "Bewoner van een dorpje bij Labrang"),
	new Array("Xiahe/imm012_13.JPG", "410", "614", "Bewoner van een dorpje bij Labrang"),
	new Array("Lijiang/IMG_1514.JPG", "410", "614", "Naxi vrouw in Lijiang"),
	new Array("Xiahe/imm002_3.JPG", "614", "410", "Pelgrims in het klooster van Labrang"),
	new Array("Xiahe/IMG_1288.JPG", "614", "410", "Bewoner van een dorpje bij Labrang"),
	new Array("Xiahe/IMG_1237.JPG", "614", "410", "Pelgrim in het klooster van Labrang"),
	new Array("Lijiang/imm023_24.JPG", "410", "614", "Vrouwen van Lijiang"),
	new Array("Lijiang/imm008_9.JPG", "410", "614", "Vrouwen van Lijiang"),
	new Array("Lijiang/imm019_20c.JPG", "410", "614", "Vrouwen van Lijiang"),
	new Array("Lijiang/imm022_23.JPG", "410", "614", "Pannenkoeken bakker in Lijiang"),
	new Array("Shilin/IMG_1876.JPG", "410", "614", "Toeristen in klederdracht in het Stenen Woud in Shilin"),
	new Array("Shilin/IMG_1878.JPG", "410", "614", "Toeristen in klederdracht in het Stenen Woud in Shilin"),
	new Array("Li Rivier/IMG_1996.JPG", "410", "614", "De aalscholvervisser in Yangshou"),
	new Array("Yangshou/imm002_3.JPG", "410", "614", "Waterdrager op Moon Hill bij Yangshou"),
	new Array("Longsheng/IMG_2382.JPG", "410", "614", "Dong danseres bij de rijst terrassen bij Longsheng"),	
	new Array("Longsheng/IMG_2256.JPG", "410", "614", "Zhuang vrouwen schikken hun haar, Longsheng"),
	new Array("Yangshou/IMG_2072.JPG", "410", "614", "Krom vrouwtje in de rijstvelden van Yangshou"),
	new Array("Yangshou/IMG_2073.JPG", "410", "614", "Krom vrouwtje in de rijstvelden van Yangshou die kaarten verkoopt"),
	new Array("Longsheng/IMG_2356.JPG", "218", "362", "Krom vrouwtje met kleinkind in San Jing"),
	new Array("Suzhou/125_2509.JPG", "410", "614", "Mannequin bij de zijde fabriek in Souzhou"),
	new Array("Shanghai/125_2599.JPG", "410", "614", "Gymnastiek in Shanghai"),
	new Array("Shanghai/127_2766.JPG", "410", "614", "Twee heel eerlijke dames in de Yu-tuin in Shanghai die mijn fototas bewaarde die ik in een paviljoentje had laten staan")
		
	);

// 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();
}