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