]> git.wh0rd.org Git - tt-rss.git/blob - viewfeed.js
fix view() being called twice on every headline click, experimental progress indicato...
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var last_article_view = false;
3 var active_real_feed_id = false;
4
5 // FIXME: kludges, needs proper implementation
6 var _reload_feedlist_after_view = false;
7
8 var _cdm_wd_timeout = false;
9 var _cdm_wd_vishist = new Array();
10
11 var article_cache = new Array();
12
13 var vgroup_last_feed = false;
14 var post_under_pointer = false;
15
16 var last_requested_article = false;
17
18 function toggle_published_callback(transport) {
19         try {
20                 if (transport.responseXML) {
21
22                         all_counters_callback2(transport);
23
24                         var note = transport.responseXML.getElementsByTagName("note")[0];
25
26                         if (note) {
27                                 var note_id = note.getAttribute("id");
28                                 var note_size = note.getAttribute("size");
29                                 var note_content = note.firstChild.nodeValue;
30
31                                 var container = $('POSTNOTE-' + note_id);
32
33                                 cache_invalidate(note_id);
34
35                                 if (container) {
36                                         if (note_size == "0") {
37                                                 Element.hide(container);
38                                         } else {
39                                                 container.innerHTML = note_content;
40                                                 Element.show(container);
41                                         }
42                                 }
43                         }       
44                 }
45
46         } catch (e) {
47                 exception_error("toggle_published_callback", e, transport);
48         }
49 }
50
51 function catchup_callback2(transport, callback) {
52         try {
53                 debug("catchup_callback2 " + transport + ", " + callback);
54                 notify("");                     
55                 all_counters_callback2(transport);
56                 if (callback) {
57                         setTimeout(callback, 10);       
58                 }
59         } catch (e) {
60                 exception_error("catchup_callback2", e, transport);
61         }
62 }
63
64 function clean_feed_selections() {
65         try {
66                 var feeds = $("feedList").getElementsByTagName("LI");
67
68                 for (var i = 0; i < feeds.length; i++) {
69                         if (feeds[i].id && feeds[i].id.match("FEEDR-")) {
70                                 feeds[i].className = feeds[i].className.replace("Selected", "");
71                         }                       
72                         if (feeds[i].id && feeds[i].id.match("FCAT-")) {
73                                 feeds[i].className = feeds[i].className.replace("Selected", "");
74                         }
75                 }
76         } catch (e) {
77                 exception_error("clean_feed_selections", e);
78         }
79 }
80
81 function headlines_callback2(transport, feed_cur_page) {
82         try {
83
84                 if (!transport.responseText && db) {
85                         offlineConfirmModeChange();
86                         return;
87                 }
88
89                 loading_set_progress(100);
90
91                 debug("headlines_callback2 [page=" + feed_cur_page + "]");
92
93                 if (!transport_error_check(transport)) return;
94
95                 clean_feed_selections();
96         
97                 var is_cat = false;
98                 var feed_id = false;
99
100                 if (transport.responseXML) {
101                         var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
102                         if (headlines) {
103                                 is_cat = headlines.getAttribute("is_cat");
104                                 feed_id = headlines.getAttribute("id");
105                                 setActiveFeedId(feed_id, is_cat);
106                         }
107                 }
108
109                 var ll = $('FLL-' + feed_id);
110
111                 if (!is_cat) {
112                         var feedr = $("FEEDR-" + feed_id);
113                         if (feedr && !feedr.className.match("Selected")) {      
114                                 feedr.className = feedr.className + "Selected";
115                         } 
116                         if (feedr && ll) feedr.removeChild(ll);
117                 } else {
118                         var feedr = $("FCAT-" + feed_id);
119                         if (feedr && !feedr.className.match("Selected")) {      
120                                 feedr.className = feedr.className + "Selected";
121                         } 
122                         if (feedr && ll) feedr.removeChild(ll);
123
124                 }
125
126                 var f = $("headlines-frame");
127                 try {
128                         if (feed_cur_page == 0) { 
129                                 debug("resetting headlines scrollTop");
130                                 f.scrollTop = 0; 
131                         }
132                 } catch (e) { };
133         
134                 if (transport.responseXML) {
135                         var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
136                         var headlines_count_obj = transport.responseXML.getElementsByTagName("headlines-count")[0];
137                         var headlines_unread_obj = transport.responseXML.getElementsByTagName("headlines-unread")[0];
138                         var disable_cache_obj = transport.responseXML.getElementsByTagName("disable-cache")[0];
139
140                         var vgroup_last_feed_obj =  transport.responseXML.getElementsByTagName("vgroup-last-feed")[0];
141
142                         var headlines_count = headlines_count_obj.getAttribute("value");
143                         var headlines_unread = headlines_unread_obj.getAttribute("value");
144                         var disable_cache = disable_cache_obj.getAttribute("value") != "0";
145
146                         vgroup_last_feed = vgroup_last_feed_obj.getAttribute("value");
147
148                         if (headlines_count == 0) {
149                                 _infscroll_disable = 1;
150                         } else {
151                                 _infscroll_disable = 0;
152                         }
153
154                         var counters = transport.responseXML.getElementsByTagName("counters")[0];
155                         var articles = transport.responseXML.getElementsByTagName("article");
156                         var runtime_info = transport.responseXML.getElementsByTagName("runtime-info");
157         
158                         if (feed_cur_page == 0) {
159                                 if (headlines) {
160                                         f.innerHTML = headlines.firstChild.nodeValue;
161
162                                         var cache_prefix = "";
163
164                                         if (is_cat) {
165                                                 cache_prefix = "C:";
166                                         } else {
167                                                 cache_prefix = "F:";
168                                         }
169
170                                         cache_invalidate(cache_prefix + feed_id);
171
172                                         if (!disable_cache) {
173                                                 cache_inject(cache_prefix + feed_id,
174                                                         headlines.firstChild.nodeValue, headlines_unread);
175                                         }
176
177                                 } else {
178                                         debug("headlines_callback: returned no data");
179                                 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
180         
181                                 }
182                         } else {
183                                 if (headlines) {
184                                         if (headlines_count > 0) {
185                                                 debug("adding some more headlines...");
186         
187                                                 var c = $("headlinesList");
188                 
189                                                 if (!c) {
190                                                         c = $("headlinesInnerContainer");
191                                                 }
192
193                                                 var ids = getSelectedArticleIds2();
194         
195                                                 c.innerHTML = c.innerHTML + headlines.firstChild.nodeValue;
196
197                                                 debug("restore selected ids: " + ids);
198
199                                                 for (var i = 0; i < ids.length; i++) {
200                                                         markHeadline(ids[i]);
201                                                 }
202
203                                         } else {
204                                                 debug("no new headlines received");
205                                         }
206                                 } else {
207                                         debug("headlines_callback: returned no data");
208                                         notify_error("Error while trying to load more headlines");      
209                                 }
210
211                         }
212         
213                         if (articles) {
214                                 for (var i = 0; i < articles.length; i++) {
215                                         var a_id = articles[i].getAttribute("id");
216                                         debug("found id: " + a_id);
217                                         cache_inject(a_id, articles[i].firstChild.nodeValue);
218                                 }
219                         } else {
220                                 debug("no cached articles received");
221                         }
222         
223                         if (counters) {
224                                 debug("parsing piggybacked counters: " + counters);
225                                 parse_counters(counters, false);
226                         } else {
227                                 debug("counters container not found in reply, requesting...");
228                                 request_counters();
229                         }
230         
231                         if (runtime_info) {
232                                 debug("parsing runtime info: " + runtime_info[0]);
233                                 parse_runtime_info(runtime_info[0]);
234                         } else {
235                                 debug("counters container not found in reply");
236                         }
237         
238                 } else {
239                         debug("headlines_callback: returned no XML object");
240                         f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
241                 }
242         
243                 if (typeof correctPNG != 'undefined') {
244                         correctPNG();
245                 }
246         
247                 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
248         
249                 if (!$("headlinesList") && 
250                                 getActiveFeedId() != -3 &&
251                                 getInitParam("cdm_auto_catchup") == 1) {
252                         debug("starting CDM watchdog");
253                         _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
254                         _cdm_wd_vishist = new Array();
255                 } else {
256                         debug("not in CDM mode or watchdog disabled");
257                 }
258         
259                 _feed_cur_page = feed_cur_page;
260                 _infscroll_request_sent = 0;
261
262                 notify("");
263
264                 remove_splash();
265
266         } catch (e) {
267                 exception_error("headlines_callback2", e, transport);
268         }
269 }
270
271 function render_article(article) {
272         try {
273                 var f = $("content-frame");
274                 try {
275                         f.scrollTop = 0;
276                 } catch (e) { };
277
278                 var fi = $("content-insert");
279
280                 try {
281                         fi.scrollTop = 0;
282                 } catch (e) { };
283                 
284                 fi.innerHTML = article;
285                 
286 //              article.evalScripts();          
287
288         } catch (e) {
289                 exception_error("render_article", e);
290         }
291 }
292
293 function showArticleInHeadlines(id) {
294
295         try {
296
297                 cleanSelected("headlinesList");
298         
299                 var crow = $("RROW-" + id);
300
301                 if (!crow) return;
302
303                 var article_is_unread = crow.className.match("Unread");
304                         
305                 crow.className = crow.className.replace("Unread", "");
306
307                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
308         
309                 var upd_img_pic = $("FUPDPIC-" + id);
310
311                 var cache_prefix = "";
312                                 
313                 if (activeFeedIsCat()) {
314                         cache_prefix = "C:";
315                 } else {
316                         cache_prefix = "F:";
317                 }
318
319                 var view_mode = false;
320
321                 try {
322                         view_mode = document.forms['main_toolbar_form'].view_mode;      
323                         view_mode = view_mode[view_mode.selectedIndex].value;
324                 } catch (e) {
325                         //
326                 }
327
328                 if (upd_img_pic && (upd_img_pic.src.match("updated.png") || 
329                                         upd_img_pic.src.match("fresh_sign.png"))) {
330
331                         upd_img_pic.src = "images/blank_icon.gif";
332
333                         cache_invalidate(cache_prefix + getActiveFeedId());
334
335                         cache_inject(cache_prefix + getActiveFeedId(),
336                                 $("headlines-frame").innerHTML,
337                                 get_feed_unread(getActiveFeedId()));
338
339                 } else if (article_is_unread && view_mode == "all_articles") {
340
341                         cache_invalidate(cache_prefix + getActiveFeedId());
342
343                         cache_inject(cache_prefix + getActiveFeedId(),
344                                 $("headlines-frame").innerHTML,
345                                 get_feed_unread(getActiveFeedId())-1);
346
347                 } else if (article_is_unread) {
348                         cache_invalidate(cache_prefix + getActiveFeedId());
349                 }
350
351                 markHeadline(id);
352
353         } catch (e) {
354                 exception_error("showArticleInHeadlines", e);
355         }
356 }
357
358 function article_callback2(transport, id, feed_id) {
359         try {
360                 debug("article_callback2 " + id);
361
362                 if (!transport.responseText && db) {
363                         offlineConfirmModeChange();
364                         return;
365                 }
366
367                 if (transport.responseXML) {
368
369                         if (!transport_error_check(transport)) return;
370
371                         var ll = $('LL-' + id);
372                         var content = $('HLC-' + id);
373
374                         if (ll && content) content.removeChild(ll);
375                         
376                         if (id != last_requested_article) {
377                                 debug("requested article id is out of sequence, aborting");
378                                 return;
379                         }
380
381                         active_real_feed_id = feed_id;
382                         active_post_id = id; 
383
384                         debug("looking for articles to cache...");
385
386                         var articles = transport.responseXML.getElementsByTagName("article");
387
388                         for (var i = 0; i < articles.length; i++) {
389                                 var a_id = articles[i].getAttribute("id");
390
391                                 debug("found id: " + a_id);
392
393                                 if (a_id == active_post_id) {
394                                         debug("active article, rendering...");                                  
395                                         render_article(articles[i].firstChild.nodeValue);
396                                 }
397
398                                 cache_inject(a_id, articles[i].firstChild.nodeValue);
399                         }
400
401
402                         showArticleInHeadlines(id);     
403
404                         if (db) {
405                                 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
406                         }
407
408                         var reply = transport.responseXML.firstChild.firstChild;
409                 
410                 } else {
411                         debug("article_callback: returned no XML object");
412                         //var f = $("content-frame");
413                         //f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
414                 }
415
416                 var date = new Date();
417                 last_article_view = date.getTime() / 1000;
418
419                 if (typeof correctPNG != 'undefined') {
420                         correctPNG();
421                 }
422
423                 if (_reload_feedlist_after_view) {
424                         setTimeout('updateFeedList(false, false)', 50);                 
425                         _reload_feedlist_after_view = false;
426                 } else {
427                         if (transport.responseXML) {
428                                 var counters = transport.responseXML.getElementsByTagName("counters")[0];
429
430                                 if (counters) {
431                                         debug("parsing piggybacked counters: " + counters);
432                                         parse_counters(counters, false);
433                                 } else {
434                                         debug("counters container not found in reply, requesting...");
435                                         request_counters();
436                                 }
437                         }
438                 }
439
440                 notify("");
441         } catch (e) {
442                 exception_error("article_callback2", e, transport);
443         }
444 }
445
446 function view(id, feed_id, skip_history) {
447         
448         try {
449                 debug("loading article: " + id + "/" + feed_id);
450
451                 if (offline_mode) return view_offline(id, feed_id);
452
453                 var cached_article = cache_find(id);
454
455                 debug("cache check result: " + (cached_article != false));
456         
457                 enableHotkeys();
458         
459                 //setActiveFeedId(feed_id);
460
461                 var query = "backend.php?op=view&id=" + param_escape(id) +
462                         "&feed=" + param_escape(feed_id);
463
464                 var date = new Date();
465
466                 var neighbor_ids = getRelativePostIds(active_post_id);
467
468                 /* only request uncached articles */
469
470                 var cids_to_request = Array();
471
472                 for (var i = 0; i < neighbor_ids.length; i++) {
473                         if (!cache_check(neighbor_ids[i])) {
474                                 cids_to_request.push(neighbor_ids[i]);
475                         }
476                 }
477
478                 debug("additional ids: " + cids_to_request.toString());                 
479
480                 /* additional info for piggyback counters */
481
482                 if (tagsAreDisplayed()) {
483                         query = query + "&omode=lt";
484                 } else {
485                         query = query + "&omode=flc";
486                 }
487
488                 var date = new Date();
489                 var timestamp = Math.round(date.getTime() / 1000);
490                 query = query + "&ts=" + timestamp;
491
492                 query = query + "&cids=" + cids_to_request.toString();
493
494                 var crow = $("RROW-" + id);
495                 var article_is_unread = crow.className.match("Unread");
496
497                 if (!async_counters_work) {
498                         query = query + "&csync=true";
499                 }
500
501                 showArticleInHeadlines(id);
502
503                 if (!cached_article) {
504
505 //                      notify_progress("Loading, please wait...", true);
506
507                         var content = $('HLC-' + id);
508
509                         if (content && !$('LL-' + id)) {
510                                 var ll = document.createElement('img');
511                 
512                                 ll.src = 'images/indicator_tiny.gif';
513                                 ll.className = 'hlLoading';
514                                 ll.id = 'LL-' + id;
515
516                                 content.appendChild(ll);
517
518                         }
519
520                 } else if (cached_article && article_is_unread) {
521
522                         query = query + "&mode=prefetch";
523
524                         render_article(cached_article);
525
526                 } else if (cached_article) {
527
528                         query = query + "&mode=prefetch_old";
529                         render_article(cached_article);
530
531                 }
532
533                 cache_expire();
534
535                 last_requested_article = id;
536
537                 new Ajax.Request(query, {
538                         onComplete: function(transport) { 
539                                 article_callback2(transport, id, feed_id); 
540                         } });
541
542                 return false;
543
544         } catch (e) {
545                 exception_error("view", e);
546         }
547 }
548
549 function tMark(id) {
550         return toggleMark(id);
551 }
552
553 function tPub(id) {
554         return togglePub(id);
555 }
556
557 function tMark_afh_off(effect) {
558         try {
559                 var elem = effect.effects[0].element;
560
561                 debug("tMark_afh_off : " + elem.id);
562
563                 if (elem) {
564                         elem.src = elem.src.replace("mark_set", "mark_unset");
565                         elem.alt = __("Star article");
566                         Element.show(elem);
567                 }
568
569         } catch (e) {
570                 exception_error("tMark_afh_off", e);
571         }
572 }
573
574 function tPub_afh_off(effect) {
575         try {
576                 var elem = effect.effects[0].element;
577
578                 debug("tPub_afh_off : " + elem.id);
579
580                 if (elem) {
581                         elem.src = elem.src.replace("pub_set", "pub_unset");
582                         elem.alt = __("Publish article");
583                         Element.show(elem);
584                 }
585
586         } catch (e) {
587                 exception_error("tPub_afh_off", e);
588         }
589 }
590
591 function toggleMark(id, client_only, no_effects) {
592
593         try {
594
595                 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
596         
597                 query = query + "&afid=" + getActiveFeedId();
598         
599                 if (tagsAreDisplayed()) {
600                         query = query + "&omode=tl";
601                 } else {
602                         query = query + "&omode=flc";
603                 }
604         
605                 var mark_img = $("FMPIC-" + id);
606
607                 if (!mark_img) return;
608
609                 var vfeedu = $("FEEDU--1");
610                 var crow = $("RROW-" + id);
611         
612                 if (mark_img.src.match("mark_unset")) {
613                         mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
614                         mark_img.alt = __("Unstar article");
615                         query = query + "&mark=1";
616
617                         if (db) {
618                                 db.execute("UPDATE articles SET marked = 1 WHERE id = ?", [id]);
619                         }
620
621                 } else {
622                         //mark_img.src = "images/mark_unset.png";
623                         mark_img.alt = __("Please wait...");
624                         query = query + "&mark=0";
625         
626                         if ($("headlinesList") && !no_effects) {
627                                 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tMark_afh_off});
628                         } else { 
629                                 mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
630                                 mark_img.alt = __("Star article");
631                         }
632
633                         if (db) {
634                                 db.execute("UPDATE articles SET marked = 0 WHERE id = ?", [id]);
635                         }
636
637                 }
638
639                 if (!no_effects) update_local_feedlist_counters();
640
641                 if (!client_only) {
642                         debug(query);
643
644                         new Ajax.Request(query, {
645                                 onComplete: function(transport) { 
646                                         all_counters_callback2(transport); 
647                                 } });
648
649                 }
650
651         } catch (e) {
652                 exception_error("toggleMark", e);
653         }
654 }
655
656 function togglePub(id, client_only, no_effects, note) {
657
658         try {
659
660                 var query = "backend.php?op=rpc&id=" + id + "&subop=publ";
661         
662                 query = query + "&afid=" + getActiveFeedId();
663
664                 if (note != undefined) {
665                         query = query + "&note=" + param_escape(note);
666                 } else {
667                         query = query + "&note=undefined";
668                 }
669         
670                 if (tagsAreDisplayed()) {
671                         query = query + "&omode=tl";
672                 } else {
673                         query = query + "&omode=flc";
674                 }
675         
676                 var mark_img = $("FPPIC-" + id);
677
678                 if (!mark_img) return;
679
680                 var vfeedu = $("FEEDU--2");
681                 var crow = $("RROW-" + id);
682         
683                 if (mark_img.src.match("pub_unset") || note != undefined) {
684                         mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
685                         mark_img.alt = __("Unpublish article");
686                         query = query + "&pub=1";
687
688                 } else {
689                         //mark_img.src = "images/pub_unset.png";
690                         mark_img.alt = __("Please wait...");
691                         query = query + "&pub=0";
692         
693                         if ($("headlinesList") && !no_effects) {
694                                 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tPub_afh_off});
695                         } else { 
696                                 mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
697                                 mark_img.alt = __("Publish article");
698                         }
699                 }
700
701                 if (!client_only) {
702                         new Ajax.Request(query, {
703                                 onComplete: function(transport) { 
704                                         toggle_published_callback(transport); 
705                                 } });
706                 }
707
708         } catch (e) {
709
710                 exception_error("togglePub", e);
711         }
712 }
713
714 function correctHeadlinesOffset(id) {
715         
716         try {
717
718                 var hlist = $("headlinesList");
719                 var container = $("headlinesInnerContainer");
720                 var row = $("RROW-" + id);
721         
722                 var viewport = container.offsetHeight;
723         
724                 var rel_offset_top = row.offsetTop - container.scrollTop;
725                 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
726         
727                 debug("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
728                 debug("Vport: " + viewport);
729
730                 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
731                         container.scrollTop = row.offsetTop;
732                 } else if (rel_offset_bottom > viewport) {
733
734                         /* doesn't properly work with Opera in some cases because
735                                 Opera fucks up element scrolling */
736
737                         container.scrollTop = row.offsetTop + row.offsetHeight - viewport;              
738                 } 
739
740         } catch (e) {
741                 exception_error("correctHeadlinesOffset", e);
742         }
743
744 }
745
746 function moveToPost(mode) {
747
748         try {
749
750                 var rows;
751
752                 if (isCdmMode()) {
753                         rows = cdmGetVisibleArticles();
754                 } else {
755                         rows = getVisibleHeadlineIds();
756                 }
757                 
758                 var prev_id = false;
759                 var next_id = false;
760                 
761                 if (!$('RROW-' + active_post_id)) {
762                         active_post_id = false;
763                 }
764                 
765                 if (active_post_id == false) {
766                         next_id = getFirstVisibleHeadlineId();
767                         prev_id = getLastVisibleHeadlineId();
768                 } else {        
769                         for (var i = 0; i < rows.length; i++) {
770                                 if (rows[i] == active_post_id) {
771                                         prev_id = rows[i-1];
772                                         next_id = rows[i+1];                    
773                                 }
774                         }
775                 }
776                 
777                 if (mode == "next") {
778                         if (next_id) {
779                                 if (isCdmMode()) {
780         
781                                         if (!cdmArticleIsActuallyVisible(next_id)) {
782                                                 cdmScrollToArticleId(next_id);
783                                         }
784                                         cdmSelectArticles("none");
785                                         toggleUnread(next_id, 0, true);
786                                         toggleSelected(next_id);
787
788                                 } else {
789                                         correctHeadlinesOffset(next_id);
790                                         view(next_id, getActiveFeedId());
791                                 }
792                         }
793                 }
794                 
795                 if (mode == "prev") {
796                         if (prev_id) {
797                                 if (isCdmMode()) {
798                                         cdmScrollToArticleId(prev_id);
799                                         cdmSelectArticles("none");
800                                         toggleUnread(prev_id, 0, true);
801                                         toggleSelected(prev_id);
802                                 } else {
803                                         correctHeadlinesOffset(prev_id);
804                                         view(prev_id, getActiveFeedId());
805                                 }
806                         }
807                 } 
808
809         } catch (e) {
810                 exception_error("moveToPost", e);
811         }
812 }
813
814 function toggleSelected(id) {
815         try {
816         
817                 var cb = $("RCHK-" + id);
818
819                 var row = $("RROW-" + id);
820                 if (row) {
821                         var nc = row.className;
822                         
823                         if (!nc.match("Selected")) {
824                                 nc = nc + "Selected";
825                                 if (cb) {
826                                         cb.checked = true;
827                                 }
828
829                                 // In CDM basically last selected article == active article
830                                 if (isCdmMode()) active_post_id = id;
831                         } else {
832                                 nc = nc.replace("Selected", "");
833                                 if (cb) {
834                                         cb.checked = false;
835                                 }
836
837                         }
838
839                         row.className = nc;
840                 }
841         } catch (e) {
842                 exception_error("toggleSelected", e);
843         }
844 }
845
846 function toggleUnread_afh(effect) {
847         try {
848
849                 var elem = effect.element;
850                 elem.style.backgroundColor = "";
851
852         } catch (e) {
853                 exception_error("toggleUnread_afh", e);
854         }
855
856
857 function toggleUnread(id, cmode, effect) {
858         try {
859         
860                 var row = $("RROW-" + id);
861                 if (row) {
862                         var nc = row.className;
863                         var is_selected = row.className.match("Selected");
864                         nc = nc.replace("Unread", "");
865                         nc = nc.replace("Selected", "");
866
867                         // since we are removing selection from the object, uncheck
868                         // corresponding checkbox
869
870                         var cb = $("RCHK-" + id);
871                         if (cb) {
872                                 cb.checked = false;
873                         }
874
875                         // NOTE: I'm not sure that resetting selection here is a feature -fox
876
877                         if (cmode == undefined || cmode == 2) {
878                                 if (row.className.match("Unread")) {
879                                         row.className = nc;
880
881                                         if (effect) {
882                                                 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
883                                                         afterFinish: toggleUnread_afh,
884                                                         queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
885                                         } 
886
887                                 } else {
888                                         row.className = nc + "Unread";
889                                 }
890
891                                 if (db) {
892                                         db.execute("UPDATE articles SET unread = not unread "+
893                                                 "WHERE id = ?", [id]);
894                                 }
895
896                         } else if (cmode == 0) {
897                                 row.className = nc;
898
899                                 if (effect) {
900                                         new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
901                                                 afterFinish: toggleUnread_afh,
902                                                 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
903                                 } 
904
905                                 if (db) {
906                                         db.execute("UPDATE articles SET unread = 0 "+
907                                                 "WHERE id = ?", [id]);
908                                 }
909
910                         } else if (cmode == 1) {
911                                 row.className = nc + "Unread";
912
913                                 if (db) {
914                                         db.execute("UPDATE articles SET unread = 1 "+
915                                                 "WHERE id = ?", [id]);
916                                 }
917
918                         }
919
920                         update_local_feedlist_counters();
921
922                         // Disable unmarking as selected for the time being (16.05.08) -fox
923                         if (is_selected) row.className = row.className + "Selected";
924
925                         if (cmode == undefined) cmode = 2;
926
927                         var query = "backend.php?op=rpc&subop=catchupSelected" +
928                                 "&cmode=" + param_escape(cmode);
929
930                         var ids = "?ids=" + param_escape(id);
931
932 //                      notify_progress("Loading, please wait...");
933
934                         new Ajax.Request(query, {
935                                 parameters: ids,
936                                 onComplete: function(transport) { 
937                                         all_counters_callback2(transport); 
938                                 } });
939
940                 }
941
942         } catch (e) {
943                 exception_error("toggleUnread", e);
944         }
945 }
946
947 function selectionRemoveLabel(id) {
948         try {
949
950                 var ids = getSelectedArticleIds2();
951
952                 if (ids.length == 0) {
953                         alert(__("No articles are selected."));
954                         return;
955                 }
956
957 //              var ok = confirm(__("Remove selected articles from label?"));
958
959 //              if (ok) {
960
961                         var query = "backend.php?op=rpc&subop=removeFromLabel&ids=" +
962                                 param_escape(ids.toString()) + "&lid=" + param_escape(id);
963
964 //                      notify_progress("Loading, please wait...");
965
966                         cache_invalidate("F:" + (-11 - id));
967
968                         new Ajax.Request(query, {
969                                 onComplete: function(transport) { 
970                                         show_labels_in_headlines(transport);
971                                         all_counters_callback2(transport);
972                                 } });
973
974 //              }
975
976         } catch (e) {
977                 exception_error("selectionAssignLabel", e);
978
979         }
980 }
981
982 function selectionAssignLabel(id) {
983         try {
984
985                 var ids = getSelectedArticleIds2();
986
987                 if (ids.length == 0) {
988                         alert(__("No articles are selected."));
989                         return;
990                 }
991
992 //              var ok = confirm(__("Assign selected articles to label?"));
993
994 //              if (ok) {
995
996                         cache_invalidate("F:" + (-11 - id));
997
998                         var query = "backend.php?op=rpc&subop=assignToLabel&ids=" +
999                                 param_escape(ids.toString()) + "&lid=" + param_escape(id);
1000
1001 //                      notify_progress("Loading, please wait...");
1002
1003                         new Ajax.Request(query, {
1004                                 onComplete: function(transport) { 
1005                                         show_labels_in_headlines(transport);
1006                                         all_counters_callback2(transport);
1007                                 } });
1008
1009 //              }
1010
1011         } catch (e) {
1012                 exception_error("selectionAssignLabel", e);
1013
1014         }
1015 }
1016
1017 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
1018         try {
1019                 var rows;
1020
1021                 if (cdm_mode) {
1022                         rows = cdmGetSelectedArticles();
1023                 } else {        
1024                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1025                 }
1026
1027                 if (rows.length == 0 && !no_error) {
1028                         alert(__("No articles are selected."));
1029                         return;
1030                 }
1031
1032                 for (i = 0; i < rows.length; i++) {
1033                         var row = $("RROW-" + rows[i]);
1034                         if (row) {
1035                                 var nc = row.className;
1036                                 nc = nc.replace("Unread", "");
1037                                 nc = nc.replace("Selected", "");
1038
1039                                 if (set_state == undefined) {
1040                                         if (row.className.match("Unread")) {
1041                                                 row.className = nc + "Selected";
1042                                         } else {
1043                                                 row.className = nc + "UnreadSelected";
1044                                         }
1045                                         if (db) {
1046                                                 db.execute("UPDATE articles SET unread = NOT unread WHERE id = ?", 
1047                                                         [rows[i]]);
1048                                         }
1049                                 }
1050
1051                                 if (set_state == false) {
1052                                         row.className = nc + "Selected";
1053                                         if (db) {
1054                                                 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", 
1055                                                         [rows[i]]);
1056                                         }
1057                                 }
1058
1059                                 if (set_state == true) {
1060                                         row.className = nc + "UnreadSelected";
1061                                         if (db) {
1062                                                 db.execute("UPDATE articles SET unread = 1 WHERE id = ?", 
1063                                                         [rows[i]]);
1064                                         }
1065                                 }
1066                         }
1067                 }
1068
1069                 if (rows.length > 0) {
1070
1071                         update_local_feedlist_counters();
1072
1073                         var cmode = "";
1074
1075                         if (set_state == undefined) {
1076                                 cmode = "2";
1077                         } else if (set_state == true) {
1078                                 cmode = "1";
1079                         } else if (set_state == false) {
1080                                 cmode = "0";
1081                         }
1082
1083                         var query = "backend.php?op=rpc&subop=catchupSelected" +
1084                                 "&cmode=" + cmode;
1085
1086                         var ids = "?ids=" + param_escape(rows.toString()); 
1087
1088                         notify_progress("Loading, please wait...");
1089
1090                         new Ajax.Request(query, {
1091                                 parameters: ids,
1092                                 onComplete: function(transport) { 
1093                                         catchup_callback2(transport, callback_func); 
1094                                 } });
1095
1096                 }
1097
1098         } catch (e) {
1099                 exception_error("selectionToggleUnread", e);
1100         }
1101 }
1102
1103 function selectionToggleMarked(cdm_mode) {
1104         try {
1105         
1106                 var rows;
1107                 
1108                 if (cdm_mode) {
1109                         rows = cdmGetSelectedArticles();
1110                 } else {        
1111                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1112                 }       
1113
1114                 if (rows.length == 0) {
1115                         alert(__("No articles are selected."));
1116                         return;
1117                 }
1118
1119                 for (i = 0; i < rows.length; i++) {
1120                         toggleMark(rows[i], true, true);
1121                 }
1122
1123                 update_local_feedlist_counters();
1124
1125                 if (rows.length > 0) {
1126
1127                         var query = "backend.php?op=rpc&subop=markSelected&ids=" +
1128                                 param_escape(rows.toString()) + "&cmode=2";
1129
1130                         query = query + "&afid=" + getActiveFeedId();
1131
1132 /*                      if (tagsAreDisplayed()) {
1133                                 query = query + "&omode=tl";
1134                         } else {
1135                                 query = query + "&omode=flc";
1136                         } */
1137
1138                         query = query + "&omode=lc";
1139
1140                         new Ajax.Request(query, {
1141                                 onComplete: function(transport) { 
1142                                         all_counters_callback2(transport); 
1143                                 } });
1144
1145                 }
1146
1147         } catch (e) {
1148                 exception_error("selectionToggleMarked", e);
1149         }
1150 }
1151
1152 function selectionTogglePublished(cdm_mode) {
1153         try {
1154         
1155                 var rows;
1156                 
1157                 if (cdm_mode) {
1158                         rows = cdmGetSelectedArticles();
1159                 } else {        
1160                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1161                 }       
1162
1163                 if (rows.length == 0) {
1164                         alert(__("No articles are selected."));
1165                         return;
1166                 }
1167
1168                 for (i = 0; i < rows.length; i++) {
1169                         togglePub(rows[i], true, true);
1170                 }
1171
1172                 if (rows.length > 0) {
1173
1174                         var query = "backend.php?op=rpc&subop=publishSelected&ids=" +
1175                                 param_escape(rows.toString()) + "&cmode=2";
1176
1177                         query = query + "&afid=" + getActiveFeedId();
1178
1179 /*                      if (tagsAreDisplayed()) {
1180                                 query = query + "&omode=tl";
1181                         } else {
1182                                 query = query + "&omode=flc";
1183                         } */
1184
1185                         query = query + "&omode=lc";
1186
1187                         new Ajax.Request(query, {
1188                                 onComplete: function(transport) { 
1189                                         all_counters_callback2(transport); 
1190                                 } });
1191
1192                 }
1193
1194         } catch (e) {
1195                 exception_error("selectionToggleMarked", e);
1196         }
1197 }
1198
1199 function cdmGetSelectedArticles() {
1200         var sel_articles = new Array();
1201         var container = $("headlinesInnerContainer");
1202
1203         for (i = 0; i < container.childNodes.length; i++) {
1204                 var child = container.childNodes[i];
1205
1206                 if (child.id && child.id.match("RROW-") && child.className.match("Selected")) {
1207                         var c_id = child.id.replace("RROW-", "");
1208                         sel_articles.push(c_id);
1209                 }
1210         }
1211
1212         return sel_articles;
1213 }
1214
1215 function cdmGetVisibleArticles() {
1216         var sel_articles = new Array();
1217         var container = $("headlinesInnerContainer");
1218
1219         if (!container) return sel_articles;
1220
1221         for (i = 0; i < container.childNodes.length; i++) {
1222                 var child = container.childNodes[i];
1223
1224                 if (child.id && child.id.match("RROW-")) {
1225                         var c_id = child.id.replace("RROW-", "");
1226                         sel_articles.push(c_id);
1227                 }
1228         }
1229
1230         return sel_articles;
1231 }
1232
1233 function cdmGetUnreadArticles() {
1234         var sel_articles = new Array();
1235         var container = $("headlinesInnerContainer");
1236
1237         for (i = 0; i < container.childNodes.length; i++) {
1238                 var child = container.childNodes[i];
1239
1240                 if (child.id && child.id.match("RROW-") && child.className.match("Unread")) {
1241                         var c_id = child.id.replace("RROW-", "");
1242                         sel_articles.push(c_id);
1243                 }
1244         }
1245
1246         return sel_articles;
1247 }
1248
1249
1250 // mode = all,none,unread
1251 function cdmSelectArticles(mode) {
1252         var container = $("headlinesInnerContainer");
1253
1254         for (i = 0; i < container.childNodes.length; i++) {
1255                 var child = container.childNodes[i];
1256
1257                 if (child.id && child.id.match("RROW-")) {
1258                         var aid = child.id.replace("RROW-", "");
1259
1260                         var cb = $("RCHK-" + aid);
1261
1262                         if (mode == "all") {
1263                                 if (!child.className.match("Selected")) {
1264                                         child.className = child.className + "Selected";
1265                                         cb.checked = true;
1266                                 }
1267                         } else if (mode == "unread") {
1268                                 if (child.className.match("Unread") && !child.className.match("Selected")) {
1269                                         child.className = child.className + "Selected";
1270                                         cb.checked = true;
1271                                 }
1272                         } else {
1273                                 child.className = child.className.replace("Selected", "");
1274                                 cb.checked = false;
1275                         }
1276                 }               
1277         }
1278 }
1279
1280 function catchupPage() {
1281
1282         var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1283         
1284         var str = __("Mark all visible articles in %s as read?");
1285
1286         str = str.replace("%s", fn);
1287
1288         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1289                 return;
1290         }
1291
1292         if ($("headlinesList")) {
1293                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
1294                 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
1295                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
1296         } else {
1297                 cdmSelectArticles('all');
1298                 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
1299                 cdmSelectArticles('none');
1300         }
1301 }
1302
1303 function catchupSelection() {
1304
1305         try {
1306
1307                 var rows;
1308         
1309                 if ($("headlinesList")) {
1310                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1311                 } else {        
1312                         rows = cdmGetSelectedArticles();
1313                 }
1314         
1315                 if (rows.length == 0) {
1316                         alert(__("No articles are selected."));
1317                         return;
1318                 }
1319         
1320         
1321                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1322                 
1323                 var str = __("Mark %d selected articles in %s as read?");
1324         
1325                 str = str.replace("%d", rows.length);
1326                 str = str.replace("%s", fn);
1327         
1328                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1329                         return;
1330                 }
1331         
1332                 if ($("headlinesList")) {
1333                         selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
1334         //              selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
1335                 } else {
1336                         selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
1337         //              cdmSelectArticles('none');
1338                 }
1339
1340         } catch (e) {
1341                 exception_error("catchupSelection", e);
1342         }
1343 }
1344
1345 function editArticleTags(id, feed_id, cdm_enabled) {
1346         displayDlg('editArticleTags', id);
1347 }
1348
1349
1350 function tag_saved_callback(transport) {
1351         try {
1352                 debug("in tag_saved_callback");
1353
1354                 closeInfoBox();
1355                 notify("");
1356
1357                 if (tagsAreDisplayed()) {
1358                         _reload_feedlist_after_view = true;
1359                 }
1360
1361
1362                 if (transport.responseXML) {
1363                         var tags_str = transport.responseXML.getElementsByTagName("tags-str")[0];
1364                         
1365                         if (tags_str) {
1366                                 var id = tags_str.getAttribute("id");
1367
1368                                 if (id) {
1369                                         var tags = $("ATSTR-" + id);
1370                                         if (tags) {
1371                                                 tags.innerHTML = tags_str.firstChild.nodeValue;
1372                                         }
1373                                 }
1374                         }
1375                 }
1376
1377         } catch (e) {
1378                 exception_error("tag_saved_callback", e);
1379         }
1380 }
1381
1382 function editTagsSave() {
1383
1384         notify_progress("Saving article tags...");
1385
1386         var form = document.forms["tag_edit_form"];
1387
1388         var query = Form.serialize("tag_edit_form");
1389
1390         query = "backend.php?op=rpc&subop=setArticleTags&" + query;
1391
1392         debug(query);
1393
1394         new Ajax.Request(query, {
1395                 onComplete: function(transport) {
1396                                 tag_saved_callback(transport);
1397                         } });
1398
1399 }
1400
1401 function editTagsInsert() {
1402         try {
1403
1404                 var form = document.forms["tag_edit_form"];
1405
1406                 var found_tags = form.found_tags;
1407                 var tags_str = form.tags_str;
1408
1409                 var tag = found_tags[found_tags.selectedIndex].value;
1410
1411                 if (tags_str.value.length > 0 && 
1412                                 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
1413
1414                         tags_str.value = tags_str.value + ", ";
1415                 }
1416
1417                 tags_str.value = tags_str.value + tag + ", ";
1418
1419                 found_tags.selectedIndex = 0;
1420                 
1421         } catch (e) {
1422                 exception_error("editTagsInsert", e);
1423         }
1424 }
1425
1426 function cdmScrollViewport(where) {
1427         debug("cdmScrollViewport: " + where);
1428
1429         var ctr = $("headlinesInnerContainer");
1430
1431         if (!ctr) return;
1432
1433         if (where == "bottom") {
1434                 ctr.scrollTop = ctr.scrollHeight;
1435         } else {
1436                 ctr.scrollTop = where;
1437         }
1438 }
1439
1440 function cdmArticleIsBelowViewport(id) {
1441         try {
1442                 var ctr = $("headlinesInnerContainer");
1443                 var e = $("RROW-" + id);
1444
1445                 if (!e || !ctr) return;
1446
1447                 // article starts below viewport
1448
1449                 if (ctr.scrollTop < e.offsetTop) {
1450                         return true;
1451                 } else {        
1452                         return false;
1453                 }
1454
1455         } catch (e) {
1456                 exception_error("cdmArticleIsVisible", e);
1457         }
1458 }
1459
1460 function cdmArticleIsAboveViewport(id) {
1461         try {
1462                 var ctr = $("headlinesInnerContainer");
1463                 var e = $("RROW-" + id);
1464
1465                 if (!e || !ctr) return;
1466
1467                 // article starts above viewport
1468
1469                 if (ctr.scrollTop > e.offsetTop + e.offsetHeight) {
1470                         return true;
1471                 } else {        
1472                         return false;
1473                 }
1474
1475         } catch (e) {
1476                 exception_error("cdmArticleIsVisible", e);
1477         }
1478 }
1479
1480 function cdmScrollToArticleId(id) {
1481         try {
1482                 var ctr = $("headlinesInnerContainer");
1483                 var e = $("RROW-" + id);
1484
1485                 if (!e || !ctr) return;
1486
1487                 ctr.scrollTop = e.offsetTop;
1488
1489         } catch (e) {
1490                 exception_error("cdmScrollToArticleId", e);
1491         }
1492 }
1493
1494 function cdmArticleIsActuallyVisible(id) {
1495         try {
1496                 var ctr = $("headlinesInnerContainer");
1497                 var e = $("RROW-" + id);
1498
1499                 if (!e || !ctr) return;
1500
1501                 // article fits in viewport OR article is longer than viewport and
1502                 // its bottom is visible
1503
1504                 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1505                                 ctr.scrollTop + ctr.offsetHeight) {
1506
1507                         return true;
1508                 
1509                 } else if (e.offsetHeight > ctr.offsetHeight &&
1510                                 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1511                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1512
1513                         return true;
1514
1515                 }
1516
1517                 return false;
1518
1519         } catch (e) {
1520                 exception_error("cdmArticleIsVisible", e);
1521         }
1522 }
1523
1524 function cdmWatchdog() {
1525
1526         try {
1527
1528                 var ctr = $("headlinesInnerContainer");
1529
1530                 if (!ctr) return;
1531
1532                 var ids = new Array();
1533
1534                 var e = ctr.firstChild;
1535
1536                 while (e) {
1537                         if (e.className && e.className == "cdmArticleUnread" && e.id &&
1538                                         e.id.match("RROW-")) {
1539
1540                                 // article fits in viewport OR article is longer than viewport and
1541                                 // its bottom is visible
1542
1543                                 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1544                                                 ctr.scrollTop + ctr.offsetHeight) {
1545
1546 //                                      debug(e.id + " is visible " + e.offsetTop + "." + 
1547 //                                              (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
1548 //                                              (ctr.scrollTop + ctr.offsetHeight));
1549
1550                                         ids.push(e.id.replace("RROW-", ""));
1551
1552                                 } else if (e.offsetHeight > ctr.offsetHeight &&
1553                                                 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1554                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1555
1556                                         ids.push(e.id.replace("RROW-", "")); 
1557
1558                                 }
1559
1560                                 // method 2: article bottom is visible and is in upper 1/2 of the viewport
1561
1562 /*                              if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1563                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
1564
1565                                         ids.push(e.id.replace("RROW-", "")); 
1566
1567                                 } */
1568
1569                         }
1570
1571                         e = e.nextSibling;
1572                 }
1573
1574                 debug("cdmWatchdog, ids= " + ids.toString());
1575
1576                 if (ids.length > 0) {
1577
1578                         for (var i = 0; i < ids.length; i++) {
1579                                 var e = $("RROW-" + ids[i]);
1580                                 if (e) {
1581                                         e.className = e.className.replace("Unread", "");
1582                                 }
1583                         }
1584
1585                         var query = "backend.php?op=rpc&subop=catchupSelected" +
1586                                 "&cmode=0";
1587
1588                         var ids = "?ids=" + param_escape(ids.toString());
1589
1590                         new Ajax.Request(query, {
1591                                 parameters: ids,
1592                                 onComplete: function(transport) { 
1593                                         all_counters_callback2(transport); 
1594                                 } });
1595
1596                 }
1597
1598                 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
1599
1600         } catch (e) {
1601                 exception_error("cdmWatchdog", e);
1602         }
1603
1604 }
1605
1606
1607 function cache_inject(id, article, param) {
1608         try {
1609                 if (!cache_check_param(id, param)) {
1610                         debug("cache_article: miss: " + id + " [p=" + param + "]");
1611         
1612         
1613                         if (db) {
1614
1615                            var date = new Date();
1616                       var ts = Math.round(date.getTime() / 1000);
1617
1618                                 db.execute("INSERT INTO cache (id, article, param, added) VALUES (?, ?, ?, ?)",
1619                                         [id, article, param, ts]);                              
1620                         } else {
1621         
1622                                 var cache_obj = new Array();
1623         
1624                                 cache_obj["id"] = id;
1625                                 cache_obj["data"] = article;
1626                                 cache_obj["param"] = param;
1627         
1628                                 article_cache.push(cache_obj);
1629                         }
1630         
1631                 } else {
1632                         debug("cache_article: hit: " + id + " [p=" + param + "]");
1633                 }
1634         } catch (e) {   
1635                 exception_error("cache_inject", e);
1636         }
1637 }
1638
1639 function cache_find(id) {
1640
1641         if (db) {
1642                 var rs = db.execute("SELECT article FROM cache WHERE id = ?", [id]);
1643                 var a = false;
1644
1645                 if (rs.isValidRow()) {
1646                         var a = rs.field(0);                    
1647                 }
1648
1649                 rs.close();
1650
1651                 return a;
1652
1653         } else {
1654                 for (var i = 0; i < article_cache.length; i++) {
1655                         if (article_cache[i]["id"] == id) {
1656                                 return article_cache[i]["data"];
1657                         }
1658                 }
1659         }
1660         return false;
1661 }
1662
1663 function cache_find_param(id, param) {
1664
1665         if (db) {
1666                 var rs = db.execute("SELECT article FROM cache WHERE id = ? AND param = ?",
1667                         [id, param]);
1668                 var a = false;
1669
1670                 if (rs.isValidRow()) {
1671                         a = rs.field(0);
1672                 }
1673
1674                 rs.close();
1675
1676                 return a;
1677
1678         } else {
1679                 for (var i = 0; i < article_cache.length; i++) {
1680                         if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1681                                 return article_cache[i]["data"];
1682                         }
1683                 }
1684         }
1685         return false;
1686 }
1687
1688 function cache_check(id) {
1689
1690         if (db) {
1691                 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ?",
1692                         [id]);
1693                 var a = false;
1694
1695                 if (rs.isValidRow()) {
1696                          a = rs.field(0) != "0";
1697                 }
1698
1699                 rs.close();
1700
1701                 return a;
1702
1703         } else {
1704                 for (var i = 0; i < article_cache.length; i++) {
1705                         if (article_cache[i]["id"] == id) {
1706                                 return true;
1707                         }
1708                 }
1709         }
1710         return false;
1711 }
1712
1713 function cache_check_param(id, param) {
1714
1715         if (db) {
1716                 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ? AND param = ?",
1717                         [id, param]);
1718                 var a = false;
1719
1720                 if (rs.isValidRow()) {
1721                         a = rs.field(0) != "0";
1722                 }
1723
1724                 rs.close();
1725
1726                 return a;
1727
1728         } else {
1729                 for (var i = 0; i < article_cache.length; i++) {
1730                         if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1731                                 return true;
1732                         }
1733                 }
1734         }
1735         return false;
1736 }
1737
1738 function cache_expire() {
1739         if (db) {
1740                 var date = new Date();
1741                 var ts = Math.round(date.getTime() / 1000);
1742
1743                 db.execute("DELETE FROM cache WHERE added < ? - 1800 AND id LIKE 'FEEDLIST'", [ts]);
1744                 db.execute("DELETE FROM cache WHERE added < ? - 600 AND (id LIKE 'F:%' OR id LIKE 'C:%')", [ts]);
1745                 db.execute("DELETE FROM cache WHERE added < ? - 86400", [ts]);
1746
1747
1748         } else {
1749                 while (article_cache.length > 25) {
1750                         article_cache.shift();
1751                 }
1752         }
1753 }
1754
1755 function cache_empty() {
1756         article_cache = new Array();
1757 }
1758
1759 function cache_invalidate(id) {
1760         try {   
1761
1762                 if (db) {
1763                         rs = db.execute("DELETE FROM cache WHERE id = ?", [id]);
1764                         return rs.rowsAffected != 0;
1765                 } else {
1766
1767                         var i = 0
1768
1769                         while (i < article_cache.length) {
1770                                 if (article_cache[i]["id"] == id) {
1771                                         debug("cache_invalidate: removed id " + id);
1772                                         article_cache.splice(i, 1);
1773                                         return true;
1774                                 }
1775                                 i++;
1776                         }
1777                 }
1778
1779                 debug("cache_invalidate: id not found: " + id);
1780                 return false;
1781         } catch (e) {
1782                 exception_error("cache_invalidate", e);
1783         }
1784 }
1785
1786 function getActiveArticleId() {
1787         return active_post_id;
1788 }
1789
1790 function cdmClicked(id) {
1791         try {
1792                 var elem = $("RROW-" + id);
1793
1794                 if (elem) {
1795                         var id = elem.id.replace("RROW-", "");
1796                         active_post_id = id;
1797
1798 //                      cdmSelectArticles("none");
1799                         toggleUnread(id, 0, true);
1800 //                      toggleSelected(id);
1801
1802                 }
1803         } catch (e) {
1804                 exception_error("cdmClicked", e);
1805         } 
1806 }
1807
1808 function preload_article_callback(transport) {
1809         try {
1810                 if (transport.responseXML) {
1811                         var articles = transport.responseXML.getElementsByTagName("article");
1812
1813                         for (var i = 0; i < articles.length; i++) {
1814                                 var id = articles[i].getAttribute("id");
1815                                 if (!cache_check(id)) {
1816                                         cache_inject(id, articles[i].firstChild.nodeValue);                             
1817                                         debug("preloaded article: " + id);
1818                                 }
1819                         }
1820                 }
1821         } catch (e) {
1822                 exception_error("preload_article_callback", e);
1823         }
1824 }
1825
1826 function preloadArticleUnderPointer(id) {
1827         try {
1828                 if (getInitParam("bw_limit") == "1") return;
1829
1830                 if (post_under_pointer == id && !cache_check(id)) {
1831
1832                         debug("trying to preload article " + id);
1833
1834                         var neighbor_ids = getRelativePostIds(id, 1);
1835
1836                         /* only request uncached articles */
1837
1838                         var cids_to_request = Array();
1839
1840                         for (var i = 0; i < neighbor_ids.length; i++) {
1841                                 if (!cache_check(neighbor_ids[i])) {
1842                                         cids_to_request.push(neighbor_ids[i]);
1843                                 }
1844                         }
1845                         debug("additional ids: " + cids_to_request.toString());
1846
1847                         cids_to_request.push(id);
1848
1849                         var query = "backend.php?op=rpc&subop=getArticles&ids=" + 
1850                                 cids_to_request.toString();
1851                         new Ajax.Request(query, {
1852                                 onComplete: function(transport) { 
1853                                         preload_article_callback(transport);
1854                         } });
1855                 }
1856         } catch (e) {
1857                 exception_error("preloadArticleUnderPointer", e);
1858         }
1859 }
1860
1861 function postMouseIn(id) {
1862         try {
1863                 if (post_under_pointer != id) {
1864                         post_under_pointer = id;
1865                         if (!isCdmMode()) {
1866                                 window.setTimeout("preloadArticleUnderPointer(" + id + ")", 250);
1867                         }
1868                 }
1869
1870         } catch (e) {
1871                 exception_error("postMouseIn", e);
1872         }
1873 }
1874
1875 function postMouseOut(id) {
1876         try {
1877                 post_under_pointer = false;
1878         } catch (e) {
1879                 exception_error("postMouseOut", e);
1880         }
1881 }
1882
1883 function headlines_scroll_handler() {
1884         try {
1885
1886                 var e = $("headlinesInnerContainer");
1887
1888                 // don't do infinite scrolling when Limit == All
1889
1890                 var toolbar_form = document.forms["main_toolbar_form"];
1891
1892                 var limit = toolbar_form.limit[toolbar_form.limit.selectedIndex];
1893                 if (limit.value != 0) {
1894                 
1895                         debug((e.scrollTop + e.offsetHeight) + " vs " + e.scrollHeight + " dis? " +
1896                                 _infscroll_disable);
1897
1898                         if (e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
1899                                 if (!_infscroll_disable) {
1900                                         debug("more cowbell!");
1901                                         viewNextFeedPage();
1902                                 }
1903                         }
1904                 }
1905
1906         } catch (e) {
1907                 exception_error("headlines_scroll_handler", e);
1908         }
1909 }
1910
1911 function catchupRelativeToArticle(below) {
1912
1913         try {
1914
1915
1916                 if (!getActiveArticleId()) {
1917                         alert(__("No article is selected."));
1918                         return;
1919                 }
1920
1921                 var visible_ids;
1922
1923                 if ($("headlinesList")) {
1924                         visible_ids = getVisibleHeadlineIds();
1925                 } else {
1926                         visible_ids = cdmGetVisibleArticles();
1927                 }
1928
1929                 var ids_to_mark = new Array();
1930
1931                 if (!below) {
1932                         for (var i = 0; i < visible_ids.length; i++) {
1933                                 if (visible_ids[i] != getActiveArticleId()) {
1934                                         var e = $("RROW-" + visible_ids[i]);
1935
1936                                         if (e && e.className.match("Unread")) {
1937                                                 ids_to_mark.push(visible_ids[i]);
1938                                         }
1939                                 } else {
1940                                         break;
1941                                 }
1942                         }
1943                 } else {
1944                         for (var i = visible_ids.length-1; i >= 0; i--) {
1945                                 if (visible_ids[i] != getActiveArticleId()) {
1946                                         var e = $("RROW-" + visible_ids[i]);
1947
1948                                         if (e && e.className.match("Unread")) {
1949                                                 ids_to_mark.push(visible_ids[i]);
1950                                         }
1951                                 } else {
1952                                         break;
1953                                 }
1954                         }
1955                 }
1956
1957                 if (ids_to_mark.length == 0) {
1958                         alert(__("No articles found to mark"));
1959                 } else {
1960                         var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
1961
1962                         if (confirm(msg)) {
1963
1964                                 for (var i = 0; i < ids_to_mark.length; i++) {
1965                                         var e = $("RROW-" + ids_to_mark[i]);
1966                                         e.className = e.className.replace("Unread", "");
1967                                 }
1968
1969                                 var query = "backend.php?op=rpc&subop=catchupSelected" +
1970                                         "&cmode=0";
1971
1972                                 var ids = "?ids=" + param_escape(ids_to_mark.toString()); 
1973
1974                                 new Ajax.Request(query, {
1975                                         parameters: ids,
1976                                         onComplete: function(transport) { 
1977                                                 catchup_callback2(transport); 
1978                                         } });
1979
1980                         }
1981                 }
1982
1983         } catch (e) {
1984                 exception_error("catchupRelativeToArticle", e);
1985         }
1986 }
1987
1988 function cdmExpandArticle(a_id) {
1989         try {
1990                 var id = 'CICD-' + a_id;
1991
1992                 try {
1993                         Element.hide("CEXC-" + a_id);
1994                 } catch (e) { } 
1995
1996                 Effect.Appear(id, {duration : 0.5, 
1997                         beforeStart: function(effect) { 
1998                                 var h_id = 'CICH-' + a_id;
1999                                 var h_elem = $(h_id);
2000                                 if (h_elem) { h_elem.style.display = "none"; }
2001
2002                                 toggleUnread(a_id, 0);
2003                         }});
2004
2005
2006         } catch (e) {
2007                 exception_error("appearBlockElementF", e);
2008         }
2009
2010 }
2011
2012 function fixHeadlinesOrder(ids) {
2013         try {
2014                 for (var i = 0; i < ids.length; i++) {
2015                         var e = $("RROW-" + ids[i]);
2016
2017                         if (e) {
2018                                 if (i % 2 == 0) {
2019                                         e.className = e.className.replace("even", "odd");
2020                                 } else {
2021                                         e.className = e.className.replace("odd", "even");
2022                                 }
2023                         }
2024                 }
2025         } catch (e) {
2026                 exception_error("fixHeadlinesOrder", e);
2027         }
2028 }
2029
2030 function hideReadHeadlines() {
2031         try {
2032
2033                 var ids = false;
2034                 var vis_ids = new Array();
2035
2036                 if ($("headlinesList")) {
2037                         ids = getVisibleHeadlineIds();
2038                 } else {
2039                         ids = cdmGetVisibleArticles();
2040                 }
2041
2042                 var read_headlines_visible = true;
2043
2044                 for (var i = 0; i < ids.length; i++) {
2045                         var row = $("RROW-" + ids[i]);
2046
2047                         if (row && row.className) {
2048                                 if (read_headlines_visible) {
2049                                         if (row.className.match("Unread") || row.className.match("Selected")) {
2050                                                 Element.show(row);
2051                                                 vis_ids.push(ids[i]);
2052                                         } else {
2053                                                 //Effect.Fade(row, {duration : 0.3});
2054                                                 Element.hide(row);
2055                                         }
2056                                 } else {
2057                                         Element.show(row);
2058                                         vis_ids.push(ids[i]);
2059                                 }
2060                         }
2061                 }
2062                 
2063                 fixHeadlinesOrder(vis_ids);
2064
2065                 read_headlines_visible = !read_headlines_visible;
2066
2067         } catch (e) {
2068                 exception_error("hideReadHeadlines", e);
2069         } 
2070 }
2071
2072 function invertHeadlineSelection() {
2073         try {
2074                 var rows = new Array();
2075                 var r = false;
2076                 
2077                 if (!isCdmMode()) {             
2078                         r = document.getElementsByTagName("TR");
2079                 } else {
2080                         r = document.getElementsByTagName("DIV");
2081                 }
2082
2083                 for (var i = 0; i < r.length; i++) {
2084                         if (r[i].id && r[i].id.match("RROW-")) {
2085                                 rows.push(r[i]);
2086                         }
2087                 }
2088                 
2089                 for (var i = 0; i < rows.length; i++) {
2090                         var nc = rows[i].className;
2091                         var id = rows[i].id.replace("RROW-", "");
2092                         var cb = $("RCHK-" + id);
2093
2094                         if (!rows[i].className.match("Selected")) {
2095                                 nc = nc + "Selected";
2096                                 cb.checked = true;
2097                         } else {
2098                                 nc = nc.replace("Selected", "");
2099                                 cb.checked = false;
2100                         }
2101
2102                         rows[i].className = nc;
2103
2104                 }
2105
2106         } catch (e) {
2107                 exception_error("invertHeadlineSelection", e);
2108         }
2109 }
2110
2111 function getArticleUnderPointer() {
2112         return post_under_pointer;
2113 }
2114
2115 function zoomToArticle(id) {
2116         try {
2117                 var w = window.open("backend.php?op=view&mode=zoom&id=" + param_escape(id), 
2118                         "ttrss_zoom_" + id,
2119                         "status=0,toolbar=0,location=0,width=450,height=300,scrollbars=1,menubar=0");
2120
2121         } catch (e) {
2122                 exception_error("zoomToArticle", e);
2123         }
2124 }
2125
2126 function showOriginalArticleInline(id) {
2127         try {
2128
2129                 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
2130
2131                 notify_progress("Loading, please wait...", true);
2132
2133                 new Ajax.Request(query, {
2134                         onComplete: function(transport) { 
2135
2136                                 if (transport.responseXML) {
2137                         
2138                                         var link = transport.responseXML.getElementsByTagName("link")[0];
2139                                         var id = transport.responseXML.getElementsByTagName("id")[0];
2140
2141                                         notify("");
2142
2143                                         if (link && id) {
2144                                                 link = link.firstChild.nodeValue;
2145
2146                                                 var ci = $("content-insert");
2147
2148                                                 var tmp = "<iframe id=\"inline_orig_article\" width=\""+ci.offsetWidth+"\" height=\""+ci.offsetHeight+"\" style=\"border-width : 0px;\" src=\""+link+"\"></iframe>";
2149
2150                                                 render_article(tmp);
2151
2152                                         }
2153                                 }
2154                         } });
2155
2156         } catch (e) {
2157                 exception_error("showOriginalArticleInline", e);
2158         }
2159 }
2160
2161
2162 function scrollArticle(offset) {
2163         try {
2164                 if (!isCdmMode()) {
2165                         var ci = $("content-insert");
2166                         if (ci) {
2167                                 ci.scrollTop += offset;
2168                         }
2169                 } else {
2170                         var hi = $("headlinesInnerContainer");
2171                         if (hi) {
2172                                 hi.scrollTop += offset;
2173                         }
2174
2175                 }
2176         } catch (e) {
2177                 exception_error("scrollArticle", e);
2178         }
2179 }
2180
2181 function show_labels_in_headlines(transport) {
2182         try {
2183                 if (transport.responseXML) {
2184                         var info = transport.responseXML.getElementsByTagName("info-for-headlines")[0];
2185
2186                         var elems = info.getElementsByTagName("entry");
2187
2188                         for (var l = 0; l < elems.length; l++) {
2189                                 var e_id = elems[l].getAttribute("id");
2190
2191                                 if (e_id) {
2192
2193                                         var ctr = $("HLLCTR-" + e_id);
2194
2195                                         if (ctr) {
2196                                                 ctr.innerHTML = elems[l].firstChild.nodeValue;
2197                                         }
2198                                 }
2199
2200                         }
2201
2202                 }
2203         } catch (e) {
2204                 exception_error("show_labels_in_headlines", e);
2205
2206         }
2207 }
2208
2209 function toggleHeadlineActions() {
2210         try {
2211                 var e = $("headlineActionsBody");
2212                 var p = $("headlineActionsDrop");
2213
2214                 if (!Element.visible(e)) {
2215                         Element.show(e);
2216                 } else {
2217                         Element.hide(e);
2218                 }
2219
2220                 e.scrollTop = 0;
2221                 e.style.left = (p.offsetLeft + 1) + "px";
2222                 e.style.top = (p.offsetTop + p.offsetHeight + 2) + "px";
2223
2224         } catch (e) {
2225                 exception_error("toggleHeadlineActions", e);
2226         }
2227 }
2228
2229 function publishWithNote(id, def_note) {
2230         try {
2231                 if (!def_note) def_note = '';
2232
2233                 var note = prompt(__("Please enter a note for this article:"), def_note);
2234
2235                 if (note != undefined) {
2236                         togglePub(id, false, false, note);
2237                 }
2238
2239         } catch (e) {
2240                 exception_error("publishWithNote", e);
2241         }
2242 }