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