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