b196a68f846dbe3222b3309ac46f4066735daedb
[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()
164 {
165    refreshAvailableTags();
166
167    whattodo = AskServerWhatToDo();
168
169    if(whattodo == 'showpi' || whattodo == 'showpi_date') {
170       showPhotoIndex();
171    }
172    if(whattodo == 'showpi_tags') {
173       refreshSelectedTags();
174       showPhotoIndex();
175    }
176    if(whattodo == 'show_photo') {
177       if(photo = getPhotoToShow()) {
178          showImage(photo)
179          refreshSelectedTags();
180       }
181    }
182 }
183
184 function setBackGrdColor(item, color)
185 {
186    if(color == 'mouseover')
187       item.style.backgroundColor='#c6e9ff';
188    if(color == 'mouseout')
189       item.style.backgroundColor='#eeeeee';
190    if(color == 'mouseclick')
191       item.style.backgroundColor='#93A8CA';
192 }
193
194 function getPhotoToShow()
195 {
196    // update selected tags
197    var photo_to_show = HTML_AJAX.grab(encodeURI('rpc.php?action=get_photo_to_show'));
198
199    // if no image needs to be shown, return false from here
200    if(photo_to_show == "")
201       return false;
202    
203    return photo_to_show;
204 }
205
206 function showCalendar(date_box, click_obj)
207 {
208    var calendar = document.getElementById('calendar');
209    var year = document.getElementById(date_box+'year').value;
210    var month = document.getElementById(date_box+'month').value;
211    if(date_box == 'from') {
212       var xpos = document.getElementById('frompic').offsetLeft;
213       var ypos = document.getElementById('frompic').offsetTop;
214       calendar_mode = 'from';
215    }
216    if(date_box == 'to') {
217       var xpos = document.getElementById('topic').offsetLeft;
218       var ypos = document.getElementById('topic').offsetTop;
219       calendar_mode = 'to';
220    }
221    calendar.style.left = xpos + 100 + 'px';
222    calendar.style.top = ypos + 80 + 'px';
223
224    if(calendar.style.visibility == "" || calendar.style.visibility == 'hidden') {
225       calendar.style.visibility = 'visible';
226       calendar.innerHTML = "Loading...";
227       calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year=' + year + '&month=' + month));
228       calendar_shown = 1;
229    }
230    else {
231       hideCalendar();
232    }
233 }
234
235 function hideCalendar()
236 {
237    var calendar = document.getElementById('calendar');
238    if(calendar.style.visibility != 'hidden') {
239       calendar.style.visibility = 'hidden';
240       calendar_shown = 0;
241    }
242 }
243
244 function setMonth(year, month, day)
245 {
246    var calendar = document.getElementById('calendar');
247    calendar.innerHTML = "Loading...";
248    calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
249 }
250
251 function setCalendarDate(year, month, day)
252 {
253    document.getElementById(calendar_mode+'year').value = year;
254    document.getElementById(calendar_mode+'month').value = month;
255    document.getElementById(calendar_mode+'day').value = day;
256    hideCalendar();
257 }
258
259 function resetAll()
260 {
261    HTML_AJAX.grab(encodeURI('rpc.php?action=reset'));
262    clearSearch();
263    refreshAvailableTags();
264    refreshSelectedTags();
265    refreshPhotoIndex();
266 }
267
268 function WSR_getElementsByClassName(oElm, strTagName, oClassNames){
269    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
270    var arrReturnElements = new Array();
271    var arrRegExpClassNames = new Array();
272    if(typeof oClassNames == "object"){
273       for(var i=0; i<oClassNames.length; i++){
274          arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
275       }
276    }
277    else{
278       arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
279    }
280    var oElement;
281    var bMatchesAll;
282    for(var j=0; j<arrElements.length; j++){
283       oElement = arrElements[j];
284       bMatchesAll = true;
285       for(var k=0; k<arrRegExpClassNames.length; k++){
286          if(!arrRegExpClassNames[k].test(oElement.className)){
287             bMatchesAll = false;
288             break;
289          }
290       }
291       if(bMatchesAll){
292          arrReturnElements.push(oElement);
293       }
294    }
295    return (arrReturnElements)
296 }
297
298
299 function preloadPhotos(lbImg) {
300
301    var d=document;
302    if(d.images)
303       if(!d.photos)
304          d.photos=new Array();
305
306    var i, j=d.photos.length;
307
308    lbImg=WSR_getElementsByClassName(document,"img","thumb");
309    for(i=0;i<lbImg.length;i++){
310       d.photos[j]=new Image;
311       d.photos[j].src=lbImg[i].src;
312       j++;
313    }
314 }
315
316 function startSlideShow()
317 {
318    if(!sliding) {
319       HTML_AJAX.grab(encodeURI('rpc.php?action=reset_slideshow'));
320       nextSlide();
321       sliding = setInterval("nextSlide()", sliding_time*1000);
322       document.getElementById('stop_ico').src = "resources/32_stop.png";
323    }
324    else {
325       clearInterval(sliding);
326       sliding = 0;
327       document.getElementById('stop_ico').src = "resources/32_play.png";
328    }
329 }
330
331 function nextSlide()
332 {
333    next_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_next_slideshow_img'));
334    document.getElementById('slide_img').src = next_img;
335 }
336
337 function prevSlide()
338 {
339    prev_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_prev_slideshow_img'));
340    document.getElementById('slide_img').src = prev_img;
341 }
342
343 function pauseSlideShow()
344 {
345    if(!sliding_paused) {
346       sliding_paused = 1;
347       clearInterval(sliding);
348       document.getElementById('pause_ico').src = "resources/32_play.png";
349    }
350    else {
351       sliding_paused = 0;
352       sliding = setInterval("nextSlide()", sliding_time*1000);
353       document.getElementById('pause_ico').src = "resources/32_pause.png";
354    }
355
356
357 function startAutoBrowse()
358 {
359    if(!autobrowse) {
360       autoBrowse();
361       autobrowse = setInterval("autoBrowse()", 5000);
362    }
363    else {
364       clearInterval(autobrowse);
365       autobrowse = 0;
366       document.getElementById('autobrowse_ico').src = "resources/32_play.png";
367    }
368
369 }
370 function autoBrowse()
371 {
372    if(document.getElementById('next_link')) {
373       var next_link = document.getElementById('next_link').href;
374       window.location.href = next_link;
375       document.getElementById('autobrowse_ico').src = "resources/32_pause.png";
376    }
377 }  
378
379 function initSlider()
380 {
381    var sliderEl = document.getElementById ? document.getElementById("slider-1") : null;
382    var inputEl = document.forms[0]["slider-input-1"];
383    var s = new Slider(sliderEl, inputEl);
384    s.setMinimum(1);
385    s.setMaximum(10);
386    s.setValue(sliding_time);
387    document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
388    s.onchange = function () {
389       sliding_time = s.getValue();
390       document.getElementById("current_slide_time").innerHTML = sliding_time + "s Interval";
391       if(!sliding_paused && sliding) {
392          clearInterval(sliding);
393          sliding = setInterval("nextSlide()", sliding_time*1000);
394       }
395    };
396    window.onresize = function () {
397       s.recalculate();
398    };
399
400 }
401
402 function update_sort_order(obj)
403 {  
404    var objTemp = new Object();
405    objTemp['value'] = obj.options[obj.selectedIndex].value;
406
407    var retr = HTML_AJAX.post('rpc.php?action=update_sort_order', objTemp);
408
409    if(retr == "ok") {
410       showPhotoIndex();
411    }
412    else {
413       window.alert(retr);
414    }
415
416 } // update_sort_order()
417
418
419 var startup = 1;
420 var calendar_shown = 0;
421 var calendar_mode = '';
422 var autobrowse = 0;
423 var sliding = 0;
424 var sliding_paused = 0;
425 var sliding_time = 3;