]> git.wh0rd.org Git - tt-rss.git/blob - functions.js
add fade-in to shortcut help overlay
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2 var debug_mode_enabled = false;
3 var xmlhttp_rpc = Ajax.getTransport();
4
5 /* add method to remove element from array */
6
7 Array.prototype.remove = function(s) {
8         for (var i=0; i < this.length; i++) {
9                 if (s == this[i]) this.splice(i, 1);
10         }
11 }
12
13 function browser_has_opacity() {
14         return navigator.userAgent.match("Gecko") != null || 
15                 navigator.userAgent.match("Opera") != null;
16 }
17
18 function is_msie() {
19         return navigator.userAgent.match("MSIE");
20 }
21
22 function is_opera() {
23         return navigator.userAgent.match("Opera");
24 }
25
26 function is_khtml() {
27         return navigator.userAgent.match("KHTML");
28 }
29
30 function is_safari() {
31         return navigator.userAgent.match("Safari");
32 }
33
34 function exception_error(location, e, silent) {
35         var msg;
36
37         if (e.fileName) {
38                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
39         
40                 msg = "Exception: " + e.name + ", " + e.message + 
41                         "\nFunction: " + location + "()" +
42                         "\nLocation: " + base_fname + ":" + e.lineNumber;
43                 
44         } else {
45                 msg = "Exception: " + e + "\nFunction: " + location + "()";
46         }
47
48         debug("<b>EXCEPTION: " + msg + "</b>");
49
50         if (!silent) {
51                 alert(msg);
52         }
53 }
54
55 function disableHotkeys() {
56         hotkeys_enabled = false;
57 }
58
59 function enableHotkeys() {
60         hotkeys_enabled = true;
61 }
62
63 function xmlhttp_ready(obj) {
64         return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
65 }
66
67 function open_article_callback(transport) {
68         try {
69
70                 if (transport.responseXML) {
71                         
72                         var link = transport.responseXML.getElementsByTagName("link")[0];
73                         var id = transport.responseXML.getElementsByTagName("id")[0];
74
75                         debug("open_article_callback, received link: " + link);
76
77                         if (link && id) {
78
79                                 var wname = "ttrss_article_" + id.firstChild.nodeValue;
80
81                                 debug("link url: " + link.firstChild.nodeValue + ", wname " + wname);
82
83                                 var w = window.open(link.firstChild.nodeValue, wname);
84
85                                 if (!w) { notify_error("Failed to load article in new window"); }
86
87                                 if (id) {
88                                         id = id.firstChild.nodeValue;
89                                         if (!document.getElementById("headlinesList")) {
90                                                 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
91                                         }
92                                 }
93                         } else {
94                                 notify_error("Can't open article: received invalid article link");
95                         }
96                 } else {
97                         notify_error("Can't open article: received invalid XML");
98                 }
99
100         } catch (e) {
101                 exception_error("open_article_callback", e);
102         }
103 }
104
105 function param_escape(arg) {
106         if (typeof encodeURIComponent != 'undefined')
107                 return encodeURIComponent(arg); 
108         else
109                 return escape(arg);
110 }
111
112 function param_unescape(arg) {
113         if (typeof decodeURIComponent != 'undefined')
114                 return decodeURIComponent(arg); 
115         else
116                 return unescape(arg);
117 }
118
119 function delay(gap) {
120         var then,now; 
121         then=new Date().getTime();
122         now=then;
123         while((now-then)<gap) {
124                 now=new Date().getTime();
125         }
126 }
127
128 var notify_hide_timerid = false;
129
130 function hide_notify() {
131         var n = document.getElementById("notify");
132         if (n) {
133                 n.style.display = "none";
134         }
135
136
137 function notify_real(msg, no_hide, n_type) {
138
139         var n = document.getElementById("notify");
140         var nb = document.getElementById("notify_body");
141
142         if (!n || !nb) return;
143
144         if (notify_hide_timerid) {
145                 window.clearTimeout(notify_hide_timerid);
146         }
147
148         if (msg == "") {
149                 if (n.style.display == "block") {
150                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
151                 }
152                 return;
153         } else {
154                 n.style.display = "block";
155         }
156
157         /* types:
158
159                 1 - generic
160                 2 - progress
161                 3 - error
162                 4 - info
163
164         */
165
166         if (typeof __ != 'undefined') {
167                 msg = __(msg);
168         }
169
170         if (n_type == 1) {
171                 n.className = "notify";
172         } else if (n_type == 2) {
173                 n.className = "notifyProgress";
174                 msg = "<img src='images/indicator_white.gif'> " + msg;
175         } else if (n_type == 3) {
176                 n.className = "notifyError";
177                 msg = "<img src='images/sign_excl.gif'> " + msg;
178         } else if (n_type == 4) {
179                 n.className = "notifyInfo";
180                 msg = "<img src='images/sign_info.gif'> " + msg;
181         }
182
183 //      msg = "<img src='images/live_com_loading.gif'> " + msg;
184
185         nb.innerHTML = msg;
186
187         if (!no_hide) {
188                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
189         }
190 }
191
192 function notify(msg, no_hide) {
193         notify_real(msg, no_hide, 1);
194 }
195
196 function notify_progress(msg, no_hide) {
197         notify_real(msg, no_hide, 2);
198 }
199
200 function notify_error(msg, no_hide) {
201         notify_real(msg, no_hide, 3);
202
203 }
204
205 function notify_info(msg, no_hide) {
206         notify_real(msg, no_hide, 4);
207 }
208
209 function printLockingError() {
210         notify_info("Please wait until operation finishes.");
211 }
212
213 function cleanSelectedList(element) {
214         var content = document.getElementById(element);
215
216         if (!document.getElementById("feedCatHolder")) {
217                 for (i = 0; i < content.childNodes.length; i++) {
218                         var child = content.childNodes[i];
219                         try {
220                                 child.className = child.className.replace("Selected", "");
221                         } catch (e) {
222                                 //
223                         }
224                 }
225         } else {
226                 for (i = 0; i < content.childNodes.length; i++) {
227                         var child = content.childNodes[i];
228                         if (child.id == "feedCatHolder") {
229                                 debug(child.id);
230                                 var fcat = child.lastChild;
231                                 for (j = 0; j < fcat.childNodes.length; j++) {
232                                         var feed = fcat.childNodes[j];
233                                         feed.className = feed.className.replace("Selected", "");
234                                 }               
235                         }
236                 } 
237         }
238 }
239
240
241 function cleanSelected(element) {
242         var content = document.getElementById(element);
243
244         for (i = 0; i < content.rows.length; i++) {
245                 content.rows[i].className = content.rows[i].className.replace("Selected", "");
246         }
247 }
248
249 function getVisibleUnreadHeadlines() {
250         var content = document.getElementById("headlinesList");
251
252         var rows = new Array();
253
254         for (i = 0; i < content.rows.length; i++) {
255                 var row_id = content.rows[i].id.replace("RROW-", "");
256                 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
257                                 rows.push(row_id);      
258                 }
259         }
260         return rows;
261 }
262
263 function getVisibleHeadlineIds() {
264
265         var content = document.getElementById("headlinesList");
266
267         var rows = new Array();
268
269         for (i = 0; i < content.rows.length; i++) {
270                 var row_id = content.rows[i].id.replace("RROW-", "");
271                 if (row_id.length > 0) {
272                                 rows.push(row_id);      
273                 }
274         }
275         return rows;
276 }
277
278 function getFirstVisibleHeadlineId() {
279         if (isCdmMode()) {
280                 var rows = cdmGetVisibleArticles();
281                 return rows[0];
282         } else {
283                 var rows = getVisibleHeadlineIds();
284                 return rows[0];
285         }
286 }
287
288 function getLastVisibleHeadlineId() {
289         if (isCdmMode()) {
290                 var rows = cdmGetVisibleArticles();
291                 return rows[rows.length-1];
292         } else {
293                 var rows = getVisibleHeadlineIds();
294                 return rows[rows.length-1];
295         }
296 }
297
298 function markHeadline(id) {
299         var row = document.getElementById("RROW-" + id);
300         if (row) {
301                 var is_active = false;
302         
303                 if (row.className.match("Active")) {
304                         is_active = true;
305                 }
306                 row.className = row.className.replace("Selected", "");
307                 row.className = row.className.replace("Active", "");
308                 row.className = row.className.replace("Insensitive", "");
309                 
310                 if (is_active) {
311                         row.className = row.className = "Active";
312                 }
313                 
314                 var check = document.getElementById("RCHK-" + id);
315
316                 if (check) {
317                         check.checked = true;
318                 }
319
320                 row.className = row.className + "Selected"; 
321                 
322         }
323 }
324
325 function getFeedIds() {
326         var content = document.getElementById("feedsList");
327
328         var rows = new Array();
329
330         for (i = 0; i < content.rows.length; i++) {
331                 var id = content.rows[i].id.replace("FEEDR-", "");
332                 if (id.length > 0) {
333                         rows.push(id);
334                 }
335         }
336
337         return rows;
338 }
339
340 function setCookie(name, value, lifetime, path, domain, secure) {
341         
342         var d = false;
343         
344         if (lifetime) {
345                 d = new Date();
346                 d.setTime(d.getTime() + (lifetime * 1000));
347         }
348
349         debug("setCookie: " + name + " => " + value + ": " + d);
350         
351         int_setCookie(name, value, d, path, domain, secure);
352
353 }
354
355 function int_setCookie(name, value, expires, path, domain, secure) {
356         document.cookie= name + "=" + escape(value) +
357                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
358                 ((path) ? "; path=" + path : "") +
359                 ((domain) ? "; domain=" + domain : "") +
360                 ((secure) ? "; secure" : "");
361 }
362
363 function delCookie(name, path, domain) {
364         if (getCookie(name)) {
365                 document.cookie = name + "=" +
366                 ((path) ? ";path=" + path : "") +
367                 ((domain) ? ";domain=" + domain : "" ) +
368                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
369         }
370 }
371                 
372
373 function getCookie(name) {
374
375         var dc = document.cookie;
376         var prefix = name + "=";
377         var begin = dc.indexOf("; " + prefix);
378         if (begin == -1) {
379             begin = dc.indexOf(prefix);
380             if (begin != 0) return null;
381         }
382         else {
383             begin += 2;
384         }
385         var end = document.cookie.indexOf(";", begin);
386         if (end == -1) {
387             end = dc.length;
388         }
389         return unescape(dc.substring(begin + prefix.length, end));
390 }
391
392 function disableContainerChildren(id, disable, doc) {
393
394         if (!doc) doc = document;
395
396         var container = doc.getElementById(id);
397
398         if (!container) {
399                 //alert("disableContainerChildren: element " + id + " not found");
400                 return;
401         }
402
403         for (var i = 0; i < container.childNodes.length; i++) {
404                 var child = container.childNodes[i];
405
406                 try {
407                         child.disabled = disable;
408                 } catch (E) {
409
410                 }
411
412                 if (disable) {
413                         if (child.className && child.className.match("button")) {
414                                 child.className = "disabledButton";
415                         }
416                 } else {
417                         if (child.className && child.className.match("disabledButton")) {
418                                 child.className = "button";
419                         }
420                 } 
421         }
422
423 }
424
425 function gotoPreferences() {
426         document.location.href = "prefs.php";
427 }
428
429 function gotoMain() {
430         document.location.href = "tt-rss.php";
431 }
432
433 function gotoExportOpml() {
434         document.location.href = "opml.php?op=Export";
435 }
436
437 function getActiveFeedId() {
438 //      return getCookie("ttrss_vf_actfeed");
439         try {
440                 debug("gAFID: " + active_feed_id);
441                 return active_feed_id;
442         } catch (e) {
443                 exception_error("getActiveFeedId", e);
444         }
445 }
446
447 function activeFeedIsCat() {
448         return active_feed_is_cat;
449 }
450
451 function setActiveFeedId(id) {
452 //      return setCookie("ttrss_vf_actfeed", id);
453         try {
454                 debug("sAFID(" + id + ")");
455                 active_feed_id = id;
456         } catch (e) {
457                 exception_error("setActiveFeedId", e);
458         }
459 }
460
461 function parse_counters(reply, scheduled_call) {
462         try {
463
464                 var feeds_found = 0;
465
466                 var elems = reply.getElementsByTagName("counter");
467
468                 for (var l = 0; l < elems.length; l++) {
469
470                         var id = elems[l].getAttribute("id");
471                         var t = elems[l].getAttribute("type");
472                         var ctr = elems[l].getAttribute("counter");
473                         var error = elems[l].getAttribute("error");
474                         var has_img = elems[l].getAttribute("hi");
475                         var updated = elems[l].getAttribute("updated");
476                         var title = elems[l].getAttribute("title");
477                 
478                         if (id == "global-unread") {
479                                 global_unread = ctr;
480                                 updateTitle();
481                                 continue;
482                         }
483
484                         if (id == "subscribed-feeds") {
485                                 feeds_found = ctr;
486                                 continue;
487                         }
488         
489                         if (t == "category") {
490                                 var catctr = document.getElementById("FCATCTR-" + id);
491                                 if (catctr) {
492                                         catctr.innerHTML = "(" + ctr + ")";
493                                         if (ctr > 0) {
494                                                 catctr.className = "catCtrHasUnread";
495                                         } else {
496                                                 catctr.className = "catCtrNoUnread";
497                                         }
498                                 }
499                                 continue;
500                         }
501                 
502                         var feedctr = document.getElementById("FEEDCTR-" + id);
503                         var feedu = document.getElementById("FEEDU-" + id);
504                         var feedr = document.getElementById("FEEDR-" + id);
505                         var feed_img = document.getElementById("FIMG-" + id);
506                         var feedlink = document.getElementById("FEEDL-" + id);
507                         var feedupd = document.getElementById("FLUPD-" + id);
508
509                         if (updated && feedlink) {
510                                 if (error) {
511                                         feedlink.title = "Error: " + error + " (" + updated + ")";
512                                 } else {
513                                         feedlink.title = "Updated: " + updated;
514                                 }
515                         }
516
517                         if (updated && feedupd) {
518                                 if (error) {
519                                         feedupd.innerHTML = updated + " (Error)";
520                                 } else {
521                                         feedupd.innerHTML = updated;
522                                 }
523                         }
524
525                         if (has_img && feed_img && !is_msie()) {
526                                 if (!feed_img.src.match(id + ".ico")) {
527                                         feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
528                                 }
529                         }
530
531                         if (feedlink && title) {
532                                 feedlink.innerHTML = title;
533                         }
534
535                         if (feedctr && feedu && feedr) {
536
537                                 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
538                                         viewCurrentFeed();
539                                 }
540
541                                 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
542
543                                 feedu.innerHTML = ctr;
544
545                                 if (error) {
546                                         feedr.className = feedr.className.replace("feed", "error");
547                                 } else if (id > 0) {
548                                         feedr.className = feedr.className.replace("error", "feed");
549                                 }
550         
551                                 if (ctr > 0) {                                  
552                                         feedctr.className = "odd";
553                                         if (!feedr.className.match("Unread")) {
554                                                 var is_selected = feedr.className.match("Selected");
555                 
556                                                 feedr.className = feedr.className.replace("Selected", "");
557                                                 feedr.className = feedr.className.replace("Unread", "");
558                 
559                                                 feedr.className = feedr.className + "Unread";
560                 
561                                                 if (is_selected) {
562                                                         feedr.className = feedr.className + "Selected";
563                                                 }       
564                                                 
565                                         }
566
567                                         if (row_needs_hl) { 
568                                                 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
569                                                         queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
570                                         }
571                                 } else {
572                                         feedctr.className = "invisible";
573                                         feedr.className = feedr.className.replace("Unread", "");
574                                 }                       
575                         }
576                 }
577
578                 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
579
580                 var feeds_stored = number_of_feeds;
581
582                 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
583
584                 if (feeds_stored != feeds_found) {
585                         number_of_feeds = feeds_found;
586
587                         if (feeds_stored != 0 && feeds_found != 0) {
588                                 debug("Subscribed feed number changed, refreshing feedlist");
589                                 setTimeout('updateFeedList(false, false)', 50);
590                         }
591                 }
592
593         } catch (e) {
594                 exception_error("parse_counters", e);
595         }
596 }
597
598 function parse_counters_reply(transport, scheduled_call) {
599
600         if (!transport.responseXML) {
601                 notify_error("Backend did not return valid XML", true);
602                 return;
603         }
604
605         var reply = transport.responseXML.firstChild;
606         
607         if (!reply) {
608                 notify_error("Backend did not return expected XML object", true);
609                 updateTitle("");
610                 return;
611         } 
612         
613         var error_code = false;
614         var error_msg = false;
615
616         if (reply.firstChild) {
617                 error_code = reply.firstChild.getAttribute("error-code");
618                 error_msg = reply.firstChild.getAttribute("error-msg");
619         }
620
621         if (!error_code) {      
622                 error_code = reply.getAttribute("error-code");
623                 error_msg = reply.getAttribute("error-msg");
624         }
625         
626         if (error_code && error_code != 0) {
627                 debug("refetch_callback: got error code " + error_code);
628                 return fatalError(error_code, error_msg);
629         }
630
631         var counters = reply.getElementsByTagName("counters")[0];
632         
633         parse_counters(counters, scheduled_call);
634
635         var runtime_info = reply.getElementsByTagName("runtime-info")[0];
636
637         parse_runtime_info(runtime_info);
638
639         if (getInitParam("feeds_sort_by_unread") == 1) {
640                         resort_feedlist();              
641         }       
642
643         hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
644
645 }
646
647 function all_counters_callback2(transport) {
648         try {
649                 debug("<b>all_counters_callback2 IN: " + transport + "</b>");
650                 parse_counters_reply(transport);
651                 debug("<b>all_counters_callback2 OUT: " + transport + "</b>");
652
653         } catch (e) {
654                 exception_error("all_counters_callback2", e);
655         }
656 }
657
658 function get_feed_unread(id) {
659         try {
660                 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);      
661         } catch (e) {
662                 exception_error("get_feed_unread", e, true);
663                 return -1;
664         }
665 }
666
667 function get_feed_entry_unread(doc, elem) {
668
669         var id = elem.id.replace("FEEDR-", "");
670
671         if (id <= 0) {
672                 return -1;
673         }
674
675         try {
676                 return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);   
677         } catch (e) {
678                 return -1;
679         }
680 }
681
682 function resort_category(doc, node) {
683         debug("resort_category: " + node);
684
685         if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
686                 for (i = 0; i < node.childNodes.length; i++) {
687                         if (node.childNodes[i].nodeName != "LI") { continue; }
688
689                         if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
690                                 continue;
691                         }
692
693                         for (j = i+1; j < node.childNodes.length; j++) {                        
694                                 if (node.childNodes[j].nodeName != "LI") { continue; }  
695
696                                 var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
697                                 var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
698
699                                 if (cur_val > tmp_val) {
700                                         tempnode_i = node.childNodes[i].cloneNode(true);
701                                         tempnode_j = node.childNodes[j].cloneNode(true);
702                                         node.replaceChild(tempnode_i, node.childNodes[j]);
703                                         node.replaceChild(tempnode_j, node.childNodes[i]);
704                                 }
705                         }
706
707                 }
708         }
709
710 }
711
712 function resort_feedlist() {
713         debug("resort_feedlist");
714
715         var fd = document;
716
717         if (fd.getElementById("feedCatHolder")) {
718
719                 var feeds = fd.getElementById("feedList");
720                 var child = feeds.firstChild;
721
722                 while (child) {
723
724                         if (child.id == "feedCatHolder") {
725                                 resort_category(fd, child.firstChild);
726                         }
727         
728                         child = child.nextSibling;
729                 }
730
731         } else {
732                 resort_category(fd, fd.getElementById("feedList"));
733         }
734 }
735
736 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
737   * * @author Sundar Dorai-Raj
738   * * Email: sdoraira@vt.edu
739   * * This program is free software; you can redistribute it and/or
740   * * modify it under the terms of the GNU General Public License 
741   * * as published by the Free Software Foundation; either version 2 
742   * * of the License, or (at your option) any later version, 
743   * * provided that any use properly credits the author. 
744   * * This program is distributed in the hope that it will be useful,
745   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
746   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
747   * * GNU General Public License for more details at http://www.gnu.org * * */
748
749   var numbers=".0123456789";
750   function isNumeric(x) {
751     // is x a String or a character?
752     if(x.length>1) {
753       // remove negative sign
754       x=Math.abs(x)+"";
755       for(j=0;j<x.length;j++) {
756         // call isNumeric recursively for each character
757         number=isNumeric(x.substring(j,j+1));
758         if(!number) return number;
759       }
760       return number;
761     }
762     else {
763       // if x is number return true
764       if(numbers.indexOf(x)>=0) return true;
765       return false;
766     }
767   }
768
769
770 function hideOrShowFeeds(doc, hide) {
771
772         debug("hideOrShowFeeds: " + doc + ", " + hide);
773
774         var fd = document;
775
776         var list = fd.getElementById("feedList");
777
778         if (fd.getElementById("feedCatHolder")) {
779
780                 var feeds = fd.getElementById("feedList");
781                 var child = feeds.firstChild;
782
783                 while (child) {
784
785                         if (child.id == "feedCatHolder") {
786                                 hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
787                         }
788         
789                         child = child.nextSibling;
790                 }
791
792         } else {
793                 hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
794         }
795 }
796
797 function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
798
799 //      debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
800
801         var cat_unread = 0;
802
803         if (!node) {
804                 debug("hideOrShowFeeds: passed node is null, aborting");
805                 return;
806         }
807
808 //      debug("cat: " + node.id);
809
810         if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
811                 for (i = 0; i < node.childNodes.length; i++) {
812                         if (node.childNodes[i].nodeName != "LI") { continue; }
813
814                         if (node.childNodes[i].style != undefined) {
815
816                                 var has_unread = (node.childNodes[i].className != "feed" &&
817                                         node.childNodes[i].className != "label" && 
818                                         !(!getInitParam("hide_read_shows_special") && 
819                                                 node.childNodes[i].className == "virt") && 
820                                         node.childNodes[i].className != "error" && 
821                                         node.childNodes[i].className != "tag");
822         
823 //                              debug(node.childNodes[i].id + " --> " + has_unread);
824         
825                                 if (hide && !has_unread) {
826                                         //node.childNodes[i].style.display = "none";
827                                         var id = node.childNodes[i].id;
828                                         Effect.Fade(node.childNodes[i], {duration : 0.3, 
829                                                 queue: { position: 'end', scope: 'FFADE-' + id, limit: 1 }});
830                                 }
831         
832                                 if (!hide) {
833                                         node.childNodes[i].style.display = "list-item";
834                                         //Effect.Appear(node.childNodes[i], {duration : 0.3});
835                                 }
836         
837                                 if (has_unread) {
838                                         node.childNodes[i].style.display = "list-item";
839                                         cat_unread++;
840                                         //Effect.Appear(node.childNodes[i], {duration : 0.3});
841                                         //Effect.Highlight(node.childNodes[i]);
842                                 }
843                         }
844                 }
845         }       
846
847 //      debug("end cat: " + node.id + " unread " + cat_unread);
848
849         if (cat_unread == 0) {
850                 if (cat_node.style == undefined) {
851                         debug("ERROR: supplied cat_node " + cat_node + 
852                                 " has no styles. WTF?");
853                         return;
854                 }
855                 if (hide) {
856                         //cat_node.style.display = "none";
857                         Effect.Fade(cat_node, {duration : 0.3, 
858                                 queue: { position: 'end', scope: 'CFADE-' + node.id, limit: 1 }});
859                 } else {
860                         cat_node.style.display = "list-item";
861                 }
862         } else {
863                 try {
864                         cat_node.style.display = "list-item";
865                 } catch (e) {
866                         debug(e);
867                 }
868         }
869
870 //      debug("unread for category: " + cat_unread);
871 }
872
873 function selectTableRow(r, do_select) {
874         r.className = r.className.replace("Selected", "");
875         
876         if (do_select) {
877                 r.className = r.className + "Selected";
878         }
879 }
880
881 function selectTableRowById(elem_id, check_id, do_select) {
882
883         try {
884
885                 var row = document.getElementById(elem_id);
886
887                 if (row) {
888                         selectTableRow(row, do_select);
889                 }               
890
891                 var check = document.getElementById(check_id);
892
893                 if (check) {
894                         check.checked = do_select;
895                 }
896         } catch (e) {
897                 exception_error("selectTableRowById", e);
898         }
899 }
900
901 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select, 
902         classcheck, reset_others) {
903
904         var content = document.getElementById(content_id);
905
906         if (!content) {
907                 alert("[selectTableRows] Element " + content_id + " not found.");
908                 return;
909         }
910
911         for (i = 0; i < content.rows.length; i++) {
912                 if (Element.visible(content.rows[i])) {
913                         if (!classcheck || content.rows[i].className.match(classcheck)) {
914                 
915                                 if (content.rows[i].id.match(prefix)) {
916                                         selectTableRow(content.rows[i], do_select);
917                                 
918                                         var row_id = content.rows[i].id.replace(prefix, "");
919                                         var check = document.getElementById(check_prefix + row_id);
920         
921                                         if (check) {
922                                                 check.checked = do_select;
923                                         }
924                                 } else if (reset_others) {
925                                         selectTableRow(content.rows[i], false);
926         
927                                         var row_id = content.rows[i].id.replace(prefix, "");
928                                         var check = document.getElementById(check_prefix + row_id);
929         
930                                         if (check) {
931                                                 check.checked = false;
932                                         }
933         
934                                 }
935                         } else if (reset_others) {
936                                 selectTableRow(content.rows[i], false);
937         
938                                 var row_id = content.rows[i].id.replace(prefix, "");
939                                 var check = document.getElementById(check_prefix + row_id);
940         
941                                 if (check) {
942                                         check.checked = false;
943                                 }
944         
945                         }
946                 }
947         }
948 }
949
950 function getSelectedTableRowIds(content_id, prefix) {
951
952         var content = document.getElementById(content_id);
953
954         if (!content) {
955                 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
956                 return;
957         }
958
959         var sel_rows = new Array();
960
961         for (i = 0; i < content.rows.length; i++) {
962                 if (content.rows[i].id.match(prefix) && 
963                                 content.rows[i].className.match("Selected")) {
964                                 
965                         var row_id = content.rows[i].id.replace(prefix + "-", "");
966                         sel_rows.push(row_id);  
967                 }
968         }
969
970         return sel_rows;
971
972 }
973
974 function toggleSelectRowById(sender, id) {
975         var row = document.getElementById(id);
976
977         if (sender.checked) {
978                 if (!row.className.match("Selected")) {
979                         row.className = row.className + "Selected";
980                 }
981         } else {
982                 if (row.className.match("Selected")) {
983                         row.className = row.className.replace("Selected", "");
984                 }
985         }
986 }
987
988 function toggleSelectListRow(sender) {
989         var parent_row = sender.parentNode;
990
991         if (sender.checked) {
992                 if (!parent_row.className.match("Selected")) {
993                         parent_row.className = parent_row.className + "Selected";
994                 }
995         } else {
996                 if (parent_row.className.match("Selected")) {
997                         parent_row.className = parent_row.className.replace("Selected", "");
998                 }
999         }
1000 }
1001
1002 function tSR(sender) {
1003         return toggleSelectRow(sender);
1004 }
1005
1006 function toggleSelectRow(sender) {
1007         var parent_row = sender.parentNode.parentNode;
1008
1009         if (sender.checked) {
1010                 if (!parent_row.className.match("Selected")) {
1011                         parent_row.className = parent_row.className + "Selected";
1012                 }
1013         } else {
1014                 if (parent_row.className.match("Selected")) {
1015                         parent_row.className = parent_row.className.replace("Selected", "");
1016                 }
1017         }
1018 }
1019
1020 function getRelativeFeedId(list, id, direction, unread_only) {  
1021         var rows = list.getElementsByTagName("LI");
1022         var feeds = new Array();
1023
1024         for (var i = 0; i < rows.length; i++) {
1025                 if (rows[i].id.match("FEEDR-")) {
1026
1027                         if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1028
1029                                 if (!unread_only || 
1030                                                 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1031                                         feeds.push(rows[i].id.replace("FEEDR-", ""));
1032                                 }
1033                         }
1034                 }
1035         }
1036
1037         if (!id) {
1038                 if (direction == "next") {
1039                         return feeds.shift();
1040                 } else {
1041                         return feeds.pop();
1042                 }
1043         } else {
1044                 if (direction == "next") {
1045                         var idx = feeds.indexOf(id);
1046                         if (idx != -1 && idx < feeds.length) {
1047                                 return feeds[idx+1];                                    
1048                         } else {
1049                                 return getRelativeFeedId(list, false, direction, unread_only);
1050                         }
1051                 } else {
1052                         var idx = feeds.indexOf(id);
1053                         if (idx > 0) {
1054                                 return feeds[idx-1];
1055                         } else {
1056                                 return getRelativeFeedId(list, false, direction, unread_only);
1057                         }
1058                 }
1059
1060         }
1061 }
1062
1063 function showBlockElement(id, h_id) {
1064         var elem = document.getElementById(id);
1065
1066         if (elem) {
1067                 elem.style.display = "block";
1068
1069                 if (h_id) {
1070                         elem = document.getElementById(h_id);
1071                         if (elem) {
1072                                 elem.style.display = "none";
1073                         }
1074                 }
1075         } else {
1076                 alert("[showBlockElement] can't find element with id " + id);
1077         } 
1078 }
1079
1080 function appearBlockElement_afh(effect) {
1081
1082 }
1083
1084 function checkboxToggleElement(elem, id) {
1085         if (elem.checked) {
1086                 Effect.SlideDown(id, {duration : 0.5});
1087         } else {
1088                 Effect.SlideUp(id, {duration : 0.5});
1089         }
1090 }
1091
1092 function appearBlockElement(id, h_id) {
1093
1094         try {
1095                 if (h_id) {
1096                         Effect.Fade(h_id);
1097                 }
1098                 Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
1099         } catch (e) {
1100                 exception_error("appearBlockElement", e);
1101         }
1102
1103 }
1104
1105 function hideParentElement(e) {
1106         e.parentNode.style.display = "none";
1107 }
1108
1109 function dropboxSelect(e, v) {
1110         for (i = 0; i < e.length; i++) {
1111                 if (e[i].value == v) {
1112                         e.selectedIndex = i;
1113                         break;
1114                 }
1115         }
1116 }
1117
1118 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1119 // bugfixed just a little bit :-)
1120 function getURLParam(strParamName){
1121   var strReturn = "";
1122   var strHref = window.location.href;
1123
1124   if (strHref.indexOf("#") == strHref.length-1) {
1125                 strHref = strHref.substring(0, strHref.length-1);
1126   }
1127
1128   if ( strHref.indexOf("?") > -1 ){
1129     var strQueryString = strHref.substr(strHref.indexOf("?"));
1130     var aQueryString = strQueryString.split("&");
1131     for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1132       if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1133         var aParam = aQueryString[iParam].split("=");
1134         strReturn = aParam[1];
1135         break;
1136       }
1137     }
1138   }
1139   return strReturn;
1140
1141
1142 function leading_zero(p) {
1143         var s = String(p);
1144         if (s.length == 1) s = "0" + s;
1145         return s;
1146 }
1147
1148 function closeInfoBox(cleanup) {
1149
1150         if (!is_msie() && !getInitParam("infobox_disable_overlay")) {
1151                 var overlay = document.getElementById("dialog_overlay");
1152                 if (overlay) {
1153                         overlay.style.display = "none";
1154                 }
1155         }
1156
1157         var box = document.getElementById('infoBox');
1158         var shadow = document.getElementById('infoBoxShadow');
1159
1160         if (shadow) {
1161                 shadow.style.display = "none";
1162         } else if (box) {
1163                 box.style.display = "none";
1164         }
1165
1166         if (cleanup) box.innerHTML = "&nbsp;";
1167
1168         enableHotkeys();
1169
1170         return false;
1171 }
1172
1173
1174 function displayDlg(id, param) {
1175
1176         notify_progress("Loading, please wait...", true);
1177
1178         disableHotkeys();
1179
1180         var query = "backend.php?op=dlg&id=" +
1181                 param_escape(id) + "&param=" + param_escape(param);
1182
1183         new Ajax.Request(query, {
1184                 onComplete: function (transport) {
1185                         infobox_callback2(transport);
1186                 } });
1187
1188         return false;
1189 }
1190
1191 function infobox_submit_callback2(transport) {
1192         closeInfoBox();
1193
1194         try {
1195                 // called from prefs, reload tab
1196                 if (active_tab) {
1197                         selectTab(active_tab, false);
1198                 }
1199         } catch (e) { }
1200
1201         if (transport.responseText) {
1202                 notify_info(transport.responseText);
1203         }
1204 }
1205
1206 function infobox_callback2(transport) {
1207         try {
1208
1209                 debug("infobox_callback2");
1210
1211                 if (!is_msie() && !getInitParam("infobox_disable_overlay")) {
1212                         var overlay = document.getElementById("dialog_overlay");
1213                         if (overlay) {
1214                                 overlay.style.display = "block";
1215                         }
1216                 }
1217
1218                 var box = document.getElementById('infoBox');
1219                 var shadow = document.getElementById('infoBoxShadow');
1220                 if (box) {                      
1221
1222 /*                              if (!is_safari()) {
1223                                 new Draggable(shadow);
1224                         } */
1225
1226                         box.innerHTML=transport.responseText;                   
1227                         if (shadow) {
1228                                 shadow.style.display = "block";
1229                         } else {
1230                                 box.style.display = "block";                            
1231                         }
1232                 }
1233
1234                 /* FIXME this needs to be moved out somewhere */
1235
1236                 if (document.getElementById("tags_choices")) {
1237                         new Ajax.Autocompleter('tags_str', 'tags_choices',
1238                                 "backend.php?op=rpc&subop=completeTags",
1239                                 { tokens: ',', paramName: "search" });
1240                 }
1241
1242                 disableHotkeys();
1243
1244                 notify("");
1245         } catch (e) {
1246                 exception_error("infobox_callback2", e);
1247         }
1248 }
1249
1250 function createFilter() {
1251
1252         var form = document.forms['filter_add_form'];
1253         var reg_exp = form.reg_exp.value;
1254
1255         if (reg_exp == "") {
1256                 alert(__("Can't add filter: nothing to match on."));
1257                 return false;
1258         }
1259
1260         var query = Form.serialize("filter_add_form");
1261
1262         // we can be called from some other tab in Prefs
1263         if (active_tab) active_tab = "filterConfig";
1264
1265         new Ajax.Request("backend.php?" + query, {
1266                 onComplete: function (transport) {
1267                         infobox_submit_callback2(transport);
1268                 } });
1269         
1270         return true;
1271 }
1272
1273 function toggleSubmitNotEmpty(e, submit_id) {
1274         try {
1275                 document.getElementById(submit_id).disabled = (e.value == "")
1276         } catch (e) {
1277                 exception_error("toggleSubmitNotEmpty", e);
1278         }
1279 }
1280
1281 function isValidURL(s) {
1282         return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1283 }
1284
1285 function subscribeToFeed() {
1286
1287         var form = document.forms['feed_add_form'];
1288         var feed_url = form.feed_url.value;
1289
1290         if (feed_url == "") {
1291                 alert(__("Can't subscribe: no feed URL given."));
1292                 return false;
1293         }
1294
1295         notify_progress(__("Subscribing to feed..."), true);
1296
1297         closeInfoBox();
1298
1299         var feeds_doc = document;
1300
1301 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1302         
1303         var query = Form.serialize("feed_add_form");
1304         
1305         debug("subscribe q: " + query);
1306
1307         new Ajax.Request("backend.php", {
1308                 parameters: query,
1309                 onComplete: function(transport) { 
1310                         dlg_frefresh_callback(transport); 
1311                 } });
1312
1313         return false;
1314 }
1315
1316 function filterCR(e, f)
1317 {
1318      var key;
1319
1320      if(window.event)
1321           key = window.event.keyCode;     //IE
1322      else
1323           key = e.which;     //firefox
1324
1325         if (key == 13) {
1326                 if (typeof f != 'undefined') {
1327                         f();
1328                         return false;
1329                 } else {
1330                         return false;
1331                 }
1332         } else {
1333                 return true;
1334         }
1335 }
1336
1337 function getMainContext() {
1338         return this.window;
1339 }
1340
1341 function getFeedsContext() {
1342         return this.window;
1343 }
1344
1345 function getContentContext() {
1346         return this.window;
1347 }
1348
1349 function getHeadlinesContext() {
1350         return this.window;
1351 }
1352
1353 var debug_last_class = "even";
1354
1355 function debug(msg) {
1356
1357         if (debug_last_class == "even") {
1358                 debug_last_class = "odd";
1359         } else {
1360                 debug_last_class = "even";
1361         }
1362
1363         var c = document.getElementById('debug_output');
1364         if (c && c.style.display == "block") {
1365                 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1366                         c.removeChild(c.lastChild);
1367                 }
1368         
1369                 var d = new Date();
1370                 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1371                         ":" + leading_zero(d.getSeconds());
1372                 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
1373                         msg + "</li>" + c.innerHTML;
1374         }
1375 }
1376
1377 function getInitParam(key) {
1378         return init_params[key];
1379 }
1380
1381 function storeInitParam(key, value) {
1382         debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1383         init_params[key] = value;
1384 }
1385
1386 function fatalError(code, message) {
1387         try {   
1388
1389                 if (code == 6) {
1390                         window.location.href = "tt-rss.php";                    
1391                 } else if (code == 5) {
1392                         window.location.href = "update.php";
1393                 } else {
1394                         var fe = document.getElementById("fatal_error");
1395                         var fc = document.getElementById("fatal_error_msg");
1396         
1397                         if (message == "") message = "Unknown error";
1398
1399                         fc.innerHTML = "<img src='images/sign_excl.gif'> " + message + " (Code " + code + ")";
1400         
1401                         fe.style.display = "block";
1402                 }
1403
1404         } catch (e) {
1405                 exception_error("fatalError", e);
1406         }
1407 }
1408
1409 function getFeedName(id, is_cat) {      
1410         var d = getFeedsContext().document;
1411
1412         var e;
1413
1414         if (is_cat) {
1415                 e = d.getElementById("FCATN-" + id);
1416         } else {
1417                 e = d.getElementById("FEEDN-" + id);
1418         }
1419         if (e) {
1420                 return e.innerHTML.stripTags();
1421         } else {
1422                 return null;
1423         }
1424 }
1425
1426 function viewContentUrl(url) {
1427         getContentContext().location = url;
1428 }
1429
1430 function filterDlgCheckAction(sender) {
1431
1432         try {
1433
1434                 var action = sender[sender.selectedIndex].value;
1435
1436                 var form = document.forms["filter_add_form"];
1437         
1438                 if (!form) {
1439                         form = document.forms["filter_edit_form"];
1440                 }
1441
1442                 if (!form) {
1443                         debug("filterDlgCheckAction: can't find form!");
1444                         return;
1445                 }
1446
1447                 var action_param = form.action_param;
1448
1449                 if (!action_param) {
1450                         debug("filterDlgCheckAction: can't find action param!");
1451                         return;
1452                 }
1453
1454                 // if selected action supports parameters, enable params field
1455                 if (action == 4 || action == 6) {
1456                         action_param.disabled = false;
1457                 } else {
1458                         action_param.disabled = true;
1459                 }
1460
1461         } catch (e) {
1462                 exception_error(e, "filterDlgCheckAction");
1463         }
1464
1465 }
1466
1467 function explainError(code) {
1468         return displayDlg("explainError", code);
1469 }
1470
1471 // this only searches loaded headlines list, not in CDM
1472 function getRelativePostIds(id) {
1473
1474         debug("getRelativePostIds: " + id);
1475
1476         var ids = new Array();
1477         var container = document.getElementById("headlinesList");
1478
1479         if (container) {
1480                 var rows = container.rows;
1481
1482                 for (var i = 0; i < rows.length; i++) {
1483                         var r_id = rows[i].id.replace("RROW-", "");
1484
1485                         if (r_id == id) {
1486                                 if (i > 0) ids.push(rows[i-1].id.replace("RROW-", ""));
1487                                 if (i > 1) ids.push(rows[i-2].id.replace("RROW-", ""));
1488                                 if (i > 2) ids.push(rows[i-3].id.replace("RROW-", ""));
1489
1490                                 if (i < rows.length-1) ids.push(rows[i+1].id.replace("RROW-", ""));
1491                                 if (i < rows.length-2) ids.push(rows[i+2].id.replace("RROW-", ""));
1492                                 if (i < rows.length-3) ids.push(rows[i+3].id.replace("RROW-", ""));
1493
1494                                 return ids;
1495                         }
1496                 }
1497         }
1498
1499         return false;
1500 }
1501
1502 function openArticleInNewWindow(id) {
1503         try {
1504                 debug("openArticleInNewWindow: " + id);
1505
1506                 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
1507                 var wname = "ttrss_article_" + id;
1508
1509                 debug(query + " " + wname);
1510
1511                 var w = window.open("", wname);
1512
1513                 if (!w) notify_error("Failed to open window for the article");
1514
1515                 new Ajax.Request(query, {
1516                         onComplete: function(transport) { 
1517                                 open_article_callback(transport); 
1518                         } });
1519
1520
1521         } catch (e) {
1522                 exception_error("openArticleInNewWindow", e);
1523         }
1524 }
1525
1526 /* http://textsnippets.com/posts/show/835 */
1527
1528 Position.GetWindowSize = function(w) {
1529         w = w ? w : window;
1530         var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
1531         var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
1532         return [width, height]
1533 }
1534
1535 /* http://textsnippets.com/posts/show/836 */
1536
1537 Position.Center = function(element, parent) {
1538         var w, h, pw, ph;
1539         var d = Element.getDimensions(element);
1540         w = d.width;
1541         h = d.height;
1542         Position.prepare();
1543         if (!parent) {
1544                 var ws = Position.GetWindowSize();
1545                 pw = ws[0];
1546                 ph = ws[1];
1547         } else {
1548                 pw = parent.offsetWidth;
1549                 ph = parent.offsetHeight;
1550         }
1551         element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
1552         element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
1553 }
1554
1555
1556 function labeltest_callback(transport) {
1557         try {
1558                 var container = document.getElementById('label_test_result');
1559         
1560                 container.innerHTML = transport.responseText;
1561                 if (!Element.visible(container)) {
1562                         Effect.SlideDown(container, { duration : 0.5 });
1563                 }
1564
1565                 notify("");
1566         } catch (e) {
1567                 exception_error("labeltest_callback", e);
1568         }
1569 }
1570
1571 function labelTest() {
1572
1573         try {
1574                 var container = document.getElementById('label_test_result');
1575         
1576                 var form = document.forms['label_edit_form'];
1577         
1578                 var sql_exp = form.sql_exp.value;
1579                 var description = form.description.value;
1580         
1581                 notify_progress("Loading, please wait...");
1582         
1583                 var query = "backend.php?op=pref-labels&subop=test&expr=" +
1584                         param_escape(sql_exp) + "&descr=" + param_escape(description);
1585         
1586                 new Ajax.Request(query, {
1587                         onComplete: function (transport) {
1588                                 labeltest_callback(transport);
1589                         } });
1590         
1591                 return false;
1592
1593         } catch (e) {
1594                 exception_error("labelTest", e);
1595         }
1596 }
1597
1598 function isCdmMode() {
1599         return !document.getElementById("headlinesList");
1600 }
1601
1602 function getSelectedArticleIds2() {
1603         var rows = new Array();
1604         var cdm_mode = isCdmMode();
1605
1606         if (cdm_mode) {
1607                 rows = cdmGetSelectedArticles();
1608         } else {        
1609                 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1610         }
1611
1612         var ids = new Array();
1613
1614         for (var i = 0; i < rows.length; i++) {
1615                 var chk = document.getElementById("RCHK-" + rows[i]);
1616                 if (chk && chk.checked) {
1617                         ids.push(rows[i]);
1618                 }
1619         }
1620
1621         return ids;
1622 }
1623
1624 function displayHelpInfobox(topic_id) {
1625
1626         var url = "backend.php?op=help&tid=" + param_escape(topic_id);
1627
1628         var w = window.open(url, "ttrss_help", 
1629                 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
1630
1631         return false;
1632 }
1633
1634 function focus_element(id) {
1635         try {
1636                 var e = document.getElementById(id);
1637                 if (e) e.focus();
1638         } catch (e) {
1639                 exception_error(e, "focus_element");
1640         }
1641         return false;
1642 }
1643