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