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