var org = {p3k: ({url:"http://jotwe.wordpress.com/feed/", xml:"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss version=\"2.0\"\n\txmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n\txmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"\n\txmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n\txmlns:atom=\"http://www.w3.org/2005/Atom\"\n\txmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"\n\txmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n\txmlns:georss=\"http://www.georss.org/georss\" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\" xmlns:media=\"http://search.yahoo.com/mrss/\"\n\t>\n\n<channel>\n\t<title></title>\n\t<atom:link href=\"http://jotwe.wordpress.com/feed/\" rel=\"self\" type=\"application/rss+xml\" />\n\t<link>http://jotwe.wordpress.com</link>\n\t<description></description>\n\t<lastBuildDate>Fri, 28 May 2010 09:57:29 +0000</lastBuildDate>\n\t<language>de</language>\n\t<sy:updatePeriod>hourly</sy:updatePeriod>\n\t<sy:updateFrequency>1</sy:updateFrequency>\n\t<generator>http://wordpress.com/</generator>\n<cloud domain='jotwe.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />\n<image>\n\t\t<url>http://www.gravatar.com/blavatar/b9dbdc20fd1d3553dfc5ad651893e125?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>\n\t\t<title></title>\n\t\t<link>http://jotwe.wordpress.com</link>\n\t</image>\n\t<atom:link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"http://jotwe.wordpress.com/osd.xml\" title=\"\" />\n\t<atom:link rel='hub' href='http://jotwe.wordpress.com/?pushpress=hub'/>\n\t\t<item>\n\t\t<title>Is Alfred the new Quicksilver?</title>\n\t\t<link>http://jotwe.wordpress.com/2010/05/28/is-alfred-the-new-quicksilver/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/05/28/is-alfred-the-new-quicksilver/#comments</comments>\n\t\t<pubDate>Fri, 28 May 2010 09:57:29 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Apps]]></category>\n\t\t<category><![CDATA[Mac/Apple]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=194</guid>\n\t\t<description><![CDATA[Having a quick launcher and the possibility to do almost everything with Quicksilver made me a fan of it for years now. Now there is a new app that came to my attention. It is called Alfred and supposedly picks up where Quicksilver left off. Still in beta it makes a good first impression, but [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=194&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/05/28/is-alfred-the-new-quicksilver/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Face it! We like our privacy</title>\n\t\t<link>http://jotwe.wordpress.com/2010/05/12/face-it-we-like-our-privacy/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/05/12/face-it-we-like-our-privacy/#comments</comments>\n\t\t<pubDate>Wed, 12 May 2010 15:35:28 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Lifestyle]]></category>\n\t\t<category><![CDATA[Social Web]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=184</guid>\n\t\t<description><![CDATA[All over the web right now are the discussions about Facebook&#8217;s privacy settings. For me it is sad to see, that more and more the privacy settings are being constrained. I don&#8217;t want to have all my details visible to everyone. But step by step Facebook is changing the settings and even overwriting existing settings, [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=184&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/05/12/face-it-we-like-our-privacy/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Romancier und Gentleman Farmer: Martin Suter</title>\n\t\t<link>http://jotwe.wordpress.com/2010/04/13/romancier-und-gentleman-farmer-martin-suter/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/04/13/romancier-und-gentleman-farmer-martin-suter/#comments</comments>\n\t\t<pubDate>Tue, 13 Apr 2010 18:49:54 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Art]]></category>\n\t\t<category><![CDATA[Books]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=173</guid>\n\t\t<description><![CDATA[Ein Portrait des Schweizer Erfolgsautors Martin Suter. Ein Film von: Hilde Heim. Deutschland, 2009, 43 min. Ausgestrahlt: arte am 10.01.2010 Teil 1: Teil 2: Teil 3: Teil 4: Teil 5:<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=173&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/04/13/romancier-und-gentleman-farmer-martin-suter/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\n\t\t<media:content url=\"http://img.youtube.com/vi/0k-uEIiJbm4/2.jpg\" medium=\"image\" />\n\n\t\t<media:content url=\"http://img.youtube.com/vi/YAxELH_Oc9k/2.jpg\" medium=\"image\" />\n\n\t\t<media:content url=\"http://img.youtube.com/vi/4NLl6BWX1Fk/2.jpg\" medium=\"image\" />\n\n\t\t<media:content url=\"http://img.youtube.com/vi/QRx51QwU1HA/2.jpg\" medium=\"image\" />\n\n\t\t<media:content url=\"http://img.youtube.com/vi/0Kb7lvMvC-s/2.jpg\" medium=\"image\" />\n\t</item>\n\t\t<item>\n\t\t<title>Learn Polish</title>\n\t\t<link>http://jotwe.wordpress.com/2010/04/13/learn-polish/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/04/13/learn-polish/#comments</comments>\n\t\t<pubDate>Tue, 13 Apr 2010 17:34:45 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Languages]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=101</guid>\n\t\t<description><![CDATA[Often people give me a wired look if I tell them that I would like to learn Polish. Apparently not many people besides me think its worth learning it. But&#8230; I think it is a very beautiful language and for everyone else that would like to give it a try a organized some of my [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=101&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/04/13/learn-polish/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Nothing but fluorescent light</title>\n\t\t<link>http://jotwe.wordpress.com/2010/04/13/nothing-but-fluorescent-light/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/04/13/nothing-but-fluorescent-light/#comments</comments>\n\t\t<pubDate>Tue, 13 Apr 2010 17:07:15 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Art]]></category>\n\t\t<category><![CDATA[Media Art]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=73</guid>\n\t\t<description><![CDATA[Dan Flavin was an american light artist, that was well known for his minimal light installations. Steve Morse, the conservator for the Dan Flavin Studio, is interviewed about how the exhibition &#8222;Dan Flavin: Constructed Light at the Pulitzer Foundation for the Arts&#8220; was installed. Morse quotes the artist about his work as follows: &#8222;It is [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=73&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/04/13/nothing-but-fluorescent-light/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\n\t\t<media:content url=\"http://img.youtube.com/vi/XJTPaIorjoM/2.jpg\" medium=\"image\" />\n\t</item>\n\t\t<item>\n\t\t<title>Symboldschungel</title>\n\t\t<link>http://jotwe.wordpress.com/2010/04/13/symboldschungel/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/04/13/symboldschungel/#comments</comments>\n\t\t<pubDate>Tue, 13 Apr 2010 16:41:12 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Food]]></category>\n\t\t<category><![CDATA[Lifestyle]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=141</guid>\n\t\t<description><![CDATA[Wer hat denn heutzutage noch den Durchblick im Symboldschungel beim Einkaufen? Eigentlich sollen diverse Siegel einem das Leben erleichtern und helfen die geeigneten Produkte zu finden, aber wer wei\xdf schon so genau wof\xfcr sie stehen? Der Siegel Spiegel von Greenpeace hilft zwischen den vielen \xd6ko- und Bio-Siegeln zu unterscheiden. Auch auf den Webseiten www.bio-siegel.de/ und [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=141&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/04/13/symboldschungel/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Mighty Mouse &#8211; Mighty annoying</title>\n\t\t<link>http://jotwe.wordpress.com/2010/04/02/mighty-mouse-mighty-annoying/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/04/02/mighty-mouse-mighty-annoying/#comments</comments>\n\t\t<pubDate>Fri, 02 Apr 2010 12:43:13 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Mac/Apple]]></category>\n\t\t<category><![CDATA[Uncategorized]]></category>\n\t\t<category><![CDATA[apple]]></category>\n\t\t<category><![CDATA[mac]]></category>\n\t\t<category><![CDATA[mighty]]></category>\n\t\t<category><![CDATA[mouse]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=131</guid>\n\t\t<description><![CDATA[Together with my iMac I received a mighty mouse. Still, after more than a year I could not get used to it. Although I like the scroll ball that allows to scroll not only up and down, but also from left to right, it totally annoys me that the scroll ball gets stuck so fast. [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=131&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/04/02/mighty-mouse-mighty-annoying/feed/</wfw:commentRss>\n\t\t<slash:comments>2</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Readability</title>\n\t\t<link>http://jotwe.wordpress.com/2010/03/23/readability/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/03/23/readability/#comments</comments>\n\t\t<pubDate>Tue, 23 Mar 2010 10:05:12 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Apps]]></category>\n\t\t<category><![CDATA[Uncategorized]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=123</guid>\n\t\t<description><![CDATA[When I just researched for my post on Corot-9b I read several articles online. And I remembered my bookmarklet called Readability. It removes all the advertisements that are annoyingly blinking when you try to read something. Just by a simple click. The website describes it as follow: &#8216;Readability is a simple tool that makes reading [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=123&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/03/23/readability/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Corot-9B &#8211; The next exoplanet</title>\n\t\t<link>http://jotwe.wordpress.com/2010/03/23/corot-9b-the-next-exoplanet/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/03/23/corot-9b-the-next-exoplanet/#comments</comments>\n\t\t<pubDate>Tue, 23 Mar 2010 09:57:48 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Readings]]></category>\n\t\t<category><![CDATA[Science]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=117</guid>\n\t\t<description><![CDATA[A new exoplanet called Corot-9B was recently discussed in nature. It was found with the help of the COROT (COnvection\xa0ROtation and planetary\xa0Transits) satellite, being one of already 400 discovered extrasolar planets. Why is Corot-9b so special? &#8216;Corot-9b stands alone because it is considered to be able to provide valuable scientific discoveries in the study of [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=117&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/03/23/corot-9b-the-next-exoplanet/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\t</item>\n\t\t<item>\n\t\t<title>Skitch &#8211; Image sharing  and editing made easy</title>\n\t\t<link>http://jotwe.wordpress.com/2010/03/21/skitch-image-sharing-and-editing-made-easy/</link>\n\t\t<comments>http://jotwe.wordpress.com/2010/03/21/skitch-image-sharing-and-editing-made-easy/#comments</comments>\n\t\t<pubDate>Sun, 21 Mar 2010 12:03:09 +0000</pubDate>\n\t\t<dc:creator>jotwe</dc:creator>\n\t\t\t\t<category><![CDATA[Apps]]></category>\n\t\t<category><![CDATA[Mac/Apple]]></category>\n\t\t<category><![CDATA[mac]]></category>\n\t\t<category><![CDATA[skitch]]></category>\n\t\t<category><![CDATA[tool]]></category>\n\n\t\t<guid isPermaLink=\"false\">http://jotwe.wordpress.com/?p=113</guid>\n\t\t<description><![CDATA[For a long time I used ImageWell by XtraLean to prepare pictures for the web, or resize them quickly. At some point the former freeware started to cost $19.95\xa0and so I decided to stick to the last version that was available for free. Unfortunately I had a lot of problems with it crashing suddenly. Now [...]<img alt=\"\" border=\"0\" src=\"http://stats.wordpress.com/b.gif?host=jotwe.wordpress.com&blog=1034118&post=113&subd=jotwe&ref=&feed=1\" />]]></description>\n\t\t<wfw:commentRss>http://jotwe.wordpress.com/2010/03/21/skitch-image-sharing-and-editing-made-easy/feed/</wfw:commentRss>\n\t\t<slash:comments>0</slash:comments>\n\t\n\t\t<media:content url=\"\" medium=\"image\">\n\t\t\t<media:title type=\"html\">jotwe</media:title>\n\t\t</media:content>\n\n\t\t<media:content url=\"http://img.skitch.com/20100321-k4smkcdt4sxx5u9udfa8w51q51.preview.jpg\" medium=\"image\">\n\t\t\t<media:title type=\"html\">skitch_screenshot_signup</media:title>\n\t\t</media:content>\n\t</item>\n\t</channel>\n</rss>\n", message:undefined, modified:(new Date(1284015718498)), baseUri:"http://p3k.org/rss/", query:"frameColor=black&textColor=black&_=1256505988157&titleBarColor=white&compact=&fontFace=&align=&showXmlButton=true&maxItems=20&boxFillColor=white&titleBarTextColor=black&width=615&url=http://jotwe.wordpress.com/feed/&", param:{textColor:"black", width:"615", align:"", fontFace:"", url:"http://jotwe.wordpress.com/feed/", compact:"", frameColor:"black", showXmlButton:"true", boxFillColor:"white", _:"1256505988157", titleBarColor:"white", maxItems:"20", titleBarTextColor:"black"}, box:"<table class=\"rssBox\" width=\"${width}\" bgcolor=\"${frameColor}\" \n      style=\"table-layout: fixed; overflow: hidden; font-family: ${fontFace};\" \n      align=\"${align}\" cellspacing=\"1\" cellpadding=\"7\" border=\"0\">\n<tr bgcolor=\"${titleBarColor}\">\n<td>\n   ${xmlButton}\n   <strong>${title}</strong>\n   <div>\n      <small style=\"color: ${titleBarTextColor};\">${date}</small>\n   </div>\n</td>\n</tr>\n<tr bgcolor=\"${boxFillColor}\">\n<td class=\"rssBoxContent\" style=\"color: ${textColor};\">\n   ${image}\n   ${items}\n   ${input}\n   <div class=\"rssBoxPromo\" style=\"margin-top: 0px; text-align: left; \n         font: 9px verdana, sans-serif\">\n      RSS box by <a href=\"http://p3k.org/rss\">p3k.org</a>.\n   </div>\n</td>\n</tr>\n</table>\n\n", image:"<a href=\"${link}\"><img src=\"${source}\" width=\"${width}\" height=\"${height}\" \nalt=\"${title}\" title=\"${title}\" align=\"${align}\" valign=\"${valign}\" \nhspace=\"${hspace}\" vspace=\"${vspace}\" border=\"0\" /></a>\n\n", input:"<form method=\"get\" action=\"${link}\">\n${description}\n<input type=\"text\" name=\"${name}\" size=\"15\" /> <input type=\"submit\" \nvalue=\"${title}\" />\n</form>\n\n", item:"<div class=\"rssBoxItemContent\">\n${title}\n${break}\n${description}\n${buttons}\n</div><br />\n\n", date:"${year}-${month}-${day}, ${hours}:${minutes}h\n\n", link:"<a class=\"${class}\" href=\"${link}\" style=\"${style}\">${text}</a>\n", error:"<?xml version=\"1.0\"?>\n<rss version=\"error\">\n   <channel>\n      <title>RSS Box Error</title>\n      <link>${link}</link>\n      <description>This output was automatically generated to report an error \n      that occurred during a request to the JavaScript RSS Box Viewer.</description>\n      <item>\n         <title>Oops, something went wrong...</title>\n         <description>An error occurred while processing the request to the \n         JavaScript RSS Box Viewer.</description>\n      </item>\n      <item>\n         <title>An error message was returned by the server.</title>\n         <description>${message}</description>\n      </item>\n      <item>\n         <description>Most likely, this might have happened because of a \n         non-existent or invalid RSS feed URL. Please check and possibly correct \n         your input, then try again.</description>\n      </item>\n   </channel>\n</rss>\n\n"})};
function debug(str) {
   return document.write('<p><span style="background-color: yellow;">', 
         str, '</span><p>');
}

org.p3k.RssBox = function() {
   var ref;
   var ISOPATTERN = /([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9:]+).*$/;
   
   var data = org.p3k;
   data.defaults = {
      url: "http://blog.p3k.org/rss",
      maxItems: 7,
      width: 200,
      align: "",
      frameColor: "black",
      titleBarColor: "lightblue",
      titleBarTextColor: "black",
      boxFillColor: "white",
      textColor: "black",
      showXmlButton: "",
      compact: ""
   };
   
   var baseUri = data.baseUri;
    
   var getColor = function(str) {
      if (str.length === 6 && parseInt(str, 16) && str.indexOf("#") !== 0) {
         str = "#" + str;
      }
      return str.toLowerCase();
   }

   var value;
   for (var i in data.defaults) {
      value = data.param[i];
      if (!value || value.length === 0) {
         data.param[i] = data.defaults[i];
      } else if (i.indexOf("Color") > 0) {
         data.param[i] = getColor(value);
      }
   }

   // FIXME: Ugly work-around for many boxes using too small width 
   // values because the former version did not show the exact output.
   if (data.param.javascript && data.param.width < 200) {
      data.param.width = 200;
   }
   // Remove obsolete parameters from param and query
   delete data.param.javascript;
   data.query = data.query.replace("javascript=true", "");   

   var NAMESPACES = {
      dc: "http://purl.org/dc/elements/1.1/",
      rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   };

   var getUrl = function(url) {
      url = baseUri + "proxy.r?" + encodeURIComponent(url);
   
      var HttpRequest = function() {
         return (typeof XMLHttpRequest !== "undefined") ?
            new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
      }
   
      var request = new HttpRequest();
      request.open("GET", url, false);
      request.send(null);   
      if (!request.getResponseHeader("Date")) {
         var cached = request;
         var ifModifiedSince = cached.getResponseHeader("Last-Modified") || 
               new Date(0); // January 1, 1970
         request = new HttpRequest();
         request.open("GET", url, false);
         request.setRequestHeader("If-Modified-Since", ifModifiedSince);
         request.send("");
         if (request.status === 304) {
            request = cached; 
         }
      }
      return request;
   }
   
   var getDocument = function(source) {
      if (source) {
         if (document.implementation.createDocument) {
            var parser = new DOMParser();
            var doc = parser.parseFromString(source, "text/xml");
            return doc;
         } else if (window.ActiveXObject) {
            var doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = "false";
            doc.loadXML(source);
            return doc;
         }
      }
      return null;
   }
   
   var getError = function() {
      var msg = null, root;
      if (!xml || data.message) {
         msg = data.message || "Unknown error.";
      } else if (xml.parseError && xml.parseError.errorCode) {
         msg = xml.parseError.reason; // IExplore
      } else if (root = xml.documentElement) {
         var errorNode;
         if (root.nodeName === "parsererror") {
            msg = xml.documentElement.textContent; // Mozilla
         } else if ((errorNode = root.childNodes[0]) && 
               errorNode.nodeName === "parsererror") {
            msg = errorNode.textContent; // Safari
         } else if (!/rss|rdf|scriptingNews/i.test(xml.documentElement.nodeName)) {
            msg = "Incompatible data format. Are you sure this is an RSS feed?";
         }
      }
      return msg;
   }
   
   var getNode = function(parent, name, namespace) {
      if (namespace) {
         if (typeof parent.getElementsByTagNameNS === "undefined") {
            var elements = parent.getElementsByTagName(namespace + ":" + name);
         } else {
            var elements = parent.getElementsByTagNameNS(NAMESPACES[namespace], name);
         }
      } else {
         var elements = parent.getElementsByTagName(name);
      }
      if (elements && elements[0]) {
         return elements[0];
      }
      return null;
   }
   
   var getText = function(node) {
      if (node && node.childNodes && node.childNodes.length > 0) {
         return node.childNodes[0].nodeValue || "";
      }
      return "";
   }
   
   var trim = function(str) {
      if (str) {
         return str.replace(/^\s*(\S*)\s*$/, "$1");
      }
      return "";
   }
   
   var padZero = function(n) {
      if (n < 10) {
         return "0" + n
      }
      return n;
   }

   var encodeXml = function(str) {
      if (!str) {
         return "";
      }
      return str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, "&amp;");
   }
      
   var render = function(template, param) {
      if (!template || !param) {
         return template;
      }
      template = template.replace(/\$\{([^}]+)\}/g, function() {
         var key = arguments[1];
         return param[key] || "";
      });
      return template;
   }
   
   var renderDate = function(date) {
      if (date.constructor !== Date) {
         var str = String(date);
         var millis = Date.parse(str.replace(ISOPATTERN, "$1/$2/$3 $4"));
         if (millis) {
            date = new Date(millis);
         } else {
            date = new Date;
         }
      }

      return render(data.date, {
         year: date.getFullYear(),
         month: padZero(date.getMonth() + 1),
         day: padZero(date.getDate()),
         hours: padZero(date.getHours()),
         minutes: padZero(date.getMinutes()),
         seconds: padZero(date.getSeconds()),
         timeZone: "" // date.getTimezoneOffset()
      });
   }
   
   var renderButtons = function(enclosure, source) {
      var result = "";
      if (enclosure && enclosure.link) {
         result += render(data.image, {
            source: baseUri + "enclosure.gif",
            title: enclosure.type,
            link: encodeURI(enclosure.link),
            width: 13,
            height: 16
         });
      }
      if (source && source.link) {
         result += render(data.image, {
            source: baseUri + "source.gif",
            title: source.title,
            link: encodeURI(source.link),
            width: 15,
            height: 15
         });
      }
      return result;         
   }
   
   var param = data.param;
   var rss = data.rss = {items: []};
   var xml = getDocument(data.xml);

   rss.error = getError();
   if (rss.error !== null) {
      xml = getDocument(render(data.error, {
         link: baseUri + "?" + encodeXml(data.query),
         message: encodeXml(rss.error)
      }));
      param.compact = 0;
      param.showXmlButton = 1;
   }

   var root = xml.documentElement;
   var type = root.nodeName;

   if (type === "scriptingNews") {
      var channel = getNode(xml, "header");
      rss.format = "Scripting News";
      rss.version = getText(getNode(channel, "scriptingNewsVersion"));
      rss.title = getText(getNode(channel, "channelTitle"));
      rss.description = getText(getNode(channel, "channelDescription"));
      rss.link = getText(getNode(channel, "channelLink"));
      if (ref = getText(getNode(channel, "imageUrl"))) {
         ref = rss.image = {source: ref};
         ref.title = getText(getNode(channel, "imageTitle"));
         ref.link = getText(getNode(channel, "imageLink"));
         ref.width = getText(getNode(channel, "imageWidth"));
         ref.height = getText(getNode(channel, "imageHeight"));
         ref.description = getText(getNode(channel, "imageCaption"));
      }
   } else {
      var channel = getNode(xml, "channel");
      rss.format = "RSS";
      rss.version = (type === "rdf:RDF") ? "1.0" : 
            root.getAttribute("version");
      rss.title = getText(getNode(channel, "title"));
      rss.description = getText(getNode(channel, "description"));
      rss.link = getText(getNode(channel, "link"));
      var image = getNode(xml, "image");
      if (image) {
         ref = rss.image = {};
         ref.source = getText(getNode(image, "url"));
         ref.title = getText(getNode(image, "title"));
         ref.link = getText(getNode(image, "link"));
         ref.width = getText(getNode(image, "width"));
         ref.height = getText(getNode(image, "height"));
         ref.description = getText(getNode(image, "description"));
      }
   }
   
   if (type === "rdf:RDF") {
      rss.date = renderDate(getText(getNode(channel, "date", "dc")) || data.modified);
      rss.rights = getText(getNode(channel, "creator", "dc"));
      var input = getNode(root, "textinput");
      if (input && !getNode(input, "link")) {
         input = root.getElementsByTagName("textinput")[1];
      }
      if (input) {
         ref = rss.input = {};
         ref.link = getText(getNode(input, "link"));
         ref.description = getText(getNode(input, "description"));
         ref.name = getText(getNode(input, "name"));
         ref.title = getText(getNode(input, "title"));
      }
   } else {
      rss.date = renderDate(getText(getNode(channel, "lastBuildDate") || getText(getNode(channel, "pubDate"))) || data.modified);
      rss.rights = getText(getNode(channel, "copyright"));
   }
   
   var item, text, node;
   var items = xml.getElementsByTagName("item");

   for (var i=0; i<Math.min(items.length, param.maxItems); i+=1) {
      item = items[i];

      if (type === "scriptingNews") {
         ref = {title: ""};
         ref.description = getText(getNode(item, "text")).replace(/\n/g, " ");
         ref.link = getText(getNode(item, "link"));
         if (text = trim(getText(getNode(item, "linetext")).replace(/\n/g, " "))) {
            ref.description = ref.description.replace(new RegExp(text), 
                  '<a href="' + getText(getNode(item, "url")) + '">' + text + '</a>');
         }
      } else {
         ref = {
            title: getText(getNode(item, "title")),
            description: getText(getNode(item, "description")),
            link: getText(getNode(item, "link") || getNode(item, "guid"))
         };
     }

     if (node = getNode(item, "source")) {
        ref.source = {
           link: node.getAttribute("url"),
           title: getText(node)
        }
     }
     
     if (node = getNode(item, "enclosure")) {
        ref.enclosure = {
           link: node.getAttribute("url"),
           length: node.getAttribute("length"),
           type: node.getAttribute("type")
        }
     }
     
     if (node = getNode(item, "category")) {
        ref.category = {
           domain: node.getAttribute("domain") || "",
           content: getText(node)
        }
     }
     
     rss.items.push(ref);
   }
   
   var item, items = "";
   for (var i=0; i<rss.items.length; i+=1) {
      item = rss.items[i];
      items += render(data.item, {
         title: new function() {
            var title = (!param.compact ? "<strong>" : "");
            if (item.link) {
               title += render(data.link, {
                  link: encodeURI(item.link),
                  text: item.title,
                  'class': "rssBoxItemTitle"
               });
            } else {
               title += item.title;
            }
            !param.compact && (title += "</strong>");
            return new String(title); // FIXME: Funny, title alone will be rendered as [object]
         }(),
         'break': item.title && item.description ? "<br />" : "",
         description: (!param.compact || !item.title) && item.description,
         buttons: renderButtons(item.enclosure, item.source)
      });
   }
   
   var box = render(data.box, {
      title: rss.link ? render(data.link, {
         link: encodeURI(rss.link),
         text: rss.title,
         'class': "rssBoxTitle",
         style: "color: " + param.titleBarTextColor
      }) : rss.title,
      description: rss.description,
      items: items,

      xmlButton: param.showXmlButton && render(data.image, {
         link: param.url,
         source: baseUri + "rss.png",
         title: rss.format + " " + rss.version,
         width: 16,
         height: 16,
         align: "right",
         hspace: 3
      }),
      
      image: !param.compact && rss.image && render(data.image, {
         link: encodeURI(rss.image.link),
         source: rss.image.source,
         width: rss.image.width,
         height: rss.image.height,
         title: rss.image.title,
         align: "right",
         valign: "baseline",
         hspace: 5,
         vspace: 5
      }),
      
      input: !param.compact && rss.input && render(data.input, {
         link: encodeURI(rss.input.link),
         description: rss.input.description,
         name: rss.input.name,
         title: rss.input.title
      }),
      
      date: rss.date,
      width: param.width,
      frameColor: param.frameColor,
      fontFace: param.fontFace,
      align: param.align,
      titleBarColor: param.titleBarColor,
      titleBarTextColor: param.titleBarTextColor,
      boxFillColor: param.boxFillColor,
      textColor: param.textColor
   });

   if (!window.rssBoxSetup) {
      document.write(box);
   }
   
   return box;
};

org.p3k.RssBox();

