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