issue51, fixed incorrect value returned from whattodo()
[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
153 function AskServerWhatToDo()
154 {
155    return HTML_AJAX.grab(encodeURI('rpc.php?action=what_to_do'));
156 }
157
158 function init_phpfspot()
159 {
160    refreshAvailableTags();
161
162    whattodo = AskServerWhatToDo();
163
164    if(whattodo == 'showpi' || whattodo == 'showpi_date') {
165       showPhotoIndex();
166    }
167    if(whattodo == 'showpi_tags') {
168       refreshSelectedTags();
169       showPhotoIndex();
170    }
171    if(whattodo == 'show_photo') {
172       if(photo = getPhotoToShow()) {
173          showImage(photo)
174          refreshSelectedTags();
175       }
176    }
177 }
178
179 function setBackGrdColor(item, color)
180 {
181    if(color == 'mouseover')
182       item.style.backgroundColor='#c6e9ff';
183    if(color == 'mouseout')
184       item.style.backgroundColor='#eeeeee';
185    if(color == 'mouseclick')
186       item.style.backgroundColor='#93A8CA';
187 }
188
189 function getPhotoToShow()
190 {
191    // update selected tags
192    var photo_to_show = HTML_AJAX.grab(encodeURI('rpc.php?action=get_photo_to_show'));
193
194    // if no image needs to be shown, return false from here
195    if(photo_to_show == "")
196       return false;
197    
198    return photo_to_show;
199 }
200
201 function showCalendar(date_box, click_obj)
202 {
203    var calendar = document.getElementById('calendar');
204    if(date_box == 'from') {
205       var xpos = document.getElementById('frompic').offsetLeft;
206       var ypos = document.getElementById('frompic').offsetTop;
207       calendar_mode = 'from';
208    }
209    if(date_box == 'to') {
210       var xpos = document.getElementById('topic').offsetLeft;
211       var ypos = document.getElementById('topic').offsetTop;
212       calendar_mode = 'to';
213    }
214    calendar.style.left = xpos + 60 + 'px';
215    calendar.style.top = ypos + 80 + 'px';
216
217    if(calendar.style.visibility == "" || calendar.style.visibility == 'hidden') {
218       calendar.style.visibility = 'visible';
219       calendar.innerHTML = "Loading...";
220       calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix'));
221       calendar_shown = 1;
222    }
223    else {
224       calendar.style.visibility = 'hidden';
225       calendar_shown = 0;
226    }
227 }
228
229 function setMonth(year, month, day)
230 {
231    var calendar = document.getElementById('calendar');
232    calendar.innerHTML = "Loading...";
233    calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
234 }
235
236 function setCalendarDate(year, month, day)
237 {
238    document.getElementById(calendar_mode+'year').value = year;
239    document.getElementById(calendar_mode+'month').value = month;
240    document.getElementById(calendar_mode+'day').value = day;
241 }
242
243 function resetAll()
244 {
245    HTML_AJAX.grab(encodeURI('rpc.php?action=reset'));
246    clearSearch();
247    refreshAvailableTags();
248    refreshSelectedTags();
249    refreshPhotoIndex();
250 }
251
252 function WSR_getElementsByClassName(oElm, strTagName, oClassNames){
253    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
254    var arrReturnElements = new Array();
255    var arrRegExpClassNames = new Array();
256    if(typeof oClassNames == "object"){
257       for(var i=0; i<oClassNames.length; i++){
258          arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
259       }
260    }
261    else{
262       arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
263    }
264    var oElement;
265    var bMatchesAll;
266    for(var j=0; j<arrElements.length; j++){
267       oElement = arrElements[j];
268       bMatchesAll = true;
269       for(var k=0; k<arrRegExpClassNames.length; k++){
270          if(!arrRegExpClassNames[k].test(oElement.className)){
271             bMatchesAll = false;
272             break;
273          }
274       }
275       if(bMatchesAll){
276          arrReturnElements.push(oElement);
277       }
278    }
279    return (arrReturnElements)
280 }
281
282
283 function preloadPhotos(lbImg) {
284
285    var d=document;
286    if(d.images)
287       if(!d.photos)
288          d.photos=new Array();
289
290    var i, j=d.photos.length;
291
292    lbImg=WSR_getElementsByClassName(document,"img","thumb");
293    for(i=0;i<lbImg.length;i++){
294       d.photos[j]=new Image;
295       d.photos[j].src=lbImg[i].src;
296       j++;
297    }
298 }
299
300 var startup = 1;
301 var calendar_shown = 0;
302 var calendar_mode = '';
303