// needs at.orf library currently located
// at http://static.orf.at/at.orf.Banner/at.orf.js

at.orf.RssBox = function(id) {
   if (!at.orf.Feeds || !at.orf.Feeds[id])
      return;

   var query = at.orf.getQuery(location.search);
   var feed = at.orf.Feeds[id];
   for (var p in feed)
      this[p] = feed[p];
   for (var n in feed.items)
      this.items[n] = new at.orf.RssBoxItem(feed.items[n]);
   this.maxItems = 99;
   this.compactView = false;
   this.render = function() {
      document.write(this.renderAsString());
      return;
   };
   this.renderAsString = function() {
      var skin = new at.orf.Skin('<table class="rssBox" width="180" bgcolor="black" border="0" cellspacing="1" cellpadding="5"><tr class="rssBoxHead" bgcolor="white"><td class="rssBoxHead"><p class="rssBoxTitle"><a href="<% url %>" class="rssBoxTitle"><% title %></a><br /><small class="rssBoxTitle" title="Letzte Aktualisierung (Shift + Reload)"><% pubdate %></small></p></td></tr><tr class="rssBoxBody" bgcolor="white"><td class="rssBoxBody"><% items %></td></tr></table>');
      var str = new String();
      var defaultMax = isNaN(query.maxItems) ? 
         this.maxItems : parseInt(query.maxItems, 10);
      var max = Math.min(defaultMax, this.items.length, 99);
      for (var i=0; i<max; i++) {
         var c = (query.compactView == "true" || 
            (this.compactView == true && !query.compactView));
         str += this.items[i].renderAsString(c);
      }
      this.items = str;
      return skin.renderAsString(this);
   };
   return this;
};


at.orf.RssBoxItem = function(param) {
   for (var p in param)
      this[p] = param[p];
   if (this.description)
      this.description = "<br />" + this.description;
   if (this.subject)
      this.subject = '<small class="rssBoxItem">' + this.subject + "</small><br />";
   if (this.href) {
      this.openlink = '<a href="' + this.href + '" class="rssBoxItem">';
      this.closelink = "</a>";
   }
   this.renderAsString = function(compactView) {
      var skin = new at.orf.Skin('<p class="rssBoxItem"><% subject %><% openlink %><% title %><% closelink %><% description %></p>');
      var param = Object.clone(this);
      if (compactView) {
         param.subject = "";
         param.description = "";
      }
      return skin.renderAsString(param);
   };
   this.render = function() {
      document.write(this.renderAsString());
      return;
   };
   return this;
};


at.orf.Skin = function(str) {
   this.source = str;
   this.render = function(param) {
      document.write(this.renderAsString(param));
      return;
   };
   this.renderAsString = function(param) {
      var str = this.source;
      var re = new RegExp("<% *([^ %>]*) *%>");
      var cnt = 0;
      while (re.test(str) && cnt++ < 100) {
         var result = re.exec(str);
         // we need to replace any $ with a $$ and that's $$$$, in fact:
         var newstr = param[result[1]].replace(/\$/g, "$$$$");
         str = str.replace(re, newstr ? newstr : "");
      }
      return str;
   };
   return this;
};

