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