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