4 This is a part of mkgallery.pl suite
5 http://www.average.org/mkgallery/
7 Uses mootools (1.2) http://www.mootools.net/
8 Inspired by slideshow http://www.phatfusion.net/slideshow/
14 - On show image: find this and next urls; put in place
15 those that are already here; free unneeded; initiate download of
16 the rest; if needed image is ready then initiate "transitioning", else
18 - On load complete: if this is the target image, initiate "transitioning".
19 - On "loading": show "loading" image
20 - On "transitioning": hide "loading" image; initiate FX animation to the
22 - On animation complete: blank previous image; if "playing" then schedule
23 autoswitch to next image in the future.
24 - On autoswitch to next image: if "playing" then switch to next image.
25 - On switch to next image: if next exists, show next image, else show
27 - On switch to prev image: if prev exists, show prev image, else show
28 "first image" message.
29 - On "play": make "playing"; switch to next image.
30 - On "stop": if "playing" cancel autoswitch; break "playing".
31 - On "start show": set up things; set "playing" state; show needed image.
32 - On "stop show": cancel any schedules, hide things.
33 - On resize: recalculate existing image size and position; determine
34 what image is needed; if it is not the one on display then request
35 "show image" for the new image.
38 var Show = new Class({
40 getOptions: function(){
42 cbStart: function(){ alert('show start undefined'); },
43 cbExit: function(){ alert('show exit undefined'); },
50 initialize: function(vimgs, container, controls, options){
51 this.setOptions(this.getOptions(), options);
53 this.container = container;
54 this.controls = controls;
55 this.controls.registershow(this);
57 this.delay = this.options.delay;
64 this.prevdisplay = new Element('img').
65 setStyle('opacity', 0).
66 injectInside(this.container.container);
67 this.ondisplay = new Element('img').
68 setStyle('opacity', 0).
69 injectInside(this.container.container);
70 this.loadingdiv = new Element('div').
71 addClass('loading').setStyles({
77 width: this.coords.width,
78 height: this.coords.height,
79 }).injectInside(this.container.container);
81 window.addEvent('resize', this.resizer.bind(this))
84 /* event handler for window resize */
88 var newstyle = this.calcsize(this.cache.curr);
89 this.ondisplay.setStyles(newstyle);
90 /* check if we need reload */
93 /* prev, play, stop, next, exit, comm are methods for button presses */
96 if (this.currentid > 0) {
97 this.show(this.currentid-1);
99 alert('show.prev called beyond first element');
104 if (this.isplaying) { $clear(this.timer); }
105 this.isplaying = false;
107 this.controls.running(0);
111 this.isplaying = true;
112 this.timer = this.autonext.delay(this.delay,this);
113 this.controls.running(1);
117 if (this.currentid < this.vimgs.length-1) {
118 this.show(this.currentid+1);
120 alert('show.next called beyond last element');
125 if (this.isplaying) { $clear(this.timer); }
126 this.prevdisplay.setStyle('display', 'none');
127 this.ondisplay.setStyle('display', 'none');
129 this.options.cbExit();
133 /* alert('show.comm called, do nothing'); */
136 /* Entry point: called to start doing things */
138 start: function(id, play){
139 this.options.cbStart();
140 this.isplaying = play;
141 this.controls.running(this.isplaying);
144 return false; /* to make it usable from href links */
147 /* "Private" methods to do the real job */
150 /* alert('called show.show('+id+')'); */
153 prev: (id > 0)?this.prepare(id-1):{},
154 curr: this.prepare(id),
155 next: (id < (this.vimgs.length-1))?this.prepare(id+1):{},
158 this.cache = newcache;
159 if (this.cache.curr.ready) {
160 this.display(this.cache.curr);
162 this.pendingload = true;
165 this.controls.info(id,this.vimgs.length,
170 prepare: function(id){
172 for (vi=0;vi<this.vimgs[id][2].length-1;vi++) {
173 if ((this.vimgs[id][2][vi][0] >= this.target.width) ||
174 (this.vimgs[id][2][vi][1] >= this.target.height)) {
178 /* alert('prepare id='+id+', selected '+vi+' at '+
179 this.vimgs[id][2][vi][0]+'x'+
180 this.vimgs[id][2][vi][1]); */
182 ['prev', 'curr', 'next'].each(function(el){
183 if (this.cache[el] &&
184 this.cache[el].id == id &&
185 this.cache[el].vi == vi) {
186 cachel = this.cache[el];
194 url: this.vimgs[id][2][vi][2],
196 cachel.img = this.bgload(cachel);
201 bgload: function(cachel){
202 /* alert('bgload: id='+cachel.id+' vi='+cachel.vi+
203 ' url='+cachel.url); */
204 return new Asset.image(this.vimgs[cachel.id][2][cachel.vi][2],{
205 id: this.vimgs[cachel.id][0],
206 title: this.vimgs[cachel.id][1],
207 onload: this.loadcomplete.bind(this,[cachel]),
211 loadcomplete: function(cachel){
212 /* alert('loadcomplete '+cachel.url+' id='+cachel.id+
213 ' vi='+cachel.vi); */
215 if (cachel.id == this.currentid &&
217 this.pendingload = false;
219 this.display(cachel);
223 display: function(cachel){
224 var newstyle = this.calcsize(cachel);
225 var newimg = cachel.img.clone();
226 newimg.setStyles(newstyle);
231 this.prevdisplay.dispose();
232 this.prevdisplay = this.ondisplay.clone().
233 setStyle('zIndex', 2).injectInside(this.container.container);
234 newimg.replaces(this.ondisplay);
235 this.ondisplay = newimg;
240 this.fx = new Fx.Tween(this.ondisplay, {
241 duration: this.options.fxduration,
243 this.fx.addEvent('complete',this.displaycomplete.bind(this));
244 this.fx.start('opacity', 0, 1);
247 displaycomplete: function(){
248 this.prevdisplay.setStyle('display', 'none');
249 if (this.isplaying) {
250 this.timer = this.autonext.delay(this.delay,this);
254 autonext: function(){
255 if (this.isplaying) {
256 if (this.currentid < this.vimgs.length-1) {
257 this.show(this.currentid+1);
264 calcsize: function(cachel){
267 candidate = this.target.width /
268 this.vimgs[cachel.id][2][cachel.vi][0];
269 if (factor > candidate) { factor = candidate; }
270 candidate = this.target.height /
271 this.vimgs[cachel.id][2][cachel.vi][1];
272 if (factor > candidate) { factor = candidate; }
273 var w = Math.round(this.vimgs[cachel.id][2][cachel.vi][0] *
275 var h = Math.round(this.vimgs[cachel.id][2][cachel.vi][1] *
277 var t = Math.round((this.coords.height-h)/2);
278 var l = Math.round((this.coords.width-w)/2);
279 /* alert('new size: '+w+'x'+h+'+'+l+'+'+t); */
281 position: 'absolute',
289 showloading: function(){
290 this.loadingdiv.setStyle('display', 'block');
293 hideloading: function(){
294 this.loadingdiv.setStyle('display', 'none');
298 if (this.fx) this.fx.cancel();
301 updatecoords: function(){
302 this.coords = this.container.getCoordinates();
304 width: Math.round(this.coords.width *
305 this.options.percentage / 100),
306 height: Math.round(this.coords.height *
307 this.options.percentage / 100),
309 /* alert('coords: '+this.coords.width+'x'+this.coords.height+
310 ', target: '+this.target.width+'x'+this.target.height); */
314 Show.implement(new Options);
315 Show.implement(new Events);