$(document).ready(function() {
   var feedUrls = {
      "ss-newsfeed": "news.xml",
      "ss-sportfeed": "sport.xml",
      "ss-fuzofeed": "futurezone.xml",
      "ss-sciencefeed": "science.xml",
      "ss-debattefeed": "debatten.xml"
   };
   
   var liveStreamUrls = {
      "liveoe1": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=740,height=250',
      "liveoe3": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=672',
      "livefm4": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "liveburgenland": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livenoe": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "liveooe": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livewien": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livesalzburg": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livesteiermark": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livekaernten": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livetirol": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466',
      "livevbg": 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=328,height=466'
   }
   
   var loadFeed = function(feedUrl, $el, maxItems) {
      var limit = maxItems || 3;
      
      $.getFeed({
         url: feedUrl,
         success: function(feed){
            var list = [];
            var item;
            for (var i = 0; i < limit && i < feed.items.length; i++) {
               item = feed.items[i];
               $el.append(["<li><a href='", item.link, "'>", item.title, "</a></li>"].join(""));
            }
         }
      });
   }
   
   $(".ss-sortOnReady li").sortElements(function(a, b) {
      return $(a).text().toLowerCase() > $(b).text().toLowerCase() ? 1 : -1;
   });
   
   $(".ss-loadFeed").each(function(index) {
      $this = $(this);
      
      var id = $this.attr('id');
      if (id != null && id.indexOf("ss-") === 0) {
         loadFeed(feedUrls[$this.attr('id')], $this);
      }
   });
   
   $(".livelink a").click(function(e) {
      $this = $(this);
      
      var streamConfig = liveStreamUrls[$this.attr("id")];
      if (streamConfig != null) {
         window.open($this.attr("href"), $this.attr("id"), streamConfig);
      }
      return false;
   });
});

