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