]> git.wh0rd.org Git - tt-rss.git/blob - js/viewfeed.js
enable tooltips on normal mode
[tt-rss.git] / js / viewfeed.js
1 var article_cache = new Array();
2
3 var _active_article_id = 0;
4
5 var vgroup_last_feed = false;
6 var post_under_pointer = false;
7
8 var last_requested_article = false;
9
10 var catchup_id_batch = [];
11 var catchup_timeout_id = false;
12
13 var cids_requested = [];
14 var loaded_article_ids = [];
15
16 var _post_preview_timeout = false;
17
18 var has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null;
19
20 function headlines_callback2(transport, offset, background, infscroll_req) {
21         try {
22                 handle_rpc_json(transport);
23
24                 loading_set_progress(25);
25
26                 console.log("headlines_callback2 [offset=" + offset + "] B:" + background + " I:" + infscroll_req);
27
28                 var is_cat = false;
29                 var feed_id = false;
30
31                 var reply = false;
32
33                 try {
34                         reply = JSON.parse(transport.responseText);
35                 } catch (e) {
36                         console.error(e);
37                 }
38
39                 if (reply) {
40
41                         is_cat = reply['headlines']['is_cat'];
42                         feed_id = reply['headlines']['id'];
43
44                         if (background) {
45                                 var content = reply['headlines']['content'];
46
47                                 if (getInitParam("cdm_auto_catchup") == 1) {
48                                         content = content + "<div id='headlines-spacer'></div>";
49                                 }
50                                 return;
51                         }
52
53                         setActiveFeedId(feed_id, is_cat);
54
55                         dijit.getEnclosingWidget(
56                                 document.forms["main_toolbar_form"].update).attr('disabled',
57                                         is_cat || feed_id <= 0);
58
59                         try {
60                                 if (infscroll_req == false) {
61                                         $("headlines-frame").scrollTop = 0;
62                                 }
63                         } catch (e) { };
64
65                         var headlines_count = reply['headlines-info']['count'];
66
67                         vgroup_last_feed = reply['headlines-info']['vgroup_last_feed'];
68
69                         if (parseInt(headlines_count) < getInitParam("default_article_limit")) {
70                                 _infscroll_disable = 1;
71                         } else {
72                                 _infscroll_disable = 0;
73                         }
74
75                         var counters = reply['counters'];
76                         var articles = reply['articles'];
77                         //var runtime_info = reply['runtime-info'];
78
79                         if (infscroll_req == false) {
80                                 loaded_article_ids = [];
81
82                                 dijit.byId("headlines-frame").attr('content',
83                                         reply['headlines']['content']);
84
85                                 dijit.byId("headlines-toolbar").attr('content',
86                                         reply['headlines']['toolbar']);
87
88                                 $$("#headlines-frame > div[id*=RROW]").each(function(row) {
89                                         if (loaded_article_ids.indexOf(row.id) != -1) {
90                                                 row.parentNode.removeChild(row);
91                                         } else {
92                                                 loaded_article_ids.push(row.id);
93                                         }
94                                 });
95
96                                 if (getInitParam("cdm_auto_catchup") == 1) {
97                                         var hsp = $("headlines-spacer");
98                                         if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"});
99                                         dijit.byId('headlines-frame').domNode.appendChild(hsp);
100                                 }
101
102                                 initHeadlinesMenu();
103
104                                 if (_search_query) {
105                                         $("feed_title").innerHTML += "<span id='cancel_search'>" +
106                                                 " (<a href='#' onclick='cancelSearch()'>" + __("Cancel search") + "</a>)" +
107                                                 "</span>";
108                                 }
109
110                         } else {
111
112                                 if (headlines_count > 0 && feed_id == getActiveFeedId() && is_cat == activeFeedIsCat()) {
113                                         console.log("adding some more headlines: " + headlines_count);
114
115                                         var c = dijit.byId("headlines-frame");
116                                         var ids = getSelectedArticleIds2();
117                                         var new_elems = [];
118
119                                         $("headlines-tmp").innerHTML = reply['headlines']['content'];
120
121                                         var hsp = $("headlines-spacer");
122
123                                         if (hsp)
124                                                 c.domNode.removeChild(hsp);
125
126                                         $$("#headlines-tmp > div").each(function(row) {
127                                                 if (row.className == 'cdmFeedTitle') {
128                                                         row.style.display = 'none';
129                                                         c.domNode.appendChild(row);
130                                                         new_elems.push(row);
131                                                 } else if (loaded_article_ids.indexOf(row.id) == -1) {
132                                                         row.style.display = 'none';
133                                                         c.domNode.appendChild(row);
134                                                         new_elems.push(row);
135                                                         loaded_article_ids.push(row.id);
136                                                 } else {
137                                                         row.parentNode.removeChild(row);
138                                                 }
139                                         });
140
141                                         if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"});
142
143                                         fixHeadlinesOrder(getLoadedArticleIds());
144
145                                         if (getInitParam("cdm_auto_catchup") == 1) {
146                                                 c.domNode.appendChild(hsp);
147                                         }
148
149                                         console.log("added " + new_elems.size() + " headlines");
150
151                                         if (new_elems.size() == 0)
152                                                 _infscroll_disable = true;
153
154                                         console.log("restore selected ids: " + ids);
155
156                                         for (var i = 0; i < ids.length; i++) {
157                                                 markHeadline(ids[i]);
158                                         }
159
160                                         initHeadlinesMenu();
161
162                                         new_elems.each(function(child) {
163                                                 var cb = dijit.byId(child.id.replace("RROW-", "RCHK-"));
164
165                                                 if (!cb) {
166                                                         dojo.parser.parse(child);
167
168                                                         if (!Element.visible(child))
169                                                                 new Effect.Appear(child, { duration : 0.5 });
170                                                 } else {
171                                                         c.domNode.removeChild(child);
172                                                 }
173                                         });
174
175                                 } else {
176                                         console.log("no new headlines received");
177
178                                         var hsp = $("headlines-spacer");
179
180                                         if (hsp) hsp.innerHTML = "";
181                                 }
182                         }
183
184                         if (articles) {
185                                 for (var i = 0; i < articles.length; i++) {
186                                         var a_id = articles[i]['id'];
187                                         cache_set("article:" + a_id, articles[i]['content']);
188                                 }
189                         } else {
190                                 console.log("no cached articles received");
191                         }
192
193                         if (counters)
194                                 parse_counters(counters);
195                         else
196                                 request_counters(true);
197
198                 } else if (transport.responseText) {
199                         console.error("Invalid object received: " + transport.responseText);
200                         dijit.byId("headlines-frame").attr('content', "<div class='whiteBox'>" +
201                                         __('Could not update headlines (invalid object received - see error console for details)') +
202                                         "</div>");
203                 } else {
204                         //notify_error("Error communicating with server.");
205                         Element.show(dijit.byId("net-alert").domNode);
206                 }
207
208                 _infscroll_request_sent = 0;
209
210                 unpackVisibleHeadlines();
211
212                 notify("");
213
214         } catch (e) {
215                 exception_error("headlines_callback2", e, transport);
216         }
217 }
218
219 function render_article(article) {
220         try {
221                 dijit.byId("headlines-wrap-inner").addChild(
222                                 dijit.byId("content-insert"));
223
224                 var c = dijit.byId("content-insert");
225
226                 try {
227                         c.domNode.scrollTop = 0;
228                 } catch (e) { };
229
230                 c.attr('content', article);
231
232                 correctHeadlinesOffset(getActiveArticleId());
233
234                 try {
235                         c.focus();
236                 } catch (e) { };
237
238         } catch (e) {
239                 exception_error("render_article", e);
240         }
241 }
242
243 function showArticleInHeadlines(id) {
244
245         try {
246
247                 selectArticles("none");
248
249                 var crow = $("RROW-" + id);
250
251                 if (!crow) return;
252
253                 var article_is_unread = crow.hasClassName("Unread");
254
255                 crow.removeClassName("Unread");
256
257                 selectArticles('none');
258
259                 var view_mode = false;
260
261                 try {
262                         view_mode = document.forms['main_toolbar_form'].view_mode;
263                         view_mode = view_mode[view_mode.selectedIndex].value;
264                 } catch (e) {
265                         //
266                 }
267
268                 markHeadline(id);
269
270                 if (article_is_unread)
271                         _force_scheduled_update = true;
272
273         } catch (e) {
274                 exception_error("showArticleInHeadlines", e);
275         }
276 }
277
278 function article_callback2(transport, id) {
279         try {
280                 console.log("article_callback2 " + id);
281
282                 handle_rpc_json(transport);
283
284                 var reply = false;
285
286                 try {
287                         reply = JSON.parse(transport.responseText);
288                 } catch (e) {
289                         console.error(e);
290                 }
291
292                 if (reply) {
293
294                         reply.each(function(article) {
295                                 if (getActiveArticleId() == article['id']) {
296                                         render_article(article['content']);
297                                 }
298                                 cids_requested.remove(article['id']);
299
300                                 cache_set("article:" + article['id'], article['content']);
301                         });
302
303 //                      if (id != last_requested_article) {
304 //                              console.log("requested article id is out of sequence, aborting");
305 //                              return;
306 //                      }
307
308                 } else if (transport.responseText) {
309                         console.error("Invalid object received: " + transport.responseText);
310
311                         render_article("<div class='whiteBox'>" +
312                                         __('Could not display article (invalid object received - see error console for details)') + "</div>");
313                 } else {
314                         Element.show(dijit.byId("net-alert").domNode);
315                 }
316
317                 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
318                 request_counters(unread_in_buffer == 0);
319
320                 //headlines_scroll_handler($("headlines-frame"));
321
322 /*              try {
323                         if (!_infscroll_disable &&
324                                         $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
325
326                                 loadMoreHeadlines();
327                         }
328                 } catch (e) {
329                         console.warn(e);
330                 } */
331
332                 notify("");
333         } catch (e) {
334                 exception_error("article_callback2", e, transport);
335         }
336 }
337
338 function view(id) {
339         try {
340                 var crow = $("RROW-" + id);
341
342                 if (!crow) return;
343
344                 console.log("loading article: " + id);
345
346                 var cached_article = cache_get("article:" + id);
347
348                 console.log("cache check result: " + (cached_article != false));
349
350                 hideAuxDlg();
351
352                 var query = "?op=article&method=view&id=" + param_escape(id);
353
354                 var neighbor_ids = getRelativePostIds(id);
355
356                 /* only request uncached articles */
357
358                 var cids_to_request = [];
359
360                 for (var i = 0; i < neighbor_ids.length; i++) {
361                         if (cids_requested.indexOf(neighbor_ids[i]) == -1)
362                                 if (!cache_get("article:" + neighbor_ids[i])) {
363                                         cids_to_request.push(neighbor_ids[i]);
364                                         cids_requested.push(neighbor_ids[i]);
365                                 }
366                 }
367
368                 console.log("additional ids: " + cids_to_request.toString());
369
370                 query = query + "&cids=" + cids_to_request.toString();
371
372                 var article_is_unread = crow.hasClassName("Unread");
373
374                 setActiveArticleId(id);
375                 showArticleInHeadlines(id);
376
377                 if (cached_article && article_is_unread) {
378
379                         query = query + "&mode=prefetch";
380
381                         render_article(cached_article);
382
383                 } else if (cached_article) {
384
385                         query = query + "&mode=prefetch_old";
386                         render_article(cached_article);
387
388                         // if we don't need to request any relative ids, we might as well skip
389                         // the server roundtrip altogether
390                         if (cids_to_request.length == 0) {
391
392 /*                              try {
393                                         if (!_infscroll_disable &&
394                                                 $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
395
396                                                         loadMoreHeadlines();
397                                         }
398                                 } catch (e) {
399                                         console.warn(e);
400                                 } */
401
402                                 //headlines_scroll_handler($("headlines-frame"));
403
404                                 return;
405                         }
406                 }
407
408                 last_requested_article = id;
409
410                 console.log(query);
411
412                 if (article_is_unread) {
413                         decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
414                 }
415
416                 new Ajax.Request("backend.php", {
417                         parameters: query,
418                         onComplete: function(transport) {
419                                 article_callback2(transport, id);
420                         } });
421
422                 return false;
423
424         } catch (e) {
425                 exception_error("view", e);
426         }
427 }
428
429 function toggleMark(id, client_only) {
430         try {
431                 var query = "?op=rpc&id=" + id + "&method=mark";
432
433                 var img = $("FMPIC-" + id);
434
435                 if (!img) return;
436
437                 if (img.src.match("mark_unset")) {
438                         img.src = img.src.replace("mark_unset", "mark_set");
439                         img.alt = __("Unstar article");
440                         query = query + "&mark=1";
441
442                 } else {
443                         img.src = img.src.replace("mark_set", "mark_unset");
444                         img.alt = __("Star article");
445                         query = query + "&mark=0";
446                 }
447
448                 if (!client_only) {
449                         new Ajax.Request("backend.php", {
450                                 parameters: query,
451                                 onComplete: function(transport) {
452                                         handle_rpc_json(transport);
453                                 } });
454                 }
455
456         } catch (e) {
457                 exception_error("toggleMark", e);
458         }
459 }
460
461 function togglePub(id, client_only, no_effects, note) {
462         try {
463                 var query = "?op=rpc&id=" + id + "&method=publ";
464
465                 if (note != undefined) {
466                         query = query + "&note=" + param_escape(note);
467                 } else {
468                         query = query + "&note=undefined";
469                 }
470
471                 var img = $("FPPIC-" + id);
472
473                 if (!img) return;
474
475                 if (img.src.match("pub_unset") || note != undefined) {
476                         img.src = img.src.replace("pub_unset", "pub_set");
477                         img.alt = __("Unpublish article");
478                         query = query + "&pub=1";
479
480                 } else {
481                         img.src = img.src.replace("pub_set", "pub_unset");
482                         img.alt = __("Publish article");
483
484                         query = query + "&pub=0";
485                 }
486
487                 if (!client_only) {
488                         new Ajax.Request("backend.php", {
489                                 parameters: query,
490                                 onComplete: function(transport) {
491                                         handle_rpc_json(transport);
492                                 } });
493                 }
494
495         } catch (e) {
496                 exception_error("togglePub", e);
497         }
498 }
499
500 function moveToPost(mode, noscroll) {
501
502         try {
503
504                 var rows = getVisibleArticleIds();
505
506                 var prev_id = false;
507                 var next_id = false;
508
509                 if (!$('RROW-' + getActiveArticleId())) {
510                         setActiveArticleId(0);
511                 }
512
513                 if (!getActiveArticleId()) {
514                         next_id = rows[0];
515                         prev_id = rows[rows.length-1]
516                 } else {
517                         for (var i = 0; i < rows.length; i++) {
518                                 if (rows[i] == getActiveArticleId()) {
519
520                                         // Account for adjacent identical article ids.
521                                         if (i > 0) prev_id = rows[i-1];
522
523                                         for (var j = i+1; j < rows.length; j++) {
524                                                 if (rows[j] != getActiveArticleId()) {
525                                                         next_id = rows[j];
526                                                         break;
527                                                 }
528                                         }
529                                         break;
530                                 }
531                         }
532                 }
533
534                 if (mode == "next") {
535                         if (next_id || getActiveArticleId()) {
536                                 if (isCdmMode()) {
537
538                                         var article = $("RROW-" + getActiveArticleId());
539                                         var ctr = $("headlines-frame");
540
541                                         if (!noscroll && article && article.offsetTop + article.offsetHeight >
542                                                         ctr.scrollTop + ctr.offsetHeight) {
543
544                                                 scrollArticle(ctr.offsetHeight/2);
545
546                                         } else if (next_id) {
547                                                 cdmExpandArticle(next_id);
548                                                 cdmScrollToArticleId(next_id, true);
549                                         }
550
551                                 } else if (next_id) {
552                                         correctHeadlinesOffset(next_id);
553                                         view(next_id, getActiveFeedId());
554                                 }
555                         }
556                 }
557
558                 if (mode == "prev") {
559                         if (prev_id || getActiveArticleId()) {
560                                 if (isCdmMode()) {
561
562                                         var article = $("RROW-" + getActiveArticleId());
563                                         var prev_article = $("RROW-" + prev_id);
564                                         var ctr = $("headlines-frame");
565
566                                         if (!getInitParam("cdm_expanded")) {
567
568                                                 if (!noscroll && article.offsetTop < ctr.scrollTop) {
569                                                         scrollArticle(-ctr.offsetHeight/3);
570                                                 } else {
571                                                         cdmExpandArticle(prev_id);
572                                                         cdmScrollToArticleId(prev_id, true);
573                                                 }
574                                         } else {
575
576                                                 if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
577                                                         scrollArticle(-ctr.offsetHeight/3);
578                                                 } else if (!noscroll && prev_article &&
579                                                                 prev_article.offsetTop < ctr.scrollTop) {
580                                                         cdmExpandArticle(prev_id);
581                                                         scrollArticle(-ctr.offsetHeight/3);
582                                                 } else if (prev_id) {
583                                                         cdmExpandArticle(prev_id);
584                                                         cdmScrollToArticleId(prev_id, noscroll);
585                                                 }
586                                         }
587
588                                 } else if (prev_id) {
589                                         correctHeadlinesOffset(prev_id);
590                                         view(prev_id, getActiveFeedId());
591                                 }
592                         }
593                 }
594
595         } catch (e) {
596                 exception_error("moveToPost", e);
597         }
598 }
599
600 function toggleSelected(id, force_on) {
601         try {
602
603                 var cb = dijit.byId("RCHK-" + id);
604                 var row = $("RROW-" + id);
605
606                 if (row) {
607                         if (row.hasClassName('Selected') && !force_on) {
608                                 row.removeClassName('Selected');
609                                 if (cb) cb.attr("checked", false);
610                         } else {
611                                 row.addClassName('Selected');
612                                 if (cb) cb.attr("checked", true);
613                         }
614                 }
615         } catch (e) {
616                 exception_error("toggleSelected", e);
617         }
618 }
619
620 function toggleUnread_afh(effect) {
621         try {
622
623                 var elem = effect.element;
624                 elem.style.backgroundColor = "";
625
626         } catch (e) {
627                 exception_error("toggleUnread_afh", e);
628         }
629 }
630
631 function toggleUnread(id, cmode, effect) {
632         try {
633
634                 var row = $("RROW-" + id);
635                 if (row) {
636                         if (cmode == undefined || cmode == 2) {
637                                 if (row.hasClassName("Unread")) {
638                                         row.removeClassName("Unread");
639
640                                         if (effect) {
641                                                 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
642                                                         afterFinish: toggleUnread_afh,
643                                                         queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
644                                         }
645
646                                 } else {
647                                         row.addClassName("Unread");
648                                 }
649
650                         } else if (cmode == 0) {
651
652                                 row.removeClassName("Unread");
653
654                                 if (effect) {
655                                         new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
656                                                 afterFinish: toggleUnread_afh,
657                                                 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
658                                 }
659
660                         } else if (cmode == 1) {
661                                 row.addClassName("Unread");
662                         }
663
664                         if (cmode == undefined) cmode = 2;
665
666                         var query = "?op=rpc&method=catchupSelected" +
667                                 "&cmode=" + param_escape(cmode) + "&ids=" + param_escape(id);
668
669 //                      notify_progress("Loading, please wait...");
670
671                         new Ajax.Request("backend.php", {
672                                 parameters: query,
673                                 onComplete: function(transport) {
674                                         handle_rpc_json(transport);
675                                 } });
676
677                 }
678
679         } catch (e) {
680                 exception_error("toggleUnread", e);
681         }
682 }
683
684 function selectionRemoveLabel(id, ids) {
685         try {
686
687                 if (!ids) ids = getSelectedArticleIds2();
688
689                 if (ids.length == 0) {
690                         alert(__("No articles are selected."));
691                         return;
692                 }
693
694                 var query = "?op=rpc&method=removeFromLabel&ids=" +
695                         param_escape(ids.toString()) + "&lid=" + param_escape(id);
696
697                 console.log(query);
698
699                 new Ajax.Request("backend.php", {
700                         parameters: query,
701                         onComplete: function(transport) {
702                                 handle_rpc_json(transport);
703                                 show_labels_in_headlines(transport);
704                         } });
705
706         } catch (e) {
707                 exception_error("selectionAssignLabel", e);
708
709         }
710 }
711
712 function selectionAssignLabel(id, ids) {
713         try {
714
715                 if (!ids) ids = getSelectedArticleIds2();
716
717                 if (ids.length == 0) {
718                         alert(__("No articles are selected."));
719                         return;
720                 }
721
722                 var query = "?op=rpc&method=assignToLabel&ids=" +
723                         param_escape(ids.toString()) + "&lid=" + param_escape(id);
724
725                 console.log(query);
726
727                 new Ajax.Request("backend.php", {
728                         parameters: query,
729                         onComplete: function(transport) {
730                                 handle_rpc_json(transport);
731                                 show_labels_in_headlines(transport);
732                         } });
733
734         } catch (e) {
735                 exception_error("selectionAssignLabel", e);
736
737         }
738 }
739
740 function selectionToggleUnread(set_state, callback, no_error) {
741         try {
742                 var rows = getSelectedArticleIds2();
743
744                 if (rows.length == 0 && !no_error) {
745                         alert(__("No articles are selected."));
746                         return;
747                 }
748
749                 for (var i = 0; i < rows.length; i++) {
750                         var row = $("RROW-" + rows[i]);
751                         if (row) {
752                                 if (set_state == undefined) {
753                                         if (row.hasClassName("Unread")) {
754                                                 row.removeClassName("Unread");
755                                         } else {
756                                                 row.addClassName("Unread");
757                                         }
758                                 }
759
760                                 if (set_state == false) {
761                                         row.removeClassName("Unread");
762                                 }
763
764                                 if (set_state == true) {
765                                         row.addClassName("Unread");
766                                 }
767                         }
768                 }
769
770                 if (rows.length > 0) {
771
772                         var cmode = "";
773
774                         if (set_state == undefined) {
775                                 cmode = "2";
776                         } else if (set_state == true) {
777                                 cmode = "1";
778                         } else if (set_state == false) {
779                                 cmode = "0";
780                         }
781
782                         var query = "?op=rpc&method=catchupSelected" +
783                                 "&cmode=" + cmode + "&ids=" + param_escape(rows.toString());
784
785                         notify_progress("Loading, please wait...");
786
787                         new Ajax.Request("backend.php", {
788                                 parameters: query,
789                                 onComplete: function(transport) {
790                                         handle_rpc_json(transport);
791                                         if (callback) callback(transport);
792                                 } });
793
794                 }
795
796         } catch (e) {
797                 exception_error("selectionToggleUnread", e);
798         }
799 }
800
801 function selectionToggleMarked() {
802         try {
803
804                 var rows = getSelectedArticleIds2();
805
806                 if (rows.length == 0) {
807                         alert(__("No articles are selected."));
808                         return;
809                 }
810
811                 for (var i = 0; i < rows.length; i++) {
812                         toggleMark(rows[i], true, true);
813                 }
814
815                 if (rows.length > 0) {
816
817                         var query = "?op=rpc&method=markSelected&ids=" +
818                                 param_escape(rows.toString()) + "&cmode=2";
819
820                         new Ajax.Request("backend.php", {
821                                 parameters: query,
822                                 onComplete: function(transport) {
823                                         handle_rpc_json(transport);
824                                 } });
825
826                 }
827
828         } catch (e) {
829                 exception_error("selectionToggleMarked", e);
830         }
831 }
832
833 function selectionTogglePublished() {
834         try {
835
836                 var rows = getSelectedArticleIds2();
837
838                 if (rows.length == 0) {
839                         alert(__("No articles are selected."));
840                         return;
841                 }
842
843                 for (var i = 0; i < rows.length; i++) {
844                         togglePub(rows[i], true, true);
845                 }
846
847                 if (rows.length > 0) {
848
849                         var query = "?op=rpc&method=publishSelected&ids=" +
850                                 param_escape(rows.toString()) + "&cmode=2";
851
852                         new Ajax.Request("backend.php", {
853                                 parameters: query,
854                                 onComplete: function(transport) {
855                                         handle_rpc_json(transport);
856                                 } });
857
858                 }
859
860         } catch (e) {
861                 exception_error("selectionToggleMarked", e);
862         }
863 }
864
865 function getSelectedArticleIds2() {
866
867         var rv = [];
868
869         $$("#headlines-frame > div[id*=RROW][class*=Selected]").each(
870                 function(child) {
871                         rv.push(child.id.replace("RROW-", ""));
872                 });
873
874         return rv;
875 }
876
877 function getLoadedArticleIds() {
878         var rv = [];
879
880         var children = $$("#headlines-frame > div[id*=RROW-]");
881
882         children.each(function(child) {
883                         rv.push(child.id.replace("RROW-", ""));
884                 });
885
886         return rv;
887
888 }
889
890 // mode = all,none,unread,invert,marked,published
891 function selectArticles(mode) {
892         try {
893
894                 var children = $$("#headlines-frame > div[id*=RROW]");
895
896                 children.each(function(child) {
897                         var id = child.id.replace("RROW-", "");
898                         var cb = dijit.byId("RCHK-" + id);
899
900                         if (mode == "all") {
901                                 child.addClassName("Selected");
902                                 if (cb) cb.attr("checked", true);
903                         } else if (mode == "unread") {
904                                 if (child.hasClassName("Unread")) {
905                                         child.addClassName("Selected");
906                                         if (cb) cb.attr("checked", true);
907                                 } else {
908                                         child.removeClassName("Selected");
909                                         if (cb) cb.attr("checked", false);
910                                 }
911                         } else if (mode == "marked") {
912                                 var img = $("FMPIC-" + child.id.replace("RROW-", ""));
913
914                                 if (img && img.src.match("mark_set")) {
915                                         child.addClassName("Selected");
916                                         if (cb) cb.attr("checked", true);
917                                 } else {
918                                         child.removeClassName("Selected");
919                                         if (cb) cb.attr("checked", false);
920                                 }
921                         } else if (mode == "published") {
922                                 var img = $("FPPIC-" + child.id.replace("RROW-", ""));
923
924                                 if (img && img.src.match("pub_set")) {
925                                         child.addClassName("Selected");
926                                         if (cb) cb.attr("checked", true);
927                                 } else {
928                                         child.removeClassName("Selected");
929                                         if (cb) cb.attr("checked", false);
930                                 }
931
932                         } else if (mode == "invert") {
933                                 if (child.hasClassName("Selected")) {
934                                         child.removeClassName("Selected");
935                                         if (cb) cb.attr("checked", false);
936                                 } else {
937                                         child.addClassName("Selected");
938                                         if (cb) cb.attr("checked", true);
939                                 }
940
941                         } else {
942                                 child.removeClassName("Selected");
943                                 if (cb) cb.attr("checked", false);
944                         }
945                 });
946
947         } catch (e) {
948                 exception_error("selectArticles", e);
949         }
950 }
951
952 function catchupPage() {
953
954         var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
955
956         var str = __("Mark all visible articles in %s as read?");
957
958         str = str.replace("%s", fn);
959
960         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
961                 return;
962         }
963
964         selectArticles('all');
965         selectionToggleUnread(false, 'viewCurrentFeed()', true);
966         selectArticles('none');
967 }
968
969 function deleteSelection() {
970
971         try {
972
973                 var rows = getSelectedArticleIds2();
974
975                 if (rows.length == 0) {
976                         alert(__("No articles are selected."));
977                         return;
978                 }
979
980                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
981                 var str;
982
983                 if (getActiveFeedId() != 0) {
984                         str = ngettext("Delete %d selected article in %s?", "Delete %d selected articles in %s?" , rows.length);
985                 } else {
986                         str = ngettext("Delete %d selected article?", "Delete %d selected articles?", rows.length);
987                 }
988
989                 str = str.replace("%d", rows.length);
990                 str = str.replace("%s", fn);
991
992                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
993                         return;
994                 }
995
996                 query = "?op=rpc&method=delete&ids=" + param_escape(rows);
997
998                 console.log(query);
999
1000                 new Ajax.Request("backend.php", {
1001                         parameters: query,
1002                         onComplete: function(transport) {
1003                                         handle_rpc_json(transport);
1004                                         viewCurrentFeed();
1005                                 } });
1006
1007         } catch (e) {
1008                 exception_error("deleteSelection", e);
1009         }
1010 }
1011
1012 function archiveSelection() {
1013
1014         try {
1015
1016                 var rows = getSelectedArticleIds2();
1017
1018                 if (rows.length == 0) {
1019                         alert(__("No articles are selected."));
1020                         return;
1021                 }
1022
1023                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1024                 var str;
1025                 var op;
1026
1027                 if (getActiveFeedId() != 0) {
1028                         str = ngettext("Archive %d selected article in %s?", "Archive %d selected articles in %s?", rows.length);
1029                         op = "archive";
1030                 } else {
1031                         str = ngettext("Move %d archived article back?", "Move %d archived articles back?", rows.length);
1032                         op = "unarchive";
1033                 }
1034
1035                 str = str.replace("%d", rows.length);
1036                 str = str.replace("%s", fn);
1037
1038                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1039                         return;
1040                 }
1041
1042                 query = "?op=rpc&method="+op+"&ids=" + param_escape(rows);
1043
1044                 console.log(query);
1045
1046                 for (var i = 0; i < rows.length; i++) {
1047                         cache_delete("article:" + rows[i]);
1048                 }
1049
1050                 new Ajax.Request("backend.php", {
1051                         parameters: query,
1052                         onComplete: function(transport) {
1053                                         handle_rpc_json(transport);
1054                                         viewCurrentFeed();
1055                                 } });
1056
1057         } catch (e) {
1058                 exception_error("archiveSelection", e);
1059         }
1060 }
1061
1062 function catchupSelection() {
1063
1064         try {
1065
1066                 var rows = getSelectedArticleIds2();
1067
1068                 if (rows.length == 0) {
1069                         alert(__("No articles are selected."));
1070                         return;
1071                 }
1072
1073                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1074
1075                 var str = ngettext("Mark %d selected article in %s as read?", "Mark %d selected articles in %s as read?", rows.length);
1076
1077                 str = str.replace("%d", rows.length);
1078                 str = str.replace("%s", fn);
1079
1080                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1081                         return;
1082                 }
1083
1084                 selectionToggleUnread(false, 'viewCurrentFeed()', true);
1085
1086         } catch (e) {
1087                 exception_error("catchupSelection", e);
1088         }
1089 }
1090
1091 function editArticleTags(id) {
1092                 var query = "backend.php?op=dlg&method=editArticleTags&param=" + param_escape(id);
1093
1094                 if (dijit.byId("editTagsDlg"))
1095                         dijit.byId("editTagsDlg").destroyRecursive();
1096
1097                 dialog = new dijit.Dialog({
1098                         id: "editTagsDlg",
1099                         title: __("Edit article Tags"),
1100                         style: "width: 600px",
1101                         execute: function() {
1102                                 if (this.validate()) {
1103                                         var query = dojo.objectToQuery(this.attr('value'));
1104
1105                                         notify_progress("Saving article tags...", true);
1106
1107                                         new Ajax.Request("backend.php", {
1108                                         parameters: query,
1109                                         onComplete: function(transport) {
1110                                                 notify('');
1111                                                 dialog.hide();
1112
1113                                                 var data = JSON.parse(transport.responseText);
1114
1115                                                 if (data) {
1116                                                         var tags_str = article.tags;
1117                                                         var id = tags_str.id;
1118
1119                                                         var tags = $("ATSTR-" + id);
1120                                                         var tooltip = dijit.byId("ATSTRTIP-" + id);
1121
1122                                                         if (tags) tags.innerHTML = tags_str.content;
1123                                                         if (tooltip) tooltip.attr('label', tags_str.content_full);
1124
1125                                                         cache_delete("article:" + id);
1126                                                 }
1127
1128                                         }});
1129                                 }
1130                         },
1131                         href: query,
1132                 });
1133
1134                 var tmph = dojo.connect(dialog, 'onLoad', function() {
1135                 dojo.disconnect(tmph);
1136
1137                         new Ajax.Autocompleter('tags_str', 'tags_choices',
1138                            "backend.php?op=rpc&method=completeTags",
1139                            { tokens: ',', paramName: "search" });
1140                 });
1141
1142                 dialog.show();
1143
1144 }
1145
1146 function cdmScrollToArticleId(id, force) {
1147         try {
1148                 var ctr = $("headlines-frame");
1149                 var e = $("RROW-" + id);
1150
1151                 if (!e || !ctr) return;
1152
1153                 if (force || e.offsetTop+e.offsetHeight > (ctr.scrollTop+ctr.offsetHeight) ||
1154                                 e.offsetTop < ctr.scrollTop) {
1155                         ctr.scrollTop = e.offsetTop;
1156                 }
1157
1158         } catch (e) {
1159                 exception_error("cdmScrollToArticleId", e);
1160         }
1161 }
1162
1163 function setActiveArticleId(id) {
1164         _active_article_id = id;
1165 }
1166
1167 function getActiveArticleId() {
1168         return _active_article_id;
1169 }
1170
1171 function postMouseIn(e, id) {
1172         post_under_pointer = id;
1173
1174         if (_post_preview_timeout) window.clearTimeout(_post_preview_timeout);
1175
1176         if (!isCdmMode() || !getInitParam("cdm_expanded")) {
1177                 _post_preview_timeout = window.setTimeout(function() {
1178                         displaySmallArticlePreview(e, id);
1179                 }, 1000);
1180         }
1181 }
1182
1183 function displaySmallArticlePreview(e, id) {
1184         try {
1185                 var query = "?op=rpc&method=cdmarticlepreview&id=" + id;
1186
1187                 new Ajax.Request("backend.php", {
1188                         parameters: query,
1189                         onComplete: function(transport) {
1190                                 cexc = $("CEXC-" + id);
1191                                 preview = $("small_article_preview");
1192                                 row = $("RROW-" + id);
1193
1194                                 if (id != getActiveArticleId() && (!isCdmMode() || (cexc && Element.visible(cexc))) && row && preview) {
1195                                         preview.innerHTML = transport.responseText;
1196                                         new Effect.Appear(preview, {duration:0.2});
1197
1198                                         preview.setStyle({
1199                                                 left: (e.clientX + 20) + 'px',
1200                                                 top: (Element.cumulativeOffset(row)[1] + row.offsetHeight + 10) + 'px' });
1201
1202                                 }
1203
1204                         } });
1205
1206
1207         } catch (e) {
1208                 exception_error("displaySmallArticlePreview", e);
1209         }
1210 }
1211
1212 function postMouseOut(id) {
1213         post_under_pointer = false;
1214
1215         if (_post_preview_timeout) window.clearTimeout(_post_preview_timeout);
1216
1217         if (Element.visible("small_article_preview"))
1218                 Element.hide("small_article_preview");
1219 }
1220
1221 function unpackVisibleHeadlines() {
1222         try {
1223
1224                 if (!isCdmMode()) return;
1225
1226                 $$("#headlines-frame > div[id*=RROW]").each(
1227                         function(child) {
1228                                 if (child.offsetTop <= $("headlines-frame").scrollTop +
1229                                         $("headlines-frame").offsetHeight) {
1230
1231                                         var cencw = $("CENCW-" + child.id.replace("RROW-", ""));
1232
1233                                         if (cencw) {
1234                                                 cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
1235                                                 cencw.setAttribute('id', '');
1236                                                 Element.show(cencw);
1237                                         }
1238                                 }
1239                         }
1240                 );
1241
1242
1243         } catch (e) {
1244                 exception_error("unpackVisibleHeadlines", e);
1245         }
1246 }
1247
1248 function headlines_scroll_handler(e) {
1249         try {
1250                 var hsp = $("headlines-spacer");
1251
1252                 unpackVisibleHeadlines();
1253
1254                 if (!_infscroll_disable) {
1255                         if ((hsp && e.scrollTop + e.offsetHeight >= hsp.offsetTop - hsp.offsetHeight) ||
1256                                         (e.scrollHeight != 0 &&
1257                                                 ((e.scrollTop + e.offsetHeight) / e.scrollHeight >= 0.7))) {
1258
1259                                 if (hsp)
1260                                         hsp.innerHTML = "<img src='images/indicator_tiny.gif'> " +
1261                                                 __("Loading, please wait...");
1262
1263                                 loadMoreHeadlines();
1264                                 return;
1265
1266                         }
1267                 } else {
1268                         if (hsp) hsp.innerHTML = "";
1269                 }
1270
1271                 if (getInitParam("cdm_auto_catchup") == 1) {
1272
1273                         $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
1274                                 function(child) {
1275                                         if (child.hasClassName("Unread") && $("headlines-frame").scrollTop >
1276                                                         (child.offsetTop + child.offsetHeight/2)) {
1277
1278                                                 var id = child.id.replace("RROW-", "");
1279
1280                                                 if (catchup_id_batch.indexOf(id) == -1)
1281                                                         catchup_id_batch.push(id);
1282
1283                                                 //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
1284                                         }
1285                                 });
1286
1287                         if (catchup_id_batch.length > 0) {
1288                                 window.clearTimeout(catchup_timeout_id);
1289
1290                                 if (!_infscroll_request_sent) {
1291                                         catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
1292                                                 2000);
1293                                 }
1294                         }
1295                 }
1296
1297         } catch (e) {
1298                 console.warn("headlines_scroll_handler: " + e);
1299         }
1300 }
1301
1302 function catchupBatchedArticles() {
1303         try {
1304                 if (catchup_id_batch.length > 0 && !_infscroll_request_sent) {
1305
1306                         // make a copy of the array
1307                         var batch = catchup_id_batch.slice();
1308                         var query = "?op=rpc&method=catchupSelected" +
1309                                 "&cmode=0&ids=" + param_escape(batch.toString());
1310
1311                         console.log(query);
1312
1313                         new Ajax.Request("backend.php", {
1314                                 parameters: query,
1315                                 onComplete: function(transport) {
1316                                         handle_rpc_json(transport);
1317
1318                                         batch.each(function(id) {
1319                                                 var elem = $("RROW-" + id);
1320                                                 if (elem) elem.removeClassName("Unread");
1321                                                 catchup_id_batch.remove(id);
1322                                         });
1323
1324                                 } });
1325                 }
1326
1327         } catch (e) {
1328                 exception_error("catchupBatchedArticles", e);
1329         }
1330 }
1331
1332 function catchupRelativeToArticle(below, id) {
1333
1334         try {
1335
1336                 if (!id) id = getActiveArticleId();
1337
1338                 if (!id) {
1339                         alert(__("No article is selected."));
1340                         return;
1341                 }
1342
1343                 var visible_ids = getVisibleArticleIds();
1344
1345                 var ids_to_mark = new Array();
1346
1347                 if (!below) {
1348                         for (var i = 0; i < visible_ids.length; i++) {
1349                                 if (visible_ids[i] != id) {
1350                                         var e = $("RROW-" + visible_ids[i]);
1351
1352                                         if (e && e.hasClassName("Unread")) {
1353                                                 ids_to_mark.push(visible_ids[i]);
1354                                         }
1355                                 } else {
1356                                         break;
1357                                 }
1358                         }
1359                 } else {
1360                         for (var i = visible_ids.length-1; i >= 0; i--) {
1361                                 if (visible_ids[i] != id) {
1362                                         var e = $("RROW-" + visible_ids[i]);
1363
1364                                         if (e && e.hasClassName("Unread")) {
1365                                                 ids_to_mark.push(visible_ids[i]);
1366                                         }
1367                                 } else {
1368                                         break;
1369                                 }
1370                         }
1371                 }
1372
1373                 if (ids_to_mark.length == 0) {
1374                         alert(__("No articles found to mark"));
1375                 } else {
1376                         var msg = ngettext("Mark %d article as read?", "Mark %d articles as read?", ids_to_mark.length).replace("%d", ids_to_mark.length);
1377
1378                         if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
1379
1380                                 for (var i = 0; i < ids_to_mark.length; i++) {
1381                                         var e = $("RROW-" + ids_to_mark[i]);
1382                                         e.removeClassName("Unread");
1383                                 }
1384
1385                                 var query = "?op=rpc&method=catchupSelected" +
1386                                         "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
1387
1388                                 new Ajax.Request("backend.php", {
1389                                         parameters: query,
1390                                         onComplete: function(transport) {
1391                                                 handle_rpc_json(transport);
1392                                         } });
1393
1394                         }
1395                 }
1396
1397         } catch (e) {
1398                 exception_error("catchupRelativeToArticle", e);
1399         }
1400 }
1401
1402 function cdmCollapseArticle(event, id) {
1403         try {
1404                 var row = $("RROW-" + id);
1405                 var elem = $("CICD-" + id);
1406
1407                 if (elem && row) {
1408                         var collapse = $$("div#RROW-" + id +
1409                                 " span[class='collapseBtn']")[0];
1410
1411                         Element.hide(elem);
1412                         Element.show("CEXC-" + id);
1413                         Element.hide(collapse);
1414
1415                         markHeadline(id, false);
1416
1417                         if (id == getActiveArticleId()) {
1418                                 setActiveArticleId(0);
1419                         }
1420
1421                         if (event) Event.stop(event);
1422                 }
1423
1424         } catch (e) {
1425                 exception_error("cdmCollapseArticle", e);
1426         }
1427 }
1428
1429 function cdmExpandArticle(id) {
1430         try {
1431                 console.log("cdmExpandArticle " + id);
1432
1433                 if (!$("RROW-" + id)) return false;
1434
1435                 hideAuxDlg();
1436
1437                 var elem = $("CICD-" + getActiveArticleId());
1438
1439                 if (id == getActiveArticleId() && Element.visible(elem))
1440                         return true;
1441
1442                 selectArticles("none");
1443
1444                 var old_offset = $("RROW-" + id).offsetTop;
1445
1446                 if (getActiveArticleId() && elem && !getInitParam("cdm_expanded")) {
1447                         var collapse = $$("div#RROW-" + getActiveArticleId() +
1448                                 " span[class='collapseBtn']")[0];
1449
1450                         Element.hide(elem);
1451                         Element.show("CEXC-" + getActiveArticleId());
1452                         Element.hide(collapse);
1453                         $("RROW-" + getActiveArticleId()).removeClassName("active");
1454                 }
1455
1456                 setActiveArticleId(id);
1457
1458                 elem = $("CICD-" + id);
1459
1460                 var collapse = $$("div#RROW-" + id +
1461                                 " span[class='collapseBtn']")[0];
1462
1463                 var cencw = $("CENCW-" + id);
1464
1465                 if (!Element.visible(elem)) {
1466                         if (cencw) {
1467                                 cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
1468                                 cencw.setAttribute('id', '');
1469                                 Element.show(cencw);
1470                         }
1471
1472                         Element.show(elem);
1473                         Element.hide("CEXC-" + id);
1474                         Element.show(collapse);
1475                         $("RROW-" + id).addClassName("active");
1476                 }
1477
1478                 var new_offset = $("RROW-" + id).offsetTop;
1479
1480                 if (old_offset > new_offset)
1481                         $("headlines-frame").scrollTop -= (old_offset-new_offset);
1482
1483                 toggleUnread(id, 0, true);
1484                 toggleSelected(id);
1485
1486         } catch (e) {
1487                 exception_error("cdmExpandArticle", e);
1488         }
1489
1490         return false;
1491 }
1492
1493 function fixHeadlinesOrder(ids) {
1494         try {
1495                 for (var i = 0; i < ids.length; i++) {
1496                         var e = $("RROW-" + ids[i]);
1497
1498                         if (e) {
1499                                 if (i % 2 == 0) {
1500                                         e.removeClassName("even");
1501                                         e.addClassName("odd");
1502                                 } else {
1503                                         e.removeClassName("odd");
1504                                         e.addClassName("even");
1505                                 }
1506                         }
1507                 }
1508         } catch (e) {
1509                 exception_error("fixHeadlinesOrder", e);
1510         }
1511 }
1512
1513 function getArticleUnderPointer() {
1514         return post_under_pointer;
1515 }
1516
1517 function scrollArticle(offset) {
1518         try {
1519                 if (!isCdmMode()) {
1520                         var ci = $("content-insert");
1521                         if (ci) {
1522                                 ci.scrollTop += offset;
1523                         }
1524                 } else {
1525                         var hi = $("headlines-frame");
1526                         if (hi) {
1527                                 hi.scrollTop += offset;
1528                         }
1529
1530                 }
1531         } catch (e) {
1532                 exception_error("scrollArticle", e);
1533         }
1534 }
1535
1536 function show_labels_in_headlines(transport) {
1537         try {
1538                 var data = JSON.parse(transport.responseText);
1539
1540                 if (data) {
1541                         data['info-for-headlines'].each(function(elem) {
1542                                 var ctr = $("HLLCTR-" + elem.id);
1543
1544                                 if (ctr) ctr.innerHTML = elem.labels;
1545                         });
1546                 }
1547         } catch (e) {
1548                 exception_error("show_labels_in_headlines", e);
1549         }
1550 }
1551
1552 function dismissArticle(id) {
1553         try {
1554                 var elem = $("RROW-" + id);
1555
1556                 toggleUnread(id, 0, true);
1557
1558                 new Effect.Fade(elem, {duration : 0.5});
1559
1560                 if (id == getActiveArticleId()) {
1561                         setActiveArticleId(0);
1562                 }
1563
1564         } catch (e) {
1565                 exception_error("dismissArticle", e);
1566         }
1567 }
1568
1569 function dismissSelectedArticles() {
1570         try {
1571
1572                 var ids = getVisibleArticleIds();
1573                 var tmp = [];
1574                 var sel = [];
1575
1576                 for (var i = 0; i < ids.length; i++) {
1577                         var elem = $("RROW-" + ids[i]);
1578
1579                         if (elem.className && elem.hasClassName("Selected") &&
1580                                         ids[i] != getActiveArticleId()) {
1581                                 new Effect.Fade(elem, {duration : 0.5});
1582                                 sel.push(ids[i]);
1583                         } else {
1584                                 tmp.push(ids[i]);
1585                         }
1586                 }
1587
1588                 if (sel.length > 0)
1589                         selectionToggleUnread(false);
1590
1591                 fixHeadlinesOrder(tmp);
1592
1593         } catch (e) {
1594                 exception_error("dismissSelectedArticles", e);
1595         }
1596 }
1597
1598 function dismissReadArticles() {
1599         try {
1600
1601                 var ids = getVisibleArticleIds();
1602                 var tmp = [];
1603
1604                 for (var i = 0; i < ids.length; i++) {
1605                         var elem = $("RROW-" + ids[i]);
1606
1607                         if (elem.className && !elem.hasClassName("Unread") &&
1608                                         !elem.hasClassName("Selected")) {
1609
1610                                 new Effect.Fade(elem, {duration : 0.5});
1611                         } else {
1612                                 tmp.push(ids[i]);
1613                         }
1614                 }
1615
1616                 fixHeadlinesOrder(tmp);
1617
1618         } catch (e) {
1619                 exception_error("dismissSelectedArticles", e);
1620         }
1621 }
1622
1623 function getVisibleArticleIds() {
1624         var ids = [];
1625
1626         try {
1627
1628                 getLoadedArticleIds().each(function(id) {
1629                         var elem = $("RROW-" + id);
1630                         if (elem && Element.visible(elem))
1631                                 ids.push(id);
1632                         });
1633
1634         } catch (e) {
1635                 exception_error("getVisibleArticleIds", e);
1636         }
1637
1638         return ids;
1639 }
1640
1641 function cdmClicked(event, id) {
1642         try {
1643                 //var shift_key = event.shiftKey;
1644
1645                 hideAuxDlg();
1646
1647                 if (!event.ctrlKey) {
1648
1649                         if (!getInitParam("cdm_expanded")) {
1650                                 return cdmExpandArticle(id);
1651                         } else {
1652
1653                                 selectArticles("none");
1654                                 toggleSelected(id);
1655
1656                                 var elem = $("RROW-" + id);
1657                                 var article_is_unread = elem.hasClassName("Unread");
1658
1659                                 if (elem)
1660                                         elem.removeClassName("Unread");
1661
1662                                 setActiveArticleId(id);
1663
1664                                 if (article_is_unread) {
1665                                         decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1666                                 }
1667
1668                                 var query = "?op=rpc&method=catchupSelected" +
1669                                         "&cmode=0&ids=" + param_escape(id);
1670
1671                                 new Ajax.Request("backend.php", {
1672                                         parameters: query,
1673                                         onComplete: function(transport) {
1674                                                 handle_rpc_json(transport);
1675                                         } });
1676
1677                                 return !event.shiftKey;
1678                         }
1679
1680                 } else {
1681                         toggleSelected(id, true);
1682
1683                         var elem = $("RROW-" + id);
1684                         var article_is_unread = elem.hasClassName("Unread");
1685
1686                         if (article_is_unread) {
1687                                 decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1688                         }
1689
1690                         toggleUnread(id, 0, false);
1691
1692                         openArticleInNewWindow(id);
1693                 }
1694
1695                 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
1696                 request_counters(unread_in_buffer == 0);
1697
1698         } catch (e) {
1699                 exception_error("cdmClicked");
1700         }
1701
1702         return false;
1703 }
1704
1705 function hlClicked(event, id) {
1706         try {
1707                 if (event.which == 2) {
1708                         view(id);
1709                         return true;
1710                 } else if (event.ctrlKey) {
1711                         toggleSelected(id, true);
1712                         toggleUnread(id, 0, false);
1713                         openArticleInNewWindow(id);
1714                         return false;
1715                 } else {
1716                         view(id);
1717                         return false;
1718                 }
1719
1720         } catch (e) {
1721                 exception_error("hlClicked");
1722         }
1723 }
1724
1725 function getFirstVisibleHeadlineId() {
1726         var rows = getVisibleArticleIds();
1727         return rows[0];
1728
1729 }
1730
1731 function getLastVisibleHeadlineId() {
1732         var rows = getVisibleArticleIds();
1733         return rows[rows.length-1];
1734 }
1735
1736 function openArticleInNewWindow(id) {
1737         toggleUnread(id, 0, false);
1738         window.open("backend.php?op=article&method=redirect&id=" + id);
1739 }
1740
1741 function isCdmMode() {
1742         return getInitParam("combined_display_mode");
1743 }
1744
1745 function markHeadline(id, marked) {
1746         if (marked == undefined) marked = true;
1747
1748         var row = $("RROW-" + id);
1749         if (row) {
1750                 var check = dijit.byId("RCHK-" + id);
1751
1752                 if (check) {
1753                         check.attr("checked", marked);
1754                 }
1755
1756                 if (marked)
1757                         row.addClassName("Selected");
1758                 else
1759                         row.removeClassName("Selected");
1760         }
1761 }
1762
1763 function getRelativePostIds(id, limit) {
1764
1765         var tmp = [];
1766
1767         try {
1768
1769                 if (!limit) limit = 6; //3
1770
1771                 var ids = getVisibleArticleIds();
1772
1773                 for (var i = 0; i < ids.length; i++) {
1774                         if (ids[i] == id) {
1775                                 for (var k = 1; k <= limit; k++) {
1776                                         //if (i > k-1) tmp.push(ids[i-k]);
1777                                         if (i < ids.length-k) tmp.push(ids[i+k]);
1778                                 }
1779                                 break;
1780                         }
1781                 }
1782
1783         } catch (e) {
1784                 exception_error("getRelativePostIds", e);
1785         }
1786
1787         return tmp;
1788 }
1789
1790 function correctHeadlinesOffset(id) {
1791
1792         try {
1793
1794                 var container = $("headlines-frame");
1795                 var row = $("RROW-" + id);
1796
1797                 if (!container || !row) return;
1798
1799                 var viewport = container.offsetHeight;
1800
1801                 var rel_offset_top = row.offsetTop - container.scrollTop;
1802                 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
1803
1804                 //console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
1805                 //console.log("Vport: " + viewport);
1806
1807                 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
1808                         container.scrollTop = row.offsetTop;
1809                 } else if (rel_offset_bottom > viewport) {
1810
1811                         /* doesn't properly work with Opera in some cases because
1812                                 Opera fucks up element scrolling */
1813
1814                         container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
1815                 }
1816
1817         } catch (e) {
1818                 exception_error("correctHeadlinesOffset", e);
1819         }
1820
1821 }
1822
1823 function headlineActionsChange(elem) {
1824         try {
1825                 eval(elem.value);
1826                 elem.attr('value', 'false');
1827         } catch (e) {
1828                 exception_error("headlineActionsChange", e);
1829         }
1830 }
1831
1832 function closeArticlePanel() {
1833
1834         if (dijit.byId("content-insert"))
1835                 dijit.byId("headlines-wrap-inner").removeChild(
1836                         dijit.byId("content-insert"));
1837 }
1838
1839 function initHeadlinesMenu() {
1840         try {
1841                 if (dijit.byId("headlinesMenu"))
1842                         dijit.byId("headlinesMenu").destroyRecursive();
1843
1844                 var ids = [];
1845
1846                 if (!isCdmMode()) {
1847                         nodes = $$("#headlines-frame > div[id*=RROW]");
1848                 } else {
1849                         nodes = $$("#headlines-frame span[id*=RTITLE]");
1850                 }
1851
1852                 nodes.each(function(node) {
1853                         ids.push(node.id);
1854                 });
1855
1856                 var menu = new dijit.Menu({
1857                         id: "headlinesMenu",
1858                         targetNodeIds: ids,
1859                 });
1860
1861                 var tmph = dojo.connect(menu, '_openMyself', function (event) {
1862                         var callerNode = event.target, match = null, tries = 0;
1863
1864                         while (match == null && callerNode && tries <= 3) {
1865                                 match = callerNode.id.match("^[A-Z]+[-]([0-9]+)$");
1866                                 callerNode = callerNode.parentNode;
1867                                 ++tries;
1868                         }
1869
1870                         if (match) this.callerRowId = parseInt(match[1]);
1871
1872                 });
1873
1874 /*              if (!isCdmMode())
1875                         menu.addChild(new dijit.MenuItem({
1876                                 label: __("View article"),
1877                                 onClick: function(event) {
1878                                         view(this.getParent().callerRowId);
1879                                 }})); */
1880
1881                 menu.addChild(new dijit.MenuItem({
1882                         label: __("Open original article"),
1883                         onClick: function(event) {
1884                                 openArticleInNewWindow(this.getParent().callerRowId);
1885                         }}));
1886
1887                 menu.addChild(new dijit.MenuItem({
1888                         label: __("Display article URL"),
1889                         onClick: function(event) {
1890                                 displayArticleUrl(this.getParent().callerRowId);
1891                         }}));
1892
1893                 menu.addChild(new dijit.MenuSeparator());
1894
1895                 menu.addChild(new dijit.MenuItem({
1896                         label: __("Mark above as read"),
1897                         onClick: function(event) {
1898                                 catchupRelativeToArticle(0, this.getParent().callerRowId);
1899                                 }}));
1900
1901                 menu.addChild(new dijit.MenuItem({
1902                         label: __("Mark below as read"),
1903                         onClick: function(event) {
1904                                 catchupRelativeToArticle(1, this.getParent().callerRowId);
1905                                 }}));
1906
1907
1908                 var labels = dijit.byId("feedTree").model.getItemsInCategory(-2);
1909
1910                 if (labels) {
1911
1912                         menu.addChild(new dijit.MenuSeparator());
1913
1914                         var labelAddMenu = new dijit.Menu({ownerMenu: menu});
1915                         var labelDelMenu = new dijit.Menu({ownerMenu: menu});
1916
1917                         labels.each(function(label) {
1918                                 var id = label.id[0];
1919                                 var bare_id = id.substr(id.indexOf(":")+1);
1920                                 var name = label.name[0];
1921
1922                                 bare_id = -11-bare_id;
1923
1924                                 labelAddMenu.addChild(new dijit.MenuItem({
1925                                         label: name,
1926                                         labelId: bare_id,
1927                                         onClick: function(event) {
1928                                                 var ids = getSelectedArticleIds2();
1929                                                 // cast to string
1930                                                 var id = this.getParent().ownerMenu.callerRowId + "";
1931
1932                                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1933
1934                                                 selectionAssignLabel(this.labelId, ids);
1935                                 }}));
1936
1937                                 labelDelMenu.addChild(new dijit.MenuItem({
1938                                         label: name,
1939                                         labelId: bare_id,
1940                                         onClick: function(event) {
1941                                                 var ids = getSelectedArticleIds2();
1942                                                 // cast to string
1943                                                 var id = this.getParent().ownerMenu.callerRowId + "";
1944
1945                                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1946
1947                                                 selectionRemoveLabel(this.labelId, ids);
1948                                 }}));
1949
1950                         });
1951
1952                         menu.addChild(new dijit.PopupMenuItem({
1953                                 label: __("Assign label"),
1954                                 popup: labelAddMenu,
1955                         }));
1956
1957                         menu.addChild(new dijit.PopupMenuItem({
1958                                 label: __("Remove label"),
1959                                 popup: labelDelMenu,
1960                         }));
1961
1962                 }
1963
1964                 menu.startup();
1965
1966         } catch (e) {
1967                 exception_error("initHeadlinesMenu", e);
1968         }
1969 }
1970
1971
1972 function player(elem) {
1973         var aid = elem.getAttribute("audio-id");
1974         var status = elem.getAttribute("status");
1975
1976         var audio = $(aid);
1977
1978         if (audio) {
1979                 if (status == 0) {
1980                         audio.play();
1981                         status = 1;
1982                         elem.innerHTML = __("Playing...");
1983                         elem.title = __("Click to pause");
1984                         elem.addClassName("playing");
1985                 } else {
1986                         audio.pause();
1987                         status = 0;
1988                         elem.innerHTML = __("Play");
1989                         elem.title = __("Click to play");
1990                         elem.removeClassName("playing");
1991                 }
1992
1993                 elem.setAttribute("status", status);
1994         } else {
1995                 alert("Your browser doesn't seem to support HTML5 audio.");
1996         }
1997 }
1998
1999 function cache_set(id, obj) {
2000         //console.log("cache_set: " + id);
2001         if (has_storage)
2002                 try {
2003                         sessionStorage[id] = obj;
2004                 } catch (e) {
2005                         sessionStorage.clear();
2006                 }
2007 }
2008
2009 function cache_get(id) {
2010         if (has_storage)
2011                 return sessionStorage[id];
2012 }
2013
2014 function cache_clear() {
2015         if (has_storage)
2016                 sessionStorage.clear();
2017 }
2018
2019 function cache_delete(id) {
2020         if (has_storage)
2021                 sessionStorage.removeItem(id);
2022 }
2023
2024 function cancelSearch() {
2025         try {
2026                 _search_query = "";
2027                 viewCurrentFeed();
2028         } catch (e) {
2029                 exception_error("cancelSearch", e);
2030         }
2031 }
2032
2033 function setSelectionScore() {
2034         try {
2035                 var ids = getSelectedArticleIds2();
2036
2037                 if (ids.length > 0) {
2038                         console.log(ids);
2039
2040                         var score = prompt(__("Please enter new score for selected articles:"), score);
2041
2042                         if (score != undefined) {
2043                                 var query = "op=rpc&method=setScore&id=" + param_escape(ids.toString()) +
2044                                         "&score=" + param_escape(score);
2045
2046                                 new Ajax.Request("backend.php", {
2047                                         parameters: query,
2048                                         onComplete: function(transport) {
2049                                                 var reply = JSON.parse(transport.responseText);
2050                                                 if (reply) {
2051                                                         console.log(ids);
2052
2053                                                         ids.each(function(id) {
2054                                                                 var row = $("RROW-" + id);
2055
2056                                                                 if (row) {
2057                                                                         var pic = row.getElementsByClassName("hlScorePic")[0];
2058
2059                                                                         if (pic) {
2060                                                                                 pic.src = pic.src.replace(/score_.*?\.png/,
2061                                                                                         reply["score_pic"]);
2062                                                                                 pic.setAttribute("score", score);
2063                                                                         }
2064                                                                 }
2065                                                         });
2066                                                 }
2067                                         } });
2068                         }
2069
2070                 } else {
2071                         alert(__("No articles are selected."));
2072                 }
2073         } catch (e) {
2074                 exception_error("setSelectionScore", e);
2075         }
2076 }
2077
2078 function changeScore(id, pic) {
2079         try {
2080                 var score = pic.getAttribute("score");
2081
2082                 var new_score = prompt(__("Please enter new score for this article:"), score);
2083
2084                 if (new_score != undefined) {
2085
2086                         var query = "op=rpc&method=setScore&id=" + param_escape(id) +
2087                                 "&score=" + param_escape(new_score);
2088
2089                         new Ajax.Request("backend.php", {
2090                                 parameters: query,
2091                                 onComplete: function(transport) {
2092                                         var reply = JSON.parse(transport.responseText);
2093
2094                                         if (reply) {
2095                                                 pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
2096                                                 pic.setAttribute("score", new_score);
2097                                         }
2098                                 } });
2099                 }
2100         } catch (e) {
2101                 exception_error("changeScore", e);
2102         }
2103 }
2104
2105 function displayArticleUrl(id) {
2106         try {
2107                 var query = "op=rpc&method=getlinktitlebyid&id=" + param_escape(id);
2108
2109                         new Ajax.Request("backend.php", {
2110                                 parameters: query,
2111                                 onComplete: function(transport) {
2112                                         var reply = JSON.parse(transport.responseText);
2113
2114                                         if (reply && reply.link) {
2115                                                 prompt(__("Article URL:"), reply.link);
2116                                         }
2117                                 } });
2118         } catch (e) {
2119                 exception_error("changeScore", e);
2120         }
2121 }