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