Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1111 jp_milcent 1
/*
2
	Slimbox v1.22 - The ultimate lightweight Lightbox clone
3
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
4
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
5
*/
6
 
7
var Lightbox = {
8
 
9
	init: function(options) {
10
		this.options = Object.extend({
11
			resizeDuration: 400,	// Duration of height and width resizing (ms)
12
			initialWidth: 250,		// Initial width of the box (px)
13
			initialHeight: 250,		// Initial height of the box (px)
14
			animateCaption: true	// Enable/Disable caption animation
15
		}, options || {});
16
 
17
		this.anchors = [];
18
		$A(document.getElementsByTagName('a')).each(function(el){
19
			if(el.rel && el.href && el.rel.test('^lightbox', 'i')) {
20
				el.onclick = this.click.pass(el, this);
21
				this.anchors.push(el);
22
			}
23
		}, this);
24
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
25
		this.eventPosition = this.position.bind(this);
26
 
27
		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
28
 
29
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
30
		this.image = new Element('div').setProperty('id', 'lbImage').injectInside(this.center);
31
		this.prevLink = new Element('a').setProperties({id: 'lbPrevLink', href: '#'}).setStyle('display', 'none').injectInside(this.image);
32
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
33
		this.prevLink.onclick = this.previous.bind(this);
34
		this.nextLink.onclick = this.next.bind(this);
35
 
36
		this.bottom = new Element('div').setProperty('id', 'lbBottom').setStyle('display', 'none').injectInside(document.body);
37
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
38
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
39
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
40
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);
41
 
42
		var nextEffect = this.nextEffect.bind(this);
43
		this.fx = {
44
			overlay: this.overlay.effect('opacity', { duration: 500 }).hide(),
45
			resize: this.center.effects({ duration: this.options.resizeDuration, onComplete: nextEffect }),
46
			image: this.image.effect('opacity', { duration: 500, onComplete: nextEffect }),
47
			bottom: this.bottom.effects({ duration: 400, onComplete: nextEffect })
48
		};
49
 
50
		this.preloadPrev = new Image();
51
		this.preloadNext = new Image();
52
	},
53
 
54
	click: function(link) {
55
		if(link.rel.length == 8)
56
			return this.show(link.href, link.title);
57
 
58
		var j, imageNum, images = [];
59
		this.anchors.each(function(el){
60
			if(el.rel == link.rel) {
61
				for(j = 0; j < images.length; j++)
62
					if(images[j][0] == el.href) break;
63
				if(j == images.length) {
64
					images.push([el.href, el.title]);
65
					if(el.href == link.href) imageNum = j;
66
				}
67
			}
68
		}, this);
69
		return this.open(images, imageNum);
70
	},
71
 
72
	show: function(url, title) {
73
		return this.open([[url, title]], 0);
74
	},
75
 
76
	open: function(images, imageNum) {
77
		this.images = images;
78
		this.position();
79
		this.setup(true);
80
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
81
		this.center.setStyles({top: this.top+'px', display: ''});
82
		this.fx.overlay.goTo(0.8);
83
		return this.changeImage(imageNum);
84
	},
85
 
86
	position: function() {
87
		this.overlay.setStyles({top: Window.getScrollTop()+'px', height: Window.getHeight()+'px'});
88
	},
89
 
90
	setup: function(open) {
91
		var elements = $A(document.getElementsByTagName('object'));
92
		elements.extend(document.getElementsByTagName(window.ActiveXObject ? 'select' : 'embed'));
93
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
94
		var fn = open ? 'addEvent' : 'removeEvent';
95
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
96
		document[fn]('keydown', this.eventKeyDown);
97
		this.step = 0;
98
	},
99
 
100
	keyboardListener: function(event) {
101
		switch(event.keyCode) {
102
			case 27: case 88: case 67: this.close(); break;
103
			case 37: case 80: this.previous(); break;
104
			case 39: case 78: this.next();
105
		}
106
	},
107
 
108
	previous: function() {
109
		return this.changeImage(this.activeImage-1);
110
	},
111
 
112
	next: function() {
113
		return this.changeImage(this.activeImage+1);
114
	},
115
 
116
	changeImage: function(imageNum) {
117
		if(this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
118
		this.step = 1;
119
		this.activeImage = imageNum;
120
 
121
		this.prevLink.style.display = this.nextLink.style.display = 'none';
122
		this.bottom.setStyles({opacity: '0', height: '0px', display: 'none'});
123
		this.fx.image.hide();
124
		this.center.className = 'lbLoading';
125
 
126
		this.preload = new Image();
127
		this.preload.onload = this.nextEffect.bind(this);
128
		this.preload.src = this.images[imageNum][0];
129
		return false;
130
	},
131
 
132
	nextEffect: function() {
133
		switch(this.step++) {
134
		case 1:
135
			this.center.className = '';
136
			this.image.setStyles({backgroundImage: 'url('+this.images[this.activeImage][0]+')', width: this.preload.width+'px'});
137
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';
138
 
139
			this.caption.setHTML(this.images[this.activeImage][1] || '');
140
			this.number.setHTML((this.images.length == 1) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length);
141
 
142
			if(this.activeImage != 0) this.preloadPrev.src = this.images[this.activeImage - 1][0];
143
			if(this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage + 1][0];
144
			if(this.center.clientHeight != this.image.offsetHeight) {
145
				this.fx.resize.custom({height: [this.center.clientHeight, this.image.offsetHeight]});
146
				break;
147
			}
148
			this.step++;
149
		case 2:
150
			if(this.center.clientWidth != this.image.offsetWidth) {
151
				this.fx.resize.custom({width: [this.center.clientWidth, this.image.offsetWidth], marginLeft: [-this.center.clientWidth/2, -this.image.offsetWidth/2]});
152
				break;
153
			}
154
			this.step++;
155
		case 3:
156
			this.bottom.setStyles({top: (this.top + this.center.clientHeight)+'px', width: this.image.style.width, marginLeft: this.center.style.marginLeft, display: ''});
157
			this.fx.image.custom(0, 1);
158
			break;
159
		case 4:
160
			if(this.options.animateCaption) {
161
				this.fx.bottom.custom({opacity: [0, 1], height: [0, this.bottom.scrollHeight]});
162
				break;
163
			}
164
			this.bottom.setStyles({opacity: '1', height: this.bottom.scrollHeight+'px'});
165
		case 5:
166
			if(this.activeImage != 0) this.prevLink.style.display = '';
167
			if(this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
168
			this.step = 0;
169
		}
170
	},
171
 
172
	close: function() {
173
		if(this.step < 0) return;
174
		this.step = -1;
175
		if(this.preload) {
176
			this.preload.onload = Class.empty;
177
			this.preload = null;
178
		}
179
		for(var f in this.fx) this.fx[f].clearTimer();
180
		this.center.style.display = this.bottom.style.display = 'none';
181
		this.fx.overlay.chain(this.setup.pass(false, this)).goTo(0);
182
		return false;
183
	}
184
};
185
 
186
Window.onDomReady(Lightbox.init.bind(Lightbox));