// 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 = "galerie-expo/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
	
	new Array("autres/foto6.jpg", "800", "414", "Near death experience... (2011)"),
	new Array("autres/foto2.jpg", "441", "600", "Debout contre le sol (2011)"),
	new Array("autres/foto3.jpg", "750", "550", "Intrigués (2011)"),
	new Array("tenerife/foto10.jpg", "804", "550", "Sur le lit (2011)"),
	new Array("autres/foto4.jpg", "832", "550", "Plein de bougies (2011)"),
    new Array("tenerife/foto17.jpg", "722", "550", "Prés du piano bar (2011)"),
    new Array("tenerife/foto18.jpg", "428", "600", "Aprés le piano bar (2011)"),
    new Array("tenerife/foto36.jpg", "370", "600", "Wok de nouilles (2011)"),
    new Array("tenerife/foto38.jpg", "813", "550", "Reina Sofia Airport I (2011)"),
    new Array("tenerife/foto7.jpg", "646", "550", "L'attente (2011)"),
    new Array("tenerife/foto6.jpg", "450", "600", "Oubliés... (2011)"),
    new Array("tenerife/foto8.jpg", "408", "600", "Cuir féminin II (2011)"),
    new Array("tenerife/foto5.jpg", "494", "600", "Towards the sun (2011)"),
    new Array("tenerife/foto13.jpg", "531", "600", "Zigzag airport (2011)"),
    new Array("tenerife/foto11.jpg", "733", "550", "Pas à pas (2011)"),
    new Array("tenerife/foto37.jpg", "719", "550", "Reina Sofia Airport II (2011)"),
    new Array("tenerife/foto12.jpg", "718", "550", "Plus un pas (2011)"),
    new Array("tenerife/foto2.jpg", "445", "600", "Coulée de béton (2011)"),
    new Array("tenerife/foto23.jpg", "733", "550", "Rouillées (2011)"),
    new Array("tenerife/foto24.jpg", "370", "600", "Cabine téléphonique à Tenerife (2011)"),
	new Array("tenerife/foto1.jpg", "450", "600", "Démangeaison auriculaire (2011)"),
	new Array("autres/foto1.jpg", "906", "300", "Bertille la myrtille (2011)"),
	new Array("tenerife/foto15.jpg", "447", "600", "Blue sport (2011)"),
	new Array("tenerife/foto19.jpg", "733", "550", "Tout est complétement emmêlé (2011)"),
    new Array("tenerife/foto20.jpg", "450", "600", "Pensif sous le soleil de Tenerife (par Miss Fravez // 2011)"),
	new Array("tenerife/foto22.jpg", "441", "600", "Kinder surprise (2011)"),
    new Array("tenerife/foto25.jpg", "733", "550", "Barbed wire Paradise (2011)"),
    new Array("tenerife/foto26.jpg", "454", "600", "Cactus au balcon (2011)"),
    new Array("tenerife/foto27.jpg", "426", "600", "Parking alone (2011)"),
    new Array("tenerife/foto28.jpg", "438", "600", "The bay view (2011)"),
    new Array("tenerife/foto32.jpg", "695", "550", "Fresque sur le port de Los Cristianos (2011)"),
    new Array("tenerife/foto29.jpg", "446", "600", "Caddy (2011)"),
    new Array("tenerife/foto30.jpg", "450", "600", "Longue vue sur l'océan (2011)"),
    new Array("tenerife/foto31.jpg", "445", "600", "Rond point à Los Cristianos (2011)"),
	new Array("autres/foto5.jpg", "450", "600", "Longue vue sur un rond point (2011)"),
    new Array("tenerife/foto35.jpg", "723", "550", "Tu t'habilles comment aujourd'hui ? (2011)"),
    new Array("tenerife/foto33.jpg", "441", "600", "Port de Los Cristianos (2011)"),
    new Array("tenerife/foto34.jpg", "433", "600", "Pompe à essence pour bateaux (2011)")
	);

// 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();
}
