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