issue101, fix another misbehaviour when seting display attribute
[phpfspot.git] / phpfspot.js
1 function showImage(id, scrollup)
2 {
3    if(!document.getElementById('content'))
4       return;
5
6    var content = document.getElementById('content');
7
8    /* blank the screen */
9    if(scrollup != undefined) {
10       content.innerHTML = "";
11    }
12
13    HTML_AJAX.replace(content, encodeURI('rpc.php?action=showphoto&id=' + id));
14
15    /* scroll the window up to the top */
16    if(scrollup != undefined) {
17       window.scrollTo(0,0);
18    }
19
20    /* delete some global vars */
21    delete(origHeight); origHeight = undefined;
22    delete(origWidth); origWidth = undefined;
23    delete(photo_details_pos); photo_details_pos = undefined;
24 }
25
26 function moveToThumb(thumb_id)
27 {
28    if(thumb_id == undefined)
29       return;
30
31    if(thumbimg = document.getElementById('thumbimg' + thumb_id)) {
32       window.scrollTo(0, findPos(thumbimg,'top')-100);
33    }
34
35 } // moveToThumb()
36
37 function findPos(obj, direction) {
38    var cur = 0;
39    if (obj.offsetParent) {
40       do {
41          if(direction == 'left')
42             cur += obj.offsetLeft;
43          if(direction == 'top')
44             cur += obj.offsetTop;
45       } while (obj = obj.offsetParent);
46    }
47    return [cur];
48 }
49
50 function showCredits()
51 {
52    var credits = document.getElementById("content");
53    credits.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=showcredits'));
54 }
55
56 function Tags(mode, id)
57 {
58    var objTemp = new Object();
59
60    if(mode == "add") {
61       // add tag to users session
62       objTemp['action'] = 'addtag';
63       objTemp['id'] = id;
64    }
65    else if(mode == "del") {
66       // del tag from users session
67       objTemp['action'] = 'deltag';
68       objTemp['id'] = id;
69    }
70    else if(mode == "condition") {
71       setCheckedValue(id, id.value);
72       objTemp['action'] = 'tagcondition';
73       objTemp['mode'] = id.value;
74    }
75
76    var retr = HTML_AJAX.post('rpc.php', objTemp);
77    if(retr == "ok") {
78       refreshAvailableTags();
79       refreshSelectedTags();
80       refreshPhotoIndex();
81    }
82    else {
83       window.alert("Server message: "+ retr);
84    }
85
86 } // Tags()
87
88 function refreshAvailableTags()
89 {
90    // update available tags
91    var avail_tags = document.getElementById('available_tags');
92    avail_tags.innerHTML = "Loading...";
93    avail_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_available_tags'));
94 }
95
96 function refreshSelectedTags()
97 {
98    // update selected tags
99    var selected_tags = document.getElementById("selected_tags");
100    selected_tags.innerHTML = "Loading...";
101    selected_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_selected_tags'));
102 }
103
104 function showPhotoIndex(begin_with, last_photo)
105 {
106    var url = "rpc.php?action=show_photo_index";
107    if(begin_with != undefined)
108       url = url + '&begin_with=' + begin_with;
109    if(last_photo != undefined)
110       url = url + '&last_photo=' + last_photo;
111
112    HTML_AJAX.replace(document.getElementById("content"), encodeURI(url));
113
114 } // showPhotoIndex()
115
116 // if photo index is currently shown, refresh it
117 function refreshPhotoIndex()
118 {
119    if(document.getElementById("index") != undefined || startup == 1) {
120       showPhotoIndex();
121       startup = 0;
122    }
123 }
124
125 function click(object)
126 {
127    if(object.blur)
128       object.blur();
129
130 }
131
132 // set the radio button with the given value as being checked
133 // do nothing if there are no radio buttons
134 // if the given value does not exist, all the radio buttons
135 // are reset to unchecked
136 function setCheckedValue(condition, value) {
137
138    var count = condition.length;
139    if(count == undefined) {
140       condition.checked = (condition.value == value.toString());
141       return;
142    }
143    for(var i = 0; i < count; i++) {
144       condition[i].checked = false;
145       if(condition[i].value == value.toString()) {
146          condition[i].checked = true;
147       }
148    }
149 }
150
151 function startSearch()
152 {
153    from_year = document.getElementById('fromyear').value;
154    from_month = document.getElementById('frommonth').value;
155    from_day = document.getElementById('fromday').value;
156    from = from_year +"-"+ from_month +"-"+ from_day;
157    to_year = document.getElementById('toyear').value;
158    to_month = document.getElementById('tomonth').value;
159    to_day = document.getElementById('today').value;
160    to = to_year +"-"+ to_month +"-"+ to_day;
161
162    var objTemp = new Object();
163    objTemp['action'] = 'search';
164
165    if(document.getElementsByName('searchfor_tag')[0].value != "") {
166       objTemp['for_tag'] = document.getElementsByName('searchfor_tag')[0].value;
167    }
168    if(document.getElementsByName('searchfor_name')[0].value != "") {
169       objTemp['for_name'] = document.getElementsByName('searchfor_name')[0].value;
170    }
171    if(document.getElementsByName('consider_date')[0].checked == true) {
172       objTemp['from'] = from;
173       objTemp['to'] = to;
174    }
175
176    var retr = HTML_AJAX.post('rpc.php', objTemp);
177    if(retr == "ok") {
178       refreshAvailableTags();
179       refreshSelectedTags();
180       showPhotoIndex();
181    }
182    else {
183       window.alert("Server message: "+ retr);
184    }
185
186 } // startSearch()
187
188 function datesearch()
189 {
190    var mode = true;
191
192    if(document.getElementsByName('consider_date')[0].checked == true) {
193       mode = false;
194    }
195       
196    document.getElementById('fromyear').disabled = mode;
197    document.getElementById('frommonth').disabled = mode;
198    document.getElementById('fromday').disabled = mode;
199    document.getElementById('toyear').disabled = mode;
200    document.getElementById('tomonth').disabled = mode;
201    document.getElementById('today').disabled = mode;
202  
203 }
204
205 function setViewMode(mode)
206 {
207    var exprt = document.getElementById('output');
208    exprt.innerHTML = "Loading...";
209    exprt.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_export&mode=' + mode));
210 }
211
212 function clearSearch()
213 {
214    document.getElementsByName('searchfor_tag')[0].value = '';
215    document.getElementsByName('searchfor_name')[0].value = '';
216
217    if(document.getElementsByName('consider_date')[0].checked == true) {
218       document.getElementsByName('consider_date')[0].checked = false;
219       datesearch();
220    }  
221 }
222
223 function AskServerWhatToDo()
224 {
225    return HTML_AJAX.grab(encodeURI('rpc.php?action=what_to_do'));
226 }
227
228 function init_phpfspot(mode)
229 {
230    /* always load list of available tags */
231    refreshAvailableTags();
232
233    /* ask the server what we are currently displaying */
234    whattodo = AskServerWhatToDo();
235
236    if(whattodo == 'showpi' || whattodo == 'showpi_date') {
237       showPhotoIndex();
238    }
239    if(whattodo == 'showpi_tags') {
240       refreshSelectedTags();
241       showPhotoIndex();
242    }
243    if(whattodo == 'show_photo') {
244       if(photo = getPhotoToShow()) {
245          showImage(photo);
246          refreshSelectedTags();
247       }
248    }
249 } // init_phpfspot()
250
251 function setBackGrdColor(item, color)
252 {
253    if(color == 'mouseover')
254       item.style.backgroundColor='#c6e9ff';
255    if(color == 'mouseout')
256       item.style.backgroundColor='#eeeeee';
257    if(color == 'mouseclick')
258       item.style.backgroundColor='#93A8CA';
259 }
260
261 function getPhotoToShow()
262 {
263    // update selected tags
264    var photo_to_show = HTML_AJAX.grab(encodeURI('rpc.php?action=get_photo_to_show'));
265
266    // if no image needs to be shown, return false from here
267    if(photo_to_show == "")
268       return false;
269    
270    return photo_to_show;
271 }
272
273 function zoom(mod)
274 {
275    var photo;
276
277    if(mod == undefined)
278       return;
279
280    /* internet explorer */
281    if(document.images['photo'].width)
282       photo = document.images['photo'];
283
284    /* all others */
285    if(photo == undefined && document.getElementById('photo').width)
286       photo = document.getElementById('photo');
287
288    if(photo != undefined) {
289
290       if(origWidth == undefined)
291          origWidth = photo.width;
292       if(origHeight == undefined)
293          origHeight = photo.height;
294
295       if(mod != 0) {
296          new_w = photo.width * (1 + mod/100);
297          new_h = photo.height * (1 + mod/100);
298          photo.width = new_w;
299          photo.height = new_h;
300
301          if(photo_details_pos == undefined) {
302             photo_details_pos = findPos(document.getElementById('photo_details'),'left');
303          }
304
305          if((photo.offsetLeft + new_w) >= photo_details_pos-20) {
306             hidePhotoDetails('true');
307          }
308          else {
309             hidePhotoDetails('false');
310          }
311       }
312       else {
313          photo.width = origWidth;
314          photo.height = origHeight;
315          hidePhotoDetails('false');
316       }
317    }
318
319 } // zoom()
320
321 function hidePhotoDetails(mode)
322 {
323    var photo_details;
324
325    if(photo_details = document.getElementById('photo_details')) {
326       if(mode == 'true') {
327          photo_details.style.visibility = 'hidden';
328          photo_details.style.display = 'none';
329       }
330       else {
331          photo_details.style.visibility = 'visible';
332          photo_details.style.display = '';
333       }
334    }
335 } // hidePhotoDetails()
336
337 function showCalendar(date_box, click_obj)
338 {
339    var calendar = document.getElementById('calendar');
340    var year = document.getElementById(date_box+'year').value;
341    var month = document.getElementById(date_box+'month').value;
342    if(date_box == 'from') {
343       var xpos = document.getElementById('frompic').offsetLeft;
344       var ypos = document.getElementById('frompic').offsetTop;
345       calendar_mode = 'from';
346    }
347    if(date_box == 'to') {
348       var xpos = document.getElementById('topic').offsetLeft;
349       var ypos = document.getElementById('topic').offsetTop;
350       calendar_mode = 'to';
351    }
352    calendar.style.left = xpos + 100 + 'px';
353    calendar.style.top = ypos + 80 + 'px';
354
355    if(calendar.style.visibility == "" || calendar.style.visibility == 'hidden') {
356       calendar.style.visibility = 'visible';
357       calendar.innerHTML = "Loading...";
358       calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year=' + year + '&month=' + month));
359       calendar_shown = 1;
360    }
361    else {
362       hideCalendar();
363    }
364 }
365
366 function hideCalendar()
367 {
368    var calendar = document.getElementById('calendar');
369    if(calendar.style.visibility != 'hidden') {
370       calendar.style.visibility = 'hidden';
371       calendar_shown = 0;
372    }
373 }
374
375 function setMonth(year, month, day)
376 {
377    var calendar = document.getElementById('calendar');
378    calendar.innerHTML = "Loading...";
379    calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
380 }
381
382 function setCalendarDate(year, month, day)
383 {
384    document.getElementById(calendar_mode+'year').value = year;
385    document.getElementById(calendar_mode+'month').value = month;
386    document.getElementById(calendar_mode+'day').value = day;
387    hideCalendar();
388 }
389
390 function resetAll()
391 {
392    HTML_AJAX.grab(encodeURI('rpc.php?action=reset'));
393    clearSearch();
394    refreshAvailableTags();
395    refreshSelectedTags();
396    refreshPhotoIndex();
397 }
398
399 function WSR_getElementsByClassName(oElm, strTagName, oClassNames){
400    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
401    var arrReturnElements = new Array();
402    var arrRegExpClassNames = new Array();
403    if(typeof oClassNames == "object"){
404       for(var i=0; i<oClassNames.length; i++){
405          arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
406       }
407    }
408    else{
409       arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
410    }
411    var oElement;
412    var bMatchesAll;
413    for(var j=0; j<arrElements.length; j++){
414       oElement = arrElements[j];
415       bMatchesAll = true;
416       for(var k=0; k<arrRegExpClassNames.length; k++){
417          if(!arrRegExpClassNames[k].test(oElement.className)){
418             bMatchesAll = false;
419             break;
420          }
421       }
422       if(bMatchesAll){
423          arrReturnElements.push(oElement);
424       }
425    }
426    return (arrReturnElements)
427 }
428
429
430 function preloadPhotos(lbImg) {
431
432    var d=document;
433    if(d.images)
434       if(!d.photos)
435          d.photos=new Array();
436
437    var i, j=d.photos.length;
438
439    lbImg=WSR_getElementsByClassName(document,"img","thumb");
440    for(i=0;i<lbImg.length;i++){
441       d.photos[j]=new Image;
442       d.photos[j].src=lbImg[i].src;
443       j++;
444    }
445 }
446
447 function startSlideShow()
448 {
449    if(!sliding) {
450       HTML_AJAX.grab(encodeURI('rpc.php?action=reset_slideshow'));
451       nextSlide();
452       sliding = setInterval("nextSlide()", sliding_time*1000);
453       document.getElementById('stop_ico').src = "resources/32_stop.png";
454    }
455    else {
456       clearInterval(sliding);
457       sliding = 0;
458       document.getElementById('stop_ico').src = "resources/32_play.png";
459    }
460 }
461
462 function nextSlide()
463 {
464    var next_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_next_slideshow_img'));
465    document.getElementById('slide_img').src = next_img;
466 }
467
468 function prevSlide()
469 {
470    var prev_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_prev_slideshow_img'));
471    document.getElementById('slide_img').src = prev_img;
472 }
473
474 function pauseSlideShow()
475 {
476    if(!sliding_paused) {
477       sliding_paused = 1;
478       clearInterval(sliding);
479       document.getElementById('pause_ico').src = "resources/32_play.png";
480    }
481    else {
482       sliding_paused = 0;
483       sliding = setInterval("nextSlide()", sliding_time*1000);
484       document.getElementById('pause_ico').src = "resources/32_pause.png";
485    }
486
487
488 function startAutoBrowse()
489 {
490    if(!autobrowse) {
491       autoBrowse();
492       autobrowse = setInterval("autoBrowse()", 5000);
493    }
494    else {
495       clearInterval(autobrowse);
496       autobrowse = 0;
497       document.getElementById('autobrowse_ico').src = "resources/32_play.png";
498    }
499
500 } // startAutoBrowser()
501
502 function autoBrowse()
503 {
504    if(document.getElementById('next_link')) {
505       var next_link = document.getElementById('next_link').href;
506       window.location.href = next_link;
507       document.getElementById('autobrowse_ico').src = "resources/32_pause.png";
508    }
509    /* we have reached the last photo */
510    else {
511       if(ab_ico = document.getElementById('autobrowse_ico'))
512          ab_ico.src = "resources/32_play.png";
513       clearInterval(autobrowse);
514    }
515
516 } // autoBrowse()
517
518 function initSlider()
519 {
520    var sliderEl = document.getElementById ? document.getElementById("slider-1") : null;
521    var inputEl = document.forms[0]["slider-input-1"];
522    var s = new Slider(sliderEl, inputEl);
523    s.setMinimum(1);
524    s.setMaximum(10);
525    s.setValue(sliding_time);
526    document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
527    s.onchange = function () {
528       sliding_time = s.getValue();
529       document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
530       if(!sliding_paused && sliding) {
531          clearInterval(sliding);
532          sliding = setInterval("nextSlide()", sliding_time*1000);
533       }
534    };
535    window.onresize = function () {
536       s.recalculate();
537    };
538
539 }
540
541 function update_sort_order(obj)
542 {  
543    var objTemp = new Object();
544    objTemp['value'] = obj.options[obj.selectedIndex].value;
545
546    var retr = HTML_AJAX.post('rpc.php?action=update_sort_order', objTemp);
547
548    if(retr == "ok") {
549       showPhotoIndex();
550    }
551    else {
552       window.alert("Server message: "+ retr);
553    }
554
555 } // update_sort_order()
556
557
558 function keyDown(e) {
559    var evt = (e) ? e:(window.event) ? window.event:null;
560
561    if(evt) {
562       var key = (evt.charCode) ? evt.charCode :
563          ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
564
565
566       if(key == 37) /* left curosr */ {
567          if(document.getElementById('prev_link')) {
568             var prev_link = document.getElementById('prev_link').href;
569             window.location.href = prev_link;
570          }
571          return;
572       }
573       if(key == 38) /* up cursor */ {
574       }
575       if(key == 39) /* right curosr */ {
576          if(document.getElementById('next_link')) {
577             var next_link = document.getElementById('next_link').href;
578             window.location.href = next_link;
579          }
580          return;
581       }
582       if(key == 73 && evt.altKey && evt.ctrlKey) /* ctrl+alt+i */ {
583          showPhotoIndex();
584          return;
585       }
586       if(key == 82 && evt.altKey && evt.ctrlKey) /* ctrl+alt+r */ {
587          resetAll();
588          return;
589       }
590    }
591 }
592
593 document.onkeydown=keyDown;
594 if(document.layers) {
595    document.captureEvents(Event.KEYDOWN);
596 }
597
598 var startup = 1;
599 var calendar_shown = 0;
600 var calendar_mode = '';
601 var autobrowse = 0;
602 var sliding = 0;
603 var sliding_paused = 0;
604 var sliding_time = 3;
605 var origHeight;
606 var origWidth;
607 var photo_details_pos;