add GPL header and many comments to phpfspot.js
[phpfspot.git] / phpfspot.js
1 /***************************************************************************
2  *
3  * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
4  * All rights reserved
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  ***************************************************************************/
21
22 /**
23  * display image
24  *
25  * this function will be called by client and fetches
26  * the single-photo view via AJAX from the server.
27  * Furthermore it will scrollup the browser to the top
28  * position so the image become visibile immediatley.
29  */
30 function showImage(id, scrollup)
31 {
32    /* is phpfspot skeleton really displayed? */
33    if(!document.getElementById('content'))
34       return;
35
36    var content = document.getElementById('content');
37
38    /* blank the screen */
39    if(scrollup != undefined) {
40       content.innerHTML = "";
41    }
42
43    /* fetch single-photo view from server */
44    HTML_AJAX.replace(content, encodeURI('rpc.php?action=showphoto&id=' + id));
45
46    /* scroll the window up to the top */
47    if(scrollup != undefined) {
48       window.scrollTo(0,0);
49    }
50
51    /* delete some global vars */
52    delete(origHeight); origHeight = undefined;
53    delete(origWidth); origWidth = undefined;
54    delete(photo_details_pos); photo_details_pos = undefined;
55
56 } // showImage()
57
58 /**
59  * scroll browser to the last shown photo
60  *
61  * this function will be called when user returns from single-photo
62  * to the photo index. It will scroll down the window (if possible)
63  * to the position of the last shown photo.
64  */
65 function moveToThumb(thumb_id)
66 {
67    if(thumb_id == undefined)
68       return;
69
70    if(thumbimg = document.getElementById('thumbimg' + thumb_id)) {
71       window.scrollTo(0, findPos(thumbimg,'top')-100);
72    }
73
74 } // moveToThumb()
75
76 /**
77  * return position of object
78  *
79  * this function returns the position of an object.
80  * depending on the parameter 'direction' it will either
81  * return the X or Y position.
82  */
83 function findPos(obj, direction) {
84    var cur = 0;
85    if (obj.offsetParent) {
86       do {
87          if(direction == 'left')
88             cur += obj.offsetLeft;
89          if(direction == 'top')
90             cur += obj.offsetTop;
91       } while (obj = obj.offsetParent);
92    }
93    return [cur];
94
95 } // findPos()
96
97 /**
98  * opens the credits page
99  */
100 function showCredits()
101 {
102    var credits = document.getElementById("content");
103    credits.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=showcredits'));
104
105 } // showCredits()
106
107 /**
108  * tag-selection handling
109  *
110  * this function is getting called by client to either
111  * - add
112  ∗ - delete
113  * - modify tag-match condition
114  *
115  * It will then fetch the result from the server via AJAX
116  * and updates the tag-selection.
117  */
118 function Tags(mode, id)
119 {
120    var objTemp = new Object();
121
122    if(mode == "add") {
123       // add tag to users session
124       objTemp['action'] = 'addtag';
125       objTemp['id'] = id;
126    }
127    else if(mode == "del") {
128       // del tag from users session
129       objTemp['action'] = 'deltag';
130       objTemp['id'] = id;
131    }
132    else if(mode == "condition") {
133       setCheckedValue(id, id.value);
134       objTemp['action'] = 'tagcondition';
135       objTemp['mode'] = id.value;
136    }
137
138    var retr = HTML_AJAX.post('rpc.php', objTemp);
139    if(retr == "ok") {
140       refreshAvailableTags();
141       refreshSelectedTags();
142       refreshPhotoIndex();
143    }
144    else {
145       window.alert("Server message: "+ retr);
146    }
147
148 } // Tags()
149
150 /**
151  * update available-tags tag-cloud
152  *
153  * this function queries an actual version of the tag-cloud
154  * for the available (not-selected) tags from the server via
155  * AJAX.
156  */
157 function refreshAvailableTags()
158 {
159    // update available tags
160    var avail_tags = document.getElementById('available_tags');
161    avail_tags.innerHTML = "Loading...";
162    avail_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_available_tags'));
163
164 } // refreshAvailableTags()
165
166 /**
167  * update selected-tags list
168  *
169  * this function queries an actual version of the tag-list
170  * for the selected tags from the server via AJAX.
171  */
172 function refreshSelectedTags()
173 {
174    // update selected tags
175    var selected_tags = document.getElementById("selected_tags");
176    selected_tags.innerHTML = "Loading...";
177    selected_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_selected_tags'));
178
179 } // refreshSelectedTags()
180
181 /**
182  * show photo index
183  *
184  * this function will fetch the photo-index view from
185  * the server via AJAX. It's also used to browse through
186  * the photo-index pages
187  */
188 function showPhotoIndex(begin_with, last_photo)
189 {
190    var url = "rpc.php?action=show_photo_index";
191    if(begin_with != undefined)
192       url = url + '&begin_with=' + begin_with;
193    if(last_photo != undefined)
194       url = url + '&last_photo=' + last_photo;
195
196    HTML_AJAX.replace(document.getElementById("content"), encodeURI(url));
197
198 } // showPhotoIndex()
199
200 /**
201  * update photo index
202  *
203  * this function will be called, to request a refresh of the
204  * photo index. this, for example, can be caused, when changing
205  * the tag-selection.
206  */
207 function refreshPhotoIndex()
208 {
209    /* only invoke showPhotoIndex(), if photo-index is really shown */
210    if(document.getElementById("index") != undefined || startup == 1) {
211       showPhotoIndex();
212       startup = 0;
213    }
214
215 } // refreshPhotoIndex()
216
217 /**
218  * blur cursor focus
219  *
220  * this function removes the focus-rectangle which may appear
221  * when click on a link. Not always beautiful.
222  */
223 function click(object)
224 {
225    if(object.blur)
226       object.blur();
227
228 } // click()
229
230 /**
231  * change current radio-button setting
232  *
233  * This function will check the radio-button with the given value.
234  * If no radio-button is currently displayed, this function will do
235  * nothing.
236  * If the given value does not exist, the existing radio buttons will
237  * be reseted.
238  */
239 function setCheckedValue(condition, value) {
240
241    var count = condition.length;
242    if(count == undefined) {
243       condition.checked = (condition.value == value.toString());
244       return;
245    }
246    for(var i = 0; i < count; i++) {
247       condition[i].checked = false;
248       if(condition[i].value == value.toString()) {
249          condition[i].checked = true;
250       }
251    }
252
253 } // setCheckedValue()
254
255 /**
256  * Invoke a search
257  *
258  * This function will be invoked by starting
259  * any kind of search (tag-name, photo-name, date, ...).
260  */
261 function startSearch()
262 {
263    from_year = document.getElementById('fromyear').value;
264    from_month = document.getElementById('frommonth').value;
265    from_day = document.getElementById('fromday').value;
266    from = from_year +"-"+ from_month +"-"+ from_day;
267    to_year = document.getElementById('toyear').value;
268    to_month = document.getElementById('tomonth').value;
269    to_day = document.getElementById('today').value;
270    to = to_year +"-"+ to_month +"-"+ to_day;
271
272    var objTemp = new Object();
273    objTemp['action'] = 'search';
274
275    if(document.getElementsByName('searchfor_tag')[0].value != "") {
276       objTemp['for_tag'] = document.getElementsByName('searchfor_tag')[0].value;
277    }
278    if(document.getElementsByName('searchfor_name')[0].value != "") {
279       objTemp['for_name'] = document.getElementsByName('searchfor_name')[0].value;
280    }
281    if(document.getElementsByName('consider_date')[0].checked == true) {
282       objTemp['from'] = from;
283       objTemp['to'] = to;
284    }
285
286    var retr = HTML_AJAX.post('rpc.php', objTemp);
287    if(retr == "ok") {
288       refreshAvailableTags();
289       refreshSelectedTags();
290       showPhotoIndex();
291    }
292    else {
293       window.alert("Server message: "+ retr);
294    }
295
296 } // startSearch()
297
298 /**
299  * enable/disable date search
300  *
301  * this function will either enable or disable the
302  * input fields for the date-search
303  */
304 function datesearch()
305 {
306    var mode = true;
307
308    if(document.getElementsByName('consider_date')[0].checked == true) {
309       mode = false;
310    }
311       
312    document.getElementById('fromyear').disabled = mode;
313    document.getElementById('frommonth').disabled = mode;
314    document.getElementById('fromday').disabled = mode;
315    document.getElementById('toyear').disabled = mode;
316    document.getElementById('tomonth').disabled = mode;
317    document.getElementById('today').disabled = mode;
318  
319 } // datesearch()
320
321 /**
322  * set view mode
323  *
324  * called for photo-index export. will return the
325  * selected mode via AJAX from the server.
326  */
327 function setViewMode(mode)
328 {
329    var exprt = document.getElementById('output');
330    exprt.innerHTML = "Loading...";
331    exprt.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_export&mode=' + mode));
332
333 } // setViewMode()
334
335 /**
336  * reset all search-fields
337  */
338 function clearSearch()
339 {
340    document.getElementsByName('searchfor_tag')[0].value = '';
341    document.getElementsByName('searchfor_name')[0].value = '';
342
343    if(document.getElementsByName('consider_date')[0].checked == true) {
344       document.getElementsByName('consider_date')[0].checked = false;
345       datesearch();
346    }  
347
348 } // clearSearch()
349
350 /**
351  * if the client is planless, ask the server what to do
352  * next.
353  */
354 function AskServerWhatToDo()
355 {
356    return HTML_AJAX.grab(encodeURI('rpc.php?action=what_to_do'));
357 } // AskServerWhatToDo()
358
359 /**
360  * init phpfspot
361  *
362  * this function will be called when the browser opens phpfspot
363  * the first time. It will fetch the tag-lists and will then
364  * switch to the right view, which the browser got told from
365  * the server (maybe someone hit the refresh button...).
366  */
367 function init_phpfspot(mode)
368 {
369    /* always load list of available tags */
370    refreshAvailableTags();
371
372    /* ask the server what we are currently displaying */
373    whattodo = AskServerWhatToDo();
374
375    if(whattodo == 'showpi' || whattodo == 'showpi_date') {
376       showPhotoIndex();
377    }
378    if(whattodo == 'showpi_tags') {
379       refreshSelectedTags();
380       showPhotoIndex();
381    }
382    if(whattodo == 'show_photo') {
383       if(photo = getPhotoToShow()) {
384          showImage(photo);
385          refreshSelectedTags();
386       }
387    }
388
389 } // init_phpfspot()
390
391 /**
392  * change background-color on mouse-over
393  */
394 function setBackGrdColor(item, color)
395 {
396    if(color == 'mouseover')
397       item.style.backgroundColor='#c6e9ff';
398    if(color == 'mouseout')
399       item.style.backgroundColor='#eeeeee';
400    if(color == 'mouseclick')
401       item.style.backgroundColor='#93A8CA';
402
403 } // setBackGrdColor()
404
405 /**
406  * ask server, which photo needs to be shown
407  *
408  * when user press the refresh-button in a single-photo
409  * view or maybe enters the link via an external URL, the
410  * client does not know, what photo will be shown (dimensions...).
411  * But the server can tell this the browser.
412  */
413 function getPhotoToShow()
414 {
415    var photo_to_show = HTML_AJAX.grab(encodeURI('rpc.php?action=get_photo_to_show'));
416
417    // if no image needs to be shown, return false from here
418    if(photo_to_show == "")
419       return false;
420    
421    return photo_to_show;
422
423 } // getPhotoToShow()
424
425 /**
426  * a fake-zoom for photo
427  *
428  * a quick to let the browser do some zooming on
429  * photos.
430  */
431 function zoom(mod)
432 {
433    var photo;
434
435    if(mod == undefined)
436       return;
437
438    /* internet explorer */
439    if(document.images['photo'].width)
440       photo = document.images['photo'];
441
442    /* all others */
443    if(photo == undefined && document.getElementById('photo').width)
444       photo = document.getElementById('photo');
445
446    if(photo != undefined) {
447
448       if(origWidth == undefined)
449          origWidth = photo.width;
450       if(origHeight == undefined)
451          origHeight = photo.height;
452
453       if(mod != 0) {
454          new_w = photo.width * (1 + mod/100);
455          new_h = photo.height * (1 + mod/100);
456          photo.width = new_w;
457          photo.height = new_h;
458
459          if(photo_details_pos == undefined) {
460             photo_details_pos = findPos(document.getElementById('photo_details'),'left');
461          }
462
463          if((photo.offsetLeft + new_w) >= photo_details_pos-20) {
464             hidePhotoDetails('true');
465          }
466          else {
467             hidePhotoDetails('false');
468          }
469       }
470       else {
471          photo.width = origWidth;
472          photo.height = origHeight;
473          hidePhotoDetails('false');
474       }
475    }
476
477 } // zoom()
478
479 /**
480  * hides the photo details layin
481  *
482  * if the photo is getting zoomed quiet large, this will
483  * auto-hide (and also restore) the photo-details-box.
484  */
485 function hidePhotoDetails(mode)
486 {
487    var photo_details;
488
489    if(photo_details = document.getElementById('photo_details')) {
490       if(mode == 'true') {
491          photo_details.style.visibility = 'hidden';
492          photo_details.style.display = 'none';
493       }
494       else {
495          photo_details.style.visibility = 'visible';
496          photo_details.style.display = '';
497       }
498    }
499 } // hidePhotoDetails()
500
501 /**
502  * show calendar
503  */
504 function showCalendar(date_box, click_obj)
505 {
506    var calendar = document.getElementById('calendar');
507    var year = document.getElementById(date_box+'year').value;
508    var month = document.getElementById(date_box+'month').value;
509    if(date_box == 'from') {
510       var xpos = document.getElementById('frompic').offsetLeft;
511       var ypos = document.getElementById('frompic').offsetTop;
512       calendar_mode = 'from';
513    }
514    if(date_box == 'to') {
515       var xpos = document.getElementById('topic').offsetLeft;
516       var ypos = document.getElementById('topic').offsetTop;
517       calendar_mode = 'to';
518    }
519    calendar.style.left = xpos + 100 + 'px';
520    calendar.style.top = ypos + 80 + 'px';
521
522    if(calendar.style.visibility == "" || calendar.style.visibility == 'hidden') {
523       calendar.style.visibility = 'visible';
524       calendar.innerHTML = "Loading...";
525       calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year=' + year + '&month=' + month));
526       calendar_shown = 1;
527    }
528    else {
529       hideCalendar();
530    }
531
532 } // showCalendar()
533
534 /**
535  * hide calendar
536  */
537 function hideCalendar()
538 {
539    var calendar = document.getElementById('calendar');
540    if(calendar.style.visibility != 'hidden') {
541       calendar.style.visibility = 'hidden';
542       calendar_shown = 0;
543    }
544 } // hideCalendar()
545
546 /**
547  * switch month in calendar
548  */
549 function setMonth(year, month, day)
550 {
551    var calendar = document.getElementById('calendar');
552    calendar.innerHTML = "Loading...";
553    calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
554 }
555
556 /**
557  * get the user-selected date from the calendar and
558  * put it into the date-search boxes
559  */
560 function setCalendarDate(year, month, day)
561 {
562    document.getElementById(calendar_mode+'year').value = year;
563    document.getElementById(calendar_mode+'month').value = month;
564    document.getElementById(calendar_mode+'day').value = day;
565    hideCalendar();
566
567 } // setCalendarDate()
568
569 /**
570  * reset phpfspot complelely and move to the begining
571  */
572 function resetAll()
573 {
574    HTML_AJAX.grab(encodeURI('rpc.php?action=reset'));
575    clearSearch();
576    refreshAvailableTags();
577    refreshSelectedTags();
578    refreshPhotoIndex();
579
580 } // resetAll()
581
582 /**
583  * find objects with their class-name
584  */
585 function WSR_getElementsByClassName(oElm, strTagName, oClassNames){
586    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
587    var arrReturnElements = new Array();
588    var arrRegExpClassNames = new Array();
589    if(typeof oClassNames == "object"){
590       for(var i=0; i<oClassNames.length; i++){
591          arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
592       }
593    }
594    else{
595       arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
596    }
597    var oElement;
598    var bMatchesAll;
599    for(var j=0; j<arrElements.length; j++){
600       oElement = arrElements[j];
601       bMatchesAll = true;
602       for(var k=0; k<arrRegExpClassNames.length; k++){
603          if(!arrRegExpClassNames[k].test(oElement.className)){
604             bMatchesAll = false;
605             break;
606          }
607       }
608       if(bMatchesAll){
609          arrReturnElements.push(oElement);
610       }
611    }
612    return (arrReturnElements)
613
614 } // WSR_getElementsByClassName()
615
616 /**
617  * javascript based photo preloading
618  */
619 function preloadPhotos(image_url) {
620
621    var i;
622    var timeout = 1000;
623    var waiting = 100;
624    var counting;
625
626    lbImg=WSR_getElementsByClassName(document,"img","thumb");
627    for(i=0;i<lbImg.length;i++){
628       lbImg[i].src=image_url[i];
629       // to not bomb the server with requests, give the page some time
630       // to load the images one by one. if a image exceeds the timeout,
631       // the next image will be loaded.
632       if(lbImg[i].complete != undefined && lbImg[i].complete != true) {
633          counting = 0;
634          while(lbImg[i].complete != true && counting < timeout) {
635             window.setTimeout("noop()", waiting);
636             counting+=waiting;
637          }
638       }
639    }
640
641 } // preloadPhotos()
642
643 /* a function that does nothing */
644 function noop() {}
645
646 /**
647  * start slideshow
648  */
649 function startSlideShow()
650 {
651    if(!sliding) {
652       HTML_AJAX.grab(encodeURI('rpc.php?action=reset_slideshow'));
653       nextSlide();
654       sliding = setInterval("nextSlide()", sliding_time*1000);
655       document.getElementById('stop_ico').src = "resources/32_stop.png";
656    }
657    else {
658       clearInterval(sliding);
659       sliding = 0;
660       document.getElementById('stop_ico').src = "resources/32_play.png";
661    }
662
663 } // startSlideShow()
664
665 /**
666  * switch to next slide
667  */
668 function nextSlide()
669 {
670    var next_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_next_slideshow_img'));
671    document.getElementById('slide_img').src = next_img;
672
673 } // nextSlide()
674
675 /**
676  * switch to previous slide
677  */
678 function prevSlide()
679 {
680    var prev_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_prev_slideshow_img'));
681    document.getElementById('slide_img').src = prev_img;
682
683 } // prevSlide()
684
685 /**
686  * interrupt slide show
687  */
688 function pauseSlideShow()
689 {
690    if(!sliding_paused) {
691       sliding_paused = 1;
692       clearInterval(sliding);
693       document.getElementById('pause_ico').src = "resources/32_play.png";
694    }
695    else {
696       sliding_paused = 0;
697       sliding = setInterval("nextSlide()", sliding_time*1000);
698       document.getElementById('pause_ico').src = "resources/32_pause.png";
699    }
700
701 } // pauseSlideShow()
702
703 /**
704  * start auto-browse
705  */
706 function startAutoBrowse()
707 {
708    if(!autobrowse) {
709       autoBrowse();
710       autobrowse = setInterval("autoBrowse()", 5000);
711    }
712    else {
713       clearInterval(autobrowse);
714       autobrowse = 0;
715       document.getElementById('autobrowse_ico').src = "resources/32_play.png";
716    }
717
718 } // startAutoBrowser()
719
720 /**
721  * auto-browsing
722  */
723 function autoBrowse()
724 {
725    if(document.getElementById('next_link')) {
726       var next_link = document.getElementById('next_link').href;
727       window.location.href = next_link;
728       document.getElementById('autobrowse_ico').src = "resources/32_pause.png";
729    }
730    /* we have reached the last photo */
731    else {
732       if(ab_ico = document.getElementById('autobrowse_ico'))
733          ab_ico.src = "resources/32_play.png";
734       clearInterval(autobrowse);
735    }
736
737 } // autoBrowse()
738
739 /**
740  * initiate slider to modify slide-switching-speed
741  */
742 function initSlider()
743 {
744    var sliderEl = document.getElementById ? document.getElementById("slider-1") : null;
745    var inputEl = document.forms[0]["slider-input-1"];
746    var s = new Slider(sliderEl, inputEl);
747    s.setMinimum(1);
748    s.setMaximum(10);
749    s.setValue(sliding_time);
750    document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
751    s.onchange = function () {
752       sliding_time = s.getValue();
753       document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
754       if(!sliding_paused && sliding) {
755          clearInterval(sliding);
756          sliding = setInterval("nextSlide()", sliding_time*1000);
757       }
758    };
759    window.onresize = function () {
760       s.recalculate();
761    };
762
763 } // initSlider()
764
765 /**
766  * if the sort-order (photo-name, date, ...) has been
767  * changed, update the photo-index view.
768  */
769 function update_sort_order(obj)
770 {  
771    var objTemp = new Object();
772    objTemp['value'] = obj.options[obj.selectedIndex].value;
773
774    var retr = HTML_AJAX.post('rpc.php?action=update_sort_order', objTemp);
775
776    if(retr == "ok") {
777       showPhotoIndex();
778    }
779    else {
780       window.alert("Server message: "+ retr);
781    }
782
783 } // update_sort_order()
784
785 /**
786  * handle key events
787  */
788 function keyDown(e) {
789    var evt = (e) ? e:(window.event) ? window.event:null;
790
791    if(evt) {
792       var key = (evt.charCode) ? evt.charCode :
793          ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
794
795
796       if(key == 37) /* left curosr */ {
797          if(document.getElementById('prev_link')) {
798             var prev_link = document.getElementById('prev_link').href;
799             window.location.href = prev_link;
800          }
801          return;
802       }
803       if(key == 38) /* up cursor */ {
804       }
805       if(key == 39) /* right curosr */ {
806          if(document.getElementById('next_link')) {
807             var next_link = document.getElementById('next_link').href;
808             window.location.href = next_link;
809          }
810          return;
811       }
812       if(key == 73 && evt.altKey && evt.ctrlKey) /* ctrl+alt+i */ {
813          showPhotoIndex();
814          return;
815       }
816       if(key == 82 && evt.altKey && evt.ctrlKey) /* ctrl+alt+r */ {
817          resetAll();
818          return;
819       }
820    }
821 }
822
823 document.onkeydown=keyDown;
824 if(document.layers) {
825    document.captureEvents(Event.KEYDOWN);
826 }
827
828 // will be reseted by first viewing photo-index
829 var startup = 1;
830 // calendar specific
831 var calendar_shown = 0;
832 var calendar_mode = '';
833 // auto-browsing & sliding
834 var autobrowse = 0;
835 var sliding = 0;
836 var sliding_paused = 0;
837 var sliding_time = 3;
838 // zooming
839 var origHeight;
840 var origWidth;
841 // position of the last shown photo in photo-index
842 var photo_details_pos;