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