// lightbox_plus.js
// == written by Takuya Otani <takuya.otani@gmail.com> ===
// == Copyright (C) 2004 SimpleBoxes/SerendipityNZ Ltd. ==
/*
	Original script : Lightbox JS : Fullsize Image Overlays
	Copyright (C) 2005 Lokesh Dhakar - http://www.huddletogether.com
	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/
*/
// ver. 20060120 - added caption and close button.
// === utilities ===


function addEvent(object, type, handler)
{
	if (object.addEventListener) {
		object.addEventListener(type, handler, false);
	} else if (object.attachEvent) {
		object.attachEvent(['on',type].join(''),handler);
	} else {
		object[['on',type].join('')] = handler;
	}
}
function WindowSize()
{ // window size object
	this.w = 0;
	this.h = 0;
	return this.update();
}
WindowSize.prototype.update = function()
{
	var d = document;
	this.w = 
	  (window.innerWidth) ? window.innerWidth
	: (d.documentElement && d.documentElement.clientWidth) ? d.documentElement.clientWidth
	: d.body.clientWidth;
	this.h = 
	  (window.innerHeight) ? window.innerHeight
	: (d.documentElement && d.documentElement.clientHeight) ? d.documentElement.clientHeight
	: d.body.clientHeight;
	return this;
};
function PageSize()
{ // page size object
	this.win = new WindowSize();
	this.w = 0;
	this.h = 0;
	return this.update();
}
PageSize.prototype.update = function()
{
	var d = document;
	this.w = 
	  (window.innerWidth && window.scrollMaxX) ? window.innerWidth + window.scrollMaxX
	: (d.body.scrollWidth > d.body.offsetWidth) ? d.body.scrollWidth
	: d.body.offsetWidt;
	this.h = 
	  (window.innerHeight && window.scrollMaxY) ? window.innerHeight + window.scrollMaxY
	: (d.body.scrollHeight > d.body.offsetHeight) ? d.body.scrollHeight
	: d.body.offsetHeight;
	this.win.update();
	if (this.w < this.win.w) this.w = this.win.w;
	if (this.h < this.win.h) this.h = this.win.h;
	return this;
};
function PagePos()
{ // page position object
	this.x = this.y = 0;
	return this.update();
}
PagePos.prototype.update = function()
{
	var d = document;
	this.x =
	  (window.pageXOffset) ? window.pageXOffset
	: (d.documentElement && d.documentElement.scrollLeft) ? d.documentElement.scrollLeft
	: (d.body) ? d.body.scrollLeft
	: 0;
	this.y =
	  (window.pageYOffset) ? window.pageYOffset
	: (d.documentElement && d.documentElement.scrollTop) ? d.documentElement.scrollTop
	: (d.body) ? d.body.scrollTop
	: 0;
	return this;
};
function UserAgent()
{ // user agent information
	var ua = navigator.userAgent;
	this.isWinIE = this.isMacIE = false;
	this.isGecko  = ua.match(/Gecko\//);
	this.isSafari = ua.match(/AppleWebKit/);
	this.isOpera  = window.opera;
	if (document.all && !this.isGecko && !this.isSafari && !this.isOpera) {
		this.isWinIE = ua.match(/Win/);
		this.isMacIE = ua.match(/Mac/);
		this.isNewIE = (ua.match(/MSIE 5\.5/) || ua.match(/MSIE 6\.0/));
	}
	return this;
}
// === lightbox ===
function LightBox(option,isFrame)
{
	var self = this;
	self._imgs = new Array();
	self._flash = new Array();
	self._wrap = null;
	self._box  = null;
	self._open = -1;
	self._page = new PageSize();
	self._pos  = new PagePos();
	self._ua   = new UserAgent();
	self._expandable = false;
	self._expanded = false;
	self._expand = option.expandimg;
	self._shrink = option.shrinkimg;
	self._isInFrame = isFrame;
	return self;
}

LightBox.prototype = {
	_init : function(option)
	{
		//Ligne 124
		var self = this;
		if (self._isInFrame) var d = frames['main'].document;	
		else var d = document;
		if (!d.getElementsByTagName) return;
		var links = d.getElementsByTagName("img");
		for (var i=0;i<links.length;i++) {
			var img = links[i];
			var num = self._imgs.length;
			if (img.className!="zoomImg") continue;
			self._imgs[num] = {src:img.src,w:-1,h:-1,title:''};
			if (img.getAttribute("alt"))
				self._imgs[num].title = img.getAttribute("alt");
			self._set_cursor(img);
			img.onclick = self._genOpener(num);
		}
		if (!self._ua.isWinIE) self._flash = document.getElementsByTagName("embed");
		else self._flash = document.getElementsByTagName("object");
		var body = document.getElementsByTagName("body")[0];
		self._wrap = self._createWrapOn(body,option.loadingimg);
		self._box  = self._createBoxOn(body,option);
		return self;
	},
	_addImg : function (url,titre){
		//Rajout
		var self = this;
		self._imgs[self._imgs.length] = {src:url,w:-1,h:-1,title:titre};
		self._show(self._imgs.length-1);
	},
	_genOpener : function(num)
	{
		var self = this;
		return function() { self._show(num); return false; }
	},
	_createWrapOn : function(obj,imagePath)
	{
		var self = this;
		if (!obj) return null;
		// create wrapper object, translucent background
		var wrap = document.createElement('div');
		wrap.id = 'overlay';
		with (wrap.style) {
			display = 'none';
			position = 'fixed';
			top = '0px';
			left = '0px';
			zIndex = '50';
			width = '100%';
			height = '100%';
		}
		if (self._ua.isWinIE) wrap.style.position = 'absolute';
		addEvent(wrap,"click",function() { self._close(); });
		obj.appendChild(wrap);
		// create loading image, animated image
		var imag = new Image;
		imag.onload = function() {
			var spin = document.createElement('img');
			spin.id = 'loadingImage';
			spin.src = imag.src;
			spin.style.position = 'relative';
			self._set_cursor(spin);
			addEvent(spin,'click',function() { self._close(); });
			wrap.appendChild(spin);
			imag.onload = function(){};
		};
		if (imagePath != '') imag.src = imagePath;
		return wrap;
	},
	_createBoxOn : function(obj,option)
	{

		var self = this;
		if (!obj) return null;
		// create lightbox object, frame rectangle
		var box = document.createElement('div');
		box.id = 'lightbox';
		with (box.style) {
			display = 'none';
			position = 'absolute';
			zIndex = '60';
		}
		obj.appendChild(box);
		// create image object to display a target image
		var img = document.createElement('img');
		img.id = 'lightboxImage';
		self._set_cursor(img);
		addEvent(img,'click',function(){ self._close(); });
		box.appendChild(img);
		addEvent(window,'resize',function(){ self._set_size(true); });
		if (option.closeimg) {
			var btn = document.createElement('a');
			btn.id = 'closeButton';
			btn.innerHTML = "X";
			with (btn.style) {
				display = 'none';
				position = 'absolute';
				right = '3px';
				top = '3px';
				zIndex = '90';
				color = 'white';
			}
			btn.src = option.closeimg;
			self._set_cursor(btn);
			addEvent(btn,'click',function(){ self._close(); });
			box.appendChild(btn);
		}
		return box;
	},
	_set_photo_size : function()
	{
		var self = this;
		if (self._open == -1) return;
		var imag = self._box.firstChild;
		var targ = { w:self._page.win.w - 30, h:self._page.win.h - 30 };
		var orig = { w:self._imgs[self._open].w, h:self._imgs[self._open].h };
		// shrink image with the same aspect
		var ratio = 1.0;
		if ((orig.w >= targ.w || orig.h >= targ.h) && orig.h && orig.w)
			ratio = ((targ.w / orig.w) < (targ.h / orig.h)) ? targ.w / orig.w : targ.h / orig.h;
		imag.width  = Math.floor(orig.w * ratio);
		imag.height = Math.floor(orig.h * ratio);
		//imag.style.paddingTop = "25px";
		self._expandable = (ratio < 1.0) ? true : false;
		if (self._ua.isWinIE) self._box.style.display = "block";
		self._box.style.top  = [self._pos.y + (self._page.win.h - imag.height - 30) / 2,'px'].join('');
		self._box.style.left = [((self._page.win.w - imag.width - 30) / 2),'px'].join('');
		//self._show_caption(true);
	},
	_set_size : function(onResize)
	{
		var self = this;
		if (self._open == -1) return;
		self._page.update();
		self._pos.update();
		var spin = self._wrap.firstChild;
		if (spin) {
			var top = (self._page.win.h - spin.height) / 2;
			if (self._wrap.style.position == 'absolute') top += self._pos.y;
			spin.style.top  = [top,'px'].join('');
			spin.style.left = [(self._page.win.w - spin.width - 30) / 2,'px'].join('');
		}
		if (self._ua.isWinIE) {
			self._wrap.style.width  = [self._page.win.w,'px'].join('');
			self._wrap.style.height = [self._page.h,'px'].join('');
		}
		if (onResize) self._set_photo_size();
	},
	_show_caption : function(enable)
	{
		var self = this;
		var imag = self._box.firstChild;
		var caption = document.createElement('div');
		caption.id = 'lightboxCaption';
		with (caption.style) {
			display = 'block';
			width = [imag.width + 5,'px'].join('');
			position = 'absolute';
			zIndex = '80';
			top = '1px';
			left = '1px';
			height = '1.2em';
		}
		caption.innerHTML = "<span style='position:relative;top:1px;'>"+self._imgs[self._open].title+"</span>";
		self._box.appendChild(caption);
	},
	_show : function(num)
	{
		var d = document;
		for (var i=0;i<this._flash.length;i++) 
		{
			this._flash[i].style.visibility="hidden";
		}
		var self = this;
		var imag = new Image;
		if (num < 0 || num >= self._imgs.length) return;
		var loading = document.getElementById('loadingImage');
		self._open = num; // set opened image number
		self._set_size(false); // calc and set wrapper size
		self._wrap.style.display = "block";
		if (loading) loading.style.display = 'inline';
		imag.onload = function() {
			if (self._imgs[self._open].w == -1) {
				// store original image width and height
				self._imgs[self._open].w = imag.width;
				self._imgs[self._open].h = imag.height;
			}
			self._set_photo_size(); // calc and set lightbox size
			self._box.style.display = "block";
			self._box.firstChild.src = imag.src;
			self._box.firstChild.setAttribute('title',self._imgs[self._open].title);
			if (loading) loading.style.display = 'none';
		};
		self._expandable = false;
		self._expanded = false;
		imag.src = self._imgs[self._open].src;
	},
	_set_cursor : function(obj)
	{
		var self = this;
		//if (self._ua.isWinIE && !self._ua.isNewIE) return;
		obj.style.cursor = 'pointer';
	},
	_close : function()
	{
		for (var i=0;i<this._flash.length;i++) 
		{
			this._flash[i].style.visibility="visible";
		}
		var self = this;
		self._open = -1;
		self._wrap.style.display = "none";
		self._box.style.display  = "none";
	}
};