]> git.wh0rd.org Git - tt-rss.git/blob - viewfeed.js
possible workaround for xmlhttp stucking on safari
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var _catchup_callback_func = false;
3 var last_article_view = false;
4 var active_real_feed_id = false;
5
6 var _tag_active_post_id = false;
7 var _tag_active_feed_id = false;
8 var _tag_active_cdm = false;
9
10 // FIXME: kludge, to restore scrollTop after tag editor terminates
11 var _tag_cdm_scroll = false;
12
13 // FIXME: kludges, needs proper implementation
14 var _reload_feedlist_after_view = false;
15
16 var _cdm_wd_timeout = false;
17 var _cdm_wd_vishist = new Array();
18
19 var article_cache = new Array();
20
21 function catchup_callback() {
22         if (xmlhttp_rpc.readyState == 4) {
23                 try {
24                         debug("catchup_callback");
25                         if (_catchup_callback_func) {
26                                 setTimeout(_catchup_callback_func, 100);        
27                         }
28                         notify("");                     
29                         all_counters_callback();
30                 } catch (e) {
31                         exception_error("catchup_callback", e);
32                 }
33         }
34 }
35
36 function headlines_callback() {
37         if (xmlhttp.readyState == 4) {
38                 debug("headlines_callback");
39                 var f = document.getElementById("headlines-frame");
40                 try {
41                         f.scrollTop = 0;
42                 } catch (e) { };
43
44                 if (xmlhttp.responseXML) {
45                         var headlines = xmlhttp.responseXML.getElementsByTagName("headlines")[0];
46                         var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
47                         var articles = xmlhttp.responseXML.getElementsByTagName("article");
48
49                         if (headlines) {
50                                 f.innerHTML = headlines.firstChild.nodeValue;
51                         } else {
52                                 debug("headlines_callback: returned no data");
53                         f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
54
55                         }
56
57                         if (articles) {
58                                 for (var i = 0; i < articles.length; i++) {
59                                         var a_id = articles[i].getAttribute("id");
60                                         debug("found id: " + a_id);
61                                         cache_inject(a_id, articles[i].firstChild.nodeValue);
62                                 }
63                         } else {
64                                 debug("no cached articles received");
65                         }
66
67                         if (counters) {
68                                 debug("parsing piggybacked counters: " + counters);
69                                 parse_counters(counters, false);
70                         } else {
71                                 debug("counters container not found in reply");
72                         }
73                 } else {
74                         debug("headlines_callback: returned no XML object");
75                         f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
76                 }
77
78                 if (typeof correctPNG != 'undefined') {
79                         correctPNG();
80                 }
81
82                 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
83
84                 if (!document.getElementById("headlinesList") && 
85                                 getInitParam("cdm_auto_catchup") == 1) {
86                         debug("starting CDM watchdog");
87                         _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
88                         _cdm_wd_vishist = new Array();
89                 } else {
90                         debug("not in CDM mode or watchdog disabled");
91                 }
92
93                 if (_tag_cdm_scroll) {
94                         try {
95                                 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
96                                 _tag_cdm_scroll = false;
97                         } catch (e) { }
98                 }
99
100                 notify("");
101         }
102 }
103
104 function render_article(article) {
105         try {
106                 var f = document.getElementById("content-frame");
107                 try {
108                         f.scrollTop = 0;
109                 } catch (e) { };
110
111                 f.innerHTML = article;
112
113         } catch (e) {
114                 exception_error("render_article", e);
115         }
116 }
117
118 function article_callback() {
119         if (xmlhttp.readyState == 4) {
120                 debug("article_callback");
121
122                 try {
123                         if (xmlhttp.responseXML) {
124                                 var reply = xmlhttp.responseXML.firstChild.firstChild;
125
126                                 var articles = xmlhttp.responseXML.getElementsByTagName("article");
127
128                                 for (var i = 0; i < articles.length; i++) {
129                                         var a_id = articles[i].getAttribute("id");
130
131                                         debug("found id: " + a_id);
132
133                                         if (a_id == active_post_id) {
134                                                 debug("active article, rendering...");                                  
135                                                 render_article(articles[i].firstChild.nodeValue);
136                                         }
137
138                                         cache_inject(a_id, articles[i].firstChild.nodeValue);
139                                 }
140                         
141                         } else {
142                                 debug("article_callback: returned no XML object");
143                                 var f = document.getElementById("content-frame");
144                                 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
145                         }
146                 } catch (e) {
147                         exception_error("article_callback", e);
148                 }
149
150                 var date = new Date();
151                 last_article_view = date.getTime() / 1000;
152
153                 if (typeof correctPNG != 'undefined') {
154                         correctPNG();
155                 }
156
157                 if (_reload_feedlist_after_view) {
158                         setTimeout('updateFeedList(false, false)', 50);                 
159                         _reload_feedlist_after_view = false;
160                 } else {
161                         var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
162
163                         if (counters) {
164                                 debug("parsing piggybacked counters: " + counters);
165                                 parse_counters(counters, false);
166                         } else {
167                                 debug("counters container not found in reply");
168                         }
169                 }
170
171                 notify("");
172         }
173 }
174
175 function view(id, feed_id, skip_history) {
176         
177         try {
178                 debug("loading article: " + id + "/" + feed_id);
179
180                 active_real_feed_id = feed_id;
181
182                 var cached_article = cache_find(id);
183
184                 debug("cache check result: " + (cached_article != false));
185
186 /*              if (!skip_history) {
187                         history_push("ARTICLE:" + id + ":" + feed_id);
188                 } */
189         
190                 enableHotkeys();
191         
192                 //setActiveFeedId(feed_id);
193
194                 var query = "backend.php?op=view&id=" + param_escape(id) +
195                         "&feed=" + param_escape(feed_id);
196
197                 var date = new Date();
198
199                 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
200                         debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
201                         xmlhttp.abort();
202                         if (is_safari()) {
203                                 xmlhttp_ctr = Ajax.getTransport();
204                         }
205                 }
206
207                 if (xmlhttp_ready(xmlhttp)) {
208
209                         active_post_id = id; 
210
211                         cleanSelected("headlinesList");
212
213                         var crow = document.getElementById("RROW-" + active_post_id);
214
215                         var article_is_unread = crow.className.match("Unread");
216                         debug("article is unread: " + article_is_unread);                       
217
218                         crow.className = crow.className.replace("Unread", "");
219
220                         var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
221
222                         if (upd_img_pic) {
223                                 upd_img_pic.src = "images/blank_icon.gif";
224                         }
225
226                         selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
227                         markHeadline(active_post_id);
228
229                         var neighbor_ids = getRelativePostIds(active_post_id);
230
231                         /* only request uncached articles */
232
233                         var cids_to_request = Array();
234
235                         for (var i = 0; i < neighbor_ids.length; i++) {
236                                 if (!cache_check(neighbor_ids[i])) {
237                                         cids_to_request.push(neighbor_ids[i]);
238                                 }
239                         }
240
241                         debug("additional ids: " + cids_to_request.toString());                 
242
243                         /* additional info for piggyback counters */
244
245                         if (tagsAreDisplayed()) {
246                                 query = query + "&omode=lt";
247                         } else {
248                                 query = query + "&omode=flc";
249                         }
250
251                         var date = new Date();
252                         var timestamp = Math.round(date.getTime() / 1000);
253                         query = query + "&ts=" + timestamp;
254
255                         query = query + "&cids=" + cids_to_request.toString();
256
257                         if (!cached_article) {
258
259                                 notify_progress("Loading, please wait...");
260
261                                 debug(query);
262
263                                 xmlhttp.open("GET", query, true);
264                                 xmlhttp.onreadystatechange=article_callback;
265                                 xmlhttp.send(null);
266                         } else if (cached_article && article_is_unread) {
267
268                                 query = query + "&mode=prefetch";
269
270                                 debug(query);
271
272                                 xmlhttp.open("GET", query, true);
273                                 xmlhttp.onreadystatechange=article_callback;
274                                 xmlhttp.send(null);
275
276                                 render_article(cached_article);
277
278                         } else if (cached_article) {
279
280                                 query = query + "&mode=prefetch_old";
281
282                                 debug(query);
283
284                                 xmlhttp.open("GET", query, true);
285                                 xmlhttp.onreadystatechange=article_callback;
286                                 xmlhttp.send(null);
287
288                                 render_article(cached_article);
289
290                         }
291
292                         cache_expire();
293
294                 } else {
295                         debug("xmlhttp busy (@view)");
296                         printLockingError();
297                 }  
298
299         } catch (e) {
300                 exception_error("view", e);
301         }
302 }
303
304 function tMark(id) {
305         return toggleMark(id);
306 }
307
308 function toggleMark(id) {
309
310         if (!xmlhttp_ready(xmlhttp_rpc)) {
311                 printLockingError();
312                 return;
313         }
314
315         var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
316
317         var mark_img = document.getElementById("FMPIC-" + id);
318         var vfeedu = document.getElementById("FEEDU--1");
319         var crow = document.getElementById("RROW-" + id);
320
321         if (mark_img.alt != "Reset mark") {
322                 mark_img.src = "images/mark_set.png";
323                 mark_img.alt = "Reset mark";
324                 query = query + "&mark=1";
325
326                 if (vfeedu && crow.className.match("Unread")) {
327                         vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
328                 }
329
330         } else {
331                 mark_img.src = "images/mark_unset.png";
332                 mark_img.alt = "Set mark";
333                 query = query + "&mark=0";
334
335                 if (vfeedu && crow.className.match("Unread")) {
336                         vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
337                 }
338
339         }
340
341         var vfeedctr = document.getElementById("FEEDCTR--1");
342         var vfeedr = document.getElementById("FEEDR--1");
343
344         if (vfeedu && vfeedctr) {
345                 if ((+vfeedu.innerHTML) > 0) {
346                         if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
347                                 vfeedr.className = vfeedr.className + "Unread";
348                                 vfeedctr.className = "odd";
349                         }
350                 } else {
351                         vfeedctr.className = "invisible";
352                         vfeedr.className = vfeedr.className.replace("Unread", "");
353                 }
354         }
355
356         debug("toggle starred for aid " + id);
357
358         new Ajax.Request(query);
359
360 }
361
362 function correctHeadlinesOffset(id) {
363         
364         try {
365
366                 var hlist = document.getElementById("headlinesList");
367                 var container = document.getElementById("headlinesInnerContainer");
368                 var row = document.getElementById("RROW-" + id);
369         
370                 var viewport = container.offsetHeight;
371         
372                 var rel_offset_top = row.offsetTop - container.scrollTop;
373                 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
374         
375                 debug("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
376                 debug("Vport: " + viewport);
377
378                 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
379                         container.scrollTop = row.offsetTop;
380                 } else if (rel_offset_bottom > viewport) {
381
382                         /* doesn't properly work with Opera in some cases because
383                                 Opera fucks up element scrolling */
384
385                         container.scrollTop = row.offsetTop + row.offsetHeight - viewport;              
386                 } 
387
388         } catch (e) {
389                 exception_error("correctHeadlinesOffset", e);
390         }
391
392 }
393
394 function moveToPost(mode) {
395
396         // check for combined mode
397         if (!document.getElementById("headlinesList"))
398                 return;
399
400         var rows = getVisibleHeadlineIds();
401
402         var prev_id = false;
403         var next_id = false;
404
405         if (!document.getElementById('RROW-' + active_post_id)) {
406                 active_post_id = false;
407         }
408
409         if (active_post_id == false) {
410                 next_id = getFirstVisibleHeadlineId();
411                 prev_id = getLastVisibleHeadlineId();
412         } else {        
413                 for (var i = 0; i < rows.length; i++) {
414                         if (rows[i] == active_post_id) {
415                                 prev_id = rows[i-1];
416                                 next_id = rows[i+1];                    
417                         }
418                 }
419         }
420
421         if (mode == "next") {
422                 if (next_id) {
423                         correctHeadlinesOffset(next_id);
424                         view(next_id, getActiveFeedId());
425                 }
426         }
427
428         if (mode == "prev") {
429                 if (prev_id) {
430                         correctHeadlinesOffset(prev_id);
431                         view(prev_id, getActiveFeedId());
432                 }
433         } 
434 }
435
436 function toggleUnread(id, cmode) {
437         try {
438                 if (!xmlhttp_ready(xmlhttp_rpc)) {
439                         printLockingError();
440                         return;
441                 }
442         
443                 var row = document.getElementById("RROW-" + id);
444                 if (row) {
445                         var nc = row.className;
446                         nc = nc.replace("Unread", "");
447                         nc = nc.replace("Selected", "");
448
449                         if (row.className.match("Unread")) {
450                                 row.className = nc;
451                         } else {
452                                 row.className = nc + "Unread";
453                         }
454
455                         if (!cmode) cmode = 2;
456
457                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
458                                 param_escape(id) + "&cmode=" + param_escape(cmode);
459
460                         notify_progress("Loading, please wait...");
461
462                         xmlhttp_rpc.open("GET", query, true);
463                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
464                         xmlhttp_rpc.send(null);
465
466                 }
467
468
469         } catch (e) {
470                 exception_error("toggleUnread", e);
471         }
472 }
473
474 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
475         try {
476                 if (!xmlhttp_ready(xmlhttp_rpc)) {
477                         printLockingError();
478                         return;
479                 }
480         
481                 var rows;
482
483                 if (cdm_mode) {
484                         rows = cdmGetSelectedArticles();
485                 } else {        
486                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
487                 }
488
489                 if (rows.length == 0 && !no_error) {
490                         alert(__("No articles are selected."));
491                         return;
492                 }
493
494                 for (i = 0; i < rows.length; i++) {
495                         var row = document.getElementById("RROW-" + rows[i]);
496                         if (row) {
497                                 var nc = row.className;
498                                 nc = nc.replace("Unread", "");
499                                 nc = nc.replace("Selected", "");
500
501                                 if (row.className.match("Unread")) {
502                                         row.className = nc + "Selected";
503                                 } else {
504                                         row.className = nc + "UnreadSelected";
505                                 }
506                         }
507                 }
508
509                 if (rows.length > 0) {
510
511                         var cmode = "";
512
513                         if (set_state == undefined) {
514                                 cmode = "2";
515                         } else if (set_state == true) {
516                                 cmode = "1";
517                         } else if (set_state == false) {
518                                 cmode = "0";
519                         }
520
521                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
522                                 param_escape(rows.toString()) + "&cmode=" + cmode;
523
524                         _catchup_callback_func = callback_func;
525
526                         notify_progress("Loading, please wait...");
527
528                         xmlhttp_rpc.open("GET", query, true);
529                         xmlhttp_rpc.onreadystatechange=catchup_callback;
530                         xmlhttp_rpc.send(null);
531
532                 }
533
534         } catch (e) {
535                 exception_error("selectionToggleUnread", e);
536         }
537 }
538
539 function selectionToggleMarked(cdm_mode) {
540         try {
541                 if (!xmlhttp_ready(xmlhttp_rpc)) {
542                         printLockingError();
543                         return;
544                 }
545         
546                 var rows;
547                 
548                 if (cdm_mode) {
549                         rows = cdmGetSelectedArticles();
550                 } else {        
551                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
552                 }       
553
554                 if (rows.length == 0) {
555                         alert(__("No articles are selected."));
556                         return;
557                 }
558
559                 for (i = 0; i < rows.length; i++) {
560                         var row = document.getElementById("RROW-" + rows[i]);
561                         var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
562
563                         if (row && mark_img) {
564
565                                 if (mark_img.alt == "Set mark") {
566                                         mark_img.src = "images/mark_set.png";
567                                         mark_img.alt = "Reset mark";
568                                         mark_img.setAttribute('onclick', 
569                                                 'javascript:toggleMark('+rows[i]+', false)');
570
571                                 } else {
572                                         mark_img.src = "images/mark_unset.png";
573                                         mark_img.alt = "Set mark";
574                                         mark_img.setAttribute('onclick', 
575                                                 'javascript:toggleMark('+rows[i]+', true)');
576                                 }
577                         }
578                 }
579
580                 if (rows.length > 0) {
581
582                         var query = "backend.php?op=rpc&subop=markSelected&ids=" +
583                                 param_escape(rows.toString()) + "&cmode=2";
584
585                         xmlhttp_rpc.open("GET", query, true);
586                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
587                         xmlhttp_rpc.send(null);
588
589                 }
590
591         } catch (e) {
592                 exception_error("selectionToggleMarked", e);
593         }
594 }
595
596 function cdmGetSelectedArticles() {
597         var sel_articles = new Array();
598         var container = document.getElementById("headlinesInnerContainer");
599
600         for (i = 0; i < container.childNodes.length; i++) {
601                 var child = container.childNodes[i];
602
603                 if (child.id.match("RROW-") && child.className.match("Selected")) {
604                         var c_id = child.id.replace("RROW-", "");
605                         sel_articles.push(c_id);
606                 }
607         }
608
609         return sel_articles;
610 }
611
612 // mode = all,none,unread
613 function cdmSelectArticles(mode) {
614         var container = document.getElementById("headlinesInnerContainer");
615
616         for (i = 0; i < container.childNodes.length; i++) {
617                 var child = container.childNodes[i];
618
619                 if (child.id.match("RROW-")) {
620                         var aid = child.id.replace("RROW-", "");
621
622                         var cb = document.getElementById("RCHK-" + aid);
623
624                         if (mode == "all") {
625                                 if (!child.className.match("Selected")) {
626                                         child.className = child.className + "Selected";
627                                         cb.checked = true;
628                                 }
629                         } else if (mode == "unread") {
630                                 if (child.className.match("Unread") && !child.className.match("Selected")) {
631                                         child.className = child.className + "Selected";
632                                         cb.checked = true;
633                                 }
634                         } else {
635                                 child.className = child.className.replace("Selected", "");
636                                 cb.checked = false;
637                         }
638                 }               
639         }
640 }
641
642 function catchupPage() {
643
644         if (document.getElementById("headlinesList")) {
645                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
646                 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
647                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
648         } else {
649                 cdmSelectArticles('all');
650                 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
651                 cdmSelectArticles('none');
652         }
653 }
654
655 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
656
657         if (!xmlhttp_ready(xmlhttp_rpc)) {
658                 printLockingError();
659         }
660
661         var title = prompt("Please enter label title:", "");
662
663         if (title) {
664
665                 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
666                         "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
667                         "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) + 
668                         "&title=" + param_escape(title);
669
670                 debug("LFS: " + query);
671         
672                 xmlhttp_rpc.open("GET", query, true);
673                 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
674                 xmlhttp_rpc.send(null);
675         }
676
677 }
678
679 function editArticleTags(id, feed_id, cdm_enabled) {
680         _tag_active_post_id = id;
681         _tag_active_feed_id = feed_id;
682         _tag_active_cdm = cdm_enabled;
683
684         cache_invalidate(id);
685
686         try {
687                 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
688         } catch (e) { }
689         displayDlg('editArticleTags', id);
690 }
691
692
693 function tag_saved_callback() {
694         if (xmlhttp_rpc.readyState == 4) {
695                 try {
696                         debug("in tag_saved_callback");
697
698                         closeInfoBox();
699                         notify("");
700
701                         if (tagsAreDisplayed()) {
702                                 _reload_feedlist_after_view = true;
703                         }
704
705                         if (!_tag_active_cdm) {
706                                 if (active_post_id == _tag_active_post_id) {
707                                         debug("reloading current article");
708                                         view(_tag_active_post_id, _tag_active_feed_id);                 
709                                 }
710                         } else {
711                                 debug("reloading current feed");
712                                 viewCurrentFeed();
713                         }
714
715                 } catch (e) {
716                         exception_error("catchup_callback", e);
717                 }
718         }
719 }
720
721 function editTagsSave() {
722
723         if (!xmlhttp_ready(xmlhttp_rpc)) {
724                 printLockingError();
725         }
726
727         notify_progress("Saving article tags...");
728
729         var form = document.forms["tag_edit_form"];
730
731         var query = Form.serialize("tag_edit_form");
732
733         query = "backend.php?op=rpc&subop=setArticleTags&" + query;
734
735         debug(query);
736
737         xmlhttp_rpc.open("GET", query, true);                   
738         xmlhttp_rpc.onreadystatechange=tag_saved_callback;
739         xmlhttp_rpc.send(null);
740
741 }
742
743 function editTagsInsert() {
744         try {
745
746                 var form = document.forms["tag_edit_form"];
747
748                 var found_tags = form.found_tags;
749                 var tags_str = form.tags_str;
750
751                 var tag = found_tags[found_tags.selectedIndex].value;
752
753                 if (tags_str.value.length > 0 && 
754                                 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
755
756                         tags_str.value = tags_str.value + ", ";
757                 }
758
759                 tags_str.value = tags_str.value + tag + ", ";
760
761                 found_tags.selectedIndex = 0;
762                 
763         } catch (e) {
764                 exception_error(e, "editTagsInsert");
765         }
766 }
767
768 function cdmWatchdog() {
769
770         try {
771
772                 var ctr = document.getElementById("headlinesInnerContainer");
773
774                 if (!ctr) return;
775
776                 var ids = new Array();
777
778                 var e = ctr.firstChild;
779
780                 while (e) {
781                         if (e.className && e.className == "cdmArticleUnread" && e.id &&
782                                         e.id.match("RROW-")) {
783
784                                 // article fits in viewport OR article is longer than viewport and
785                                 // its bottom is visible
786
787                                 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
788                                                 ctr.scrollTop + ctr.offsetHeight) {
789
790 //                                      debug(e.id + " is visible " + e.offsetTop + "." + 
791 //                                              (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
792 //                                              (ctr.scrollTop + ctr.offsetHeight));
793
794                                         ids.push(e.id.replace("RROW-", ""));
795
796                                 } else if (e.offsetHeight > ctr.offsetHeight &&
797                                                 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
798                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
799
800                                         ids.push(e.id.replace("RROW-", "")); 
801
802                                 }
803
804                                 // method 2: article bottom is visible and is in upper 1/2 of the viewport
805
806 /*                              if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
807                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
808
809                                         ids.push(e.id.replace("RROW-", "")); 
810
811                                 } */
812
813                         }
814
815                         e = e.nextSibling;
816                 }
817
818                 debug("cdmWatchdog, ids= " + ids.toString());
819
820                 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
821
822                         for (var i = 0; i < ids.length; i++) {
823                                 var e = document.getElementById("RROW-" + ids[i]);
824                                 if (e) {
825                                         e.className = e.className.replace("Unread", "");
826                                 }
827                         }
828
829                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
830                                 param_escape(ids.toString()) + "&cmode=0";
831
832                         xmlhttp_rpc.open("GET", query, true);
833                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
834                         xmlhttp_rpc.send(null); 
835
836                 }
837
838                 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
839
840         } catch (e) {
841                 exception_error(e, "cdmWatchdog");
842         }
843
844 }
845
846
847 function cache_inject(id, article) {
848         if (!cache_check(id)) {
849                 debug("cache_article: miss: " + id);
850
851                 var cache_obj = new Array();
852
853                 cache_obj["id"] = id;
854                 cache_obj["data"] = article;
855
856                 article_cache.push(cache_obj);
857
858         } else {
859                 debug("cache_article: hit: " + id);
860         }
861 }
862
863 function cache_find(id) {
864         for (var i = 0; i < article_cache.length; i++) {
865                 if (article_cache[i]["id"] == id) {
866                         return article_cache[i]["data"];
867                 }
868         }
869         return false;
870 }
871
872 function cache_check(id) {
873         for (var i = 0; i < article_cache.length; i++) {
874                 if (article_cache[i]["id"] == id) {
875                         return true;
876                 }
877         }
878         return false;
879 }
880
881 function cache_expire() {
882         while (article_cache.length > 20) {
883                 article_cache.shift();
884         }
885 }
886
887 function cache_invalidate(id) {
888         var i = 0
889
890         try {   
891
892                 while (i < article_cache.length) {
893                         if (article_cache[i]["id"] == id) {
894                                 debug("cache_invalidate: removed id " + id);
895                                 article_cache.splice(i, 1);
896                                 return true;
897                         }
898                         i++;
899                 }
900                 debug("cache_invalidate: id not found: " + id);
901                 return false;
902         } catch (e) {
903                 exception_error("cache_invalidate", e);
904         }
905 }