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