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 injectInside(this.container.container);
66 this.ondisplay = new Element('img').
67 injectInside(this.container.container);
68 this.loadingdiv = new Element('div').
69 addClass('loading').setStyles({
75 width: this.coords.width,
76 height: this.coords.height,
77 }).injectInside(this.container.container);
79 window.addEvent('resize', this.resizer.bind(this))
82 /* event handler for window resize */
86 var newstyle = this.calcsize(this.cache.curr);
87 this.ondisplay.setStyles(newstyle);
88 /* check if we need reload */
91 /* prev, play, stop, next, exit, comm are methods for button presses */
94 if (this.currentid > 0) {
95 this.show(this.currentid-1);
97 alert('show.prev called beyond first element');
102 if (this.isplaying) { $clear(this.timer); }
103 this.isplaying = false;
105 this.controls.running(0);
109 this.isplaying = true;
110 this.timer = this.autonext.delay(this.delay,this);
111 this.controls.running(1);
115 if (this.currentid < this.vimgs.length-1) {
116 this.show(this.currentid+1);
118 alert('show.next called beyond last element');
123 if (this.isplaying) { $clear(this.timer); }
124 this.prevdisplay.setStyle('display', 'none');
125 this.ondisplay.setStyle('display', 'none');
127 this.options.cbExit();
131 /* alert('show.comm called, do nothing'); */
134 /* Entry point: called to start doing things */
136 start: function(id, play){
137 this.options.cbStart();
138 this.isplaying = play;
139 this.controls.running(this.isplaying);
141 return false; /* to make it usable from href links */
144 /* "Private" methods to do the real job */
147 /* alert('called show.show('+id+')'); */
150 prev: (id > 0)?this.prepare(id-1):{},
151 curr: this.prepare(id),
152 next: (id < (this.vimgs.length-1))?this.prepare(id+1):{},
155 this.cache = newcache;
156 if (this.cache.curr.ready) {
157 this.display(this.cache.curr);
159 this.pendingload = true;
162 this.controls.info(id,this.vimgs.length,
167 prepare: function(id){
169 for (vi=0;vi<this.vimgs[id][2].length-1;vi++) {
170 if ((this.vimgs[id][2][vi][0] >= this.target.width) ||
171 (this.vimgs[id][2][vi][1] >= this.target.height)) {
175 /* alert('prepare id='+id+', selected '+vi+' at '+
176 this.vimgs[id][2][vi][0]+'x'+
177 this.vimgs[id][2][vi][1]); */
179 ['prev', 'curr', 'next'].each(function(el){
180 if (this.cache[el] &&
181 this.cache[el].id == id &&
182 this.cache[el].vi == vi) {
183 cachel = this.cache[el];
191 url: this.vimgs[id][2][vi][2],
193 cachel.img = this.bgload(cachel);
198 bgload: function(cachel){
199 /* alert('bgload: id='+cachel.id+' vi='+cachel.vi+
200 ' url='+cachel.url); */
201 return new Asset.image(this.vimgs[cachel.id][2][cachel.vi][2],{
202 id: this.vimgs[cachel.id][0],
203 title: this.vimgs[cachel.id][1],
204 onload: this.loadcomplete.bind(this,[cachel]),
208 loadcomplete: function(cachel){
209 /* alert('loadcomplete '+cachel.url+' id='+cachel.id+
210 ' vi='+cachel.vi); */
212 if (cachel.id == this.currentid &&
214 this.pendingload = false;
216 this.display(cachel);
220 display: function(cachel){
221 var newstyle = this.calcsize(cachel);
222 var newimg = cachel.img.clone();
223 newimg.setStyles(newstyle);
228 this.prevdisplay.dispose();
229 this.prevdisplay = this.ondisplay.clone().
230 setStyle('zIndex', 2).injectInside(this.container.container);
231 newimg.replaces(this.ondisplay);
232 this.ondisplay = newimg;
237 this.fx = new Fx.Tween(this.ondisplay, {
238 duration: this.options.fxduration,
240 this.fx.addEvent('complete',this.displaycomplete.bind(this));
241 this.fx.start('opacity', 0, 1);
244 displaycomplete: function(){
245 this.prevdisplay.setStyle('display', 'none');
246 if (this.isplaying) {
247 this.timer = this.autonext.delay(this.delay,this);
251 autonext: function(){
252 if (this.isplaying) {
253 if (this.currentid < this.vimgs.length-1) {
254 this.show(this.currentid+1);
261 calcsize: function(cachel){
264 candidate = this.target.width /
265 this.vimgs[cachel.id][2][cachel.vi][0];
266 if (factor > candidate) { factor = candidate; }
267 candidate = this.target.height /
268 this.vimgs[cachel.id][2][cachel.vi][1];
269 if (factor > candidate) { factor = candidate; }
270 var w = Math.round(this.vimgs[cachel.id][2][cachel.vi][0] *
272 var h = Math.round(this.vimgs[cachel.id][2][cachel.vi][1] *
274 var t = Math.round((this.coords.height-h)/2);
275 var l = Math.round((this.coords.width-w)/2);
276 /* alert('new size: '+w+'x'+h+'+'+l+'+'+t); */
278 position: 'absolute',
286 showloading: function(){
287 this.loadingdiv.setStyle('display', 'block');
290 hideloading: function(){
291 this.loadingdiv.setStyle('display', 'none');
295 if (this.fx) this.fx.cancel();
298 updatecoords: function(){
299 this.coords = this.container.getCoordinates();
301 width: Math.round(this.coords.width *
302 this.options.percentage / 100),
303 height: Math.round(this.coords.height *
304 this.options.percentage / 100),
306 /* alert('coords: '+this.coords.width+'x'+this.coords.height+
307 ', target: '+this.target.width+'x'+this.target.height); */
311 Show.implement(new Options);
312 Show.implement(new Events);