var photoDir="";
var borderSize=0;

// Récupération du numéro de photo dans l'url
//var thisURL = document.location.href;
//var splitURL = thisURL.split("#");
//var photoId = splitURL[1] - 1;
//var photoId = (!photoId)? 0 : photoId;
var PopupAlreadyCreate=false;
//var Slideshow = Class.create();

function InitPopup()
{
	
		photoId=(($(IdPosition).innerHTML)-1);		
		Event.observe('Photo', 'load', function(){ myPhoto = new Slideshow(photoId); myPhoto.showPhoto(); } );
		// Supprime la premiere ligne du tableau (Car vide)
		var t=0; var TabPhoto=new Array();
		TabSrcPhotoMaxi.each(function(s) { if(s[0]!="") { TabPhoto[t]=s; t++; } });
		// Number of photos in this gallery
		var photoNum = TabPhoto.length;
		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.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 = TabPhoto[photoId][1];
			this.hNew = TabPhoto[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 + TabPhoto[photoId][0]);
			// Set anchor for bookmarking
			Element.setHref(this.prevLink, "#" + (photoId+1));
			Element.setHref(this.nextLink, "#" + (photoId+1));
		},
		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});
		},
		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'});
		},
		nextPhoto: function(){
			// Figure out which photo is next
			(photoId == (TabPhoto.length - 1)) ? photoId = 0 : photoId++;
			this.initSwap();
		},
		prevPhoto: function(){
			// Figure out which photo is previous
			(photoId == 0) ? photoId = TabPhoto.length - 1 : photoId--;
			this.initSwap();
		},
		showCurrentPhoto: function(){
			photoId = parseInt($(IdPosition).innerHTML)-1;
			this.initSwap();
		},
		initSwap: function() {
			// Begin by hiding main elements
			Element.show(this.photoBox);
			Element.show(this.loader);
			Element.hide(this.photo);
			// Set new dimensions and source, then resize
			this.setNewPhotoParams();
			this.resizePhotoBox();
		}
	}

		myPhoto = new Slideshow(photoId);
		myPhoto.initSwap();
		Event.observe('overlay', 'click', function(){ Element.hide("overlay"); Element.hide("Container"); } );
		Event.observe('fermer', 'click', function(){ Element.hide("overlay"); Element.hide("Container"); } );
		Event.observe('PrevLink', 'focus', function(){ this.blur(); } );
		Event.observe('NextLink', 'focus', function(){ this.blur(); } );
		Event.observe('PrevLink', 'click', function(){ myPhoto = new Slideshow(photoId); myPhoto.prevPhoto(); } );
		Event.observe('NextLink', 'click', function(){ myPhoto = new Slideshow(photoId); myPhoto.nextPhoto(); } );
		Event.observe(IdGrandePhoto, 'click', function(){ myPhoto = new Slideshow(photoId); myPhoto.showCurrentPhoto(); } );
		
		PopupAlreadyCreate=true;
}

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