function App() {
}

App.prototype.init = function() { 
   this.navigation = document.getElementById("navigation");
   this.breadcrumbs = document.getElementById("breadcrumbs");
   if (this.navigation) {
      //this.setupNavigation();
   }
}

App.prototype.setupNavigation = function() {
    var url = window.location.protocol+"//"+window.location.host+"/M"+window.location.pathname;
    this.metadata = new AtomFeed(null,url,null);
    var current = this;
    this.metadata.load({
       onSuccess: function() {
           DOMUtil.clearChildren(current.navigation);
           DOMUtil.clearChildren(current.breadcrumbs);
           var entry = current.metadata.firstEntry;
           var topics = [];
           for (var i=0; entry; i++) {
                var child = entry.getCategory("http://www.atomojo.org/O/relations/","child");
                if (child.length>0) {
                   var links = entry.getLinks("related");
                   var feed = links[0].href;
                   var start = feed.indexOf("/R")+2;
                   var href = feed.substring(start);
                   var title =  DOMUtil.text(entry.getTitle().xml);
                   topics.push({ title: title, href: href});
                }
                var ancestor = entry.getCategory("http://www.atomojo.org/O/relations/","ancestor");
                if (ancestor.length>0) {
                   var links = entry.getLinks("related");
                   var feed = links[0].href;
                   var start = feed.indexOf("/R")+2;
                   var href = feed.substring(start);
                   var title =  DOMUtil.text(entry.getTitle().xml);
                   var link = DOMUtil.element(current.breadcrumbs,
                    { localName: "a",
                      namespace: "http://www.w3.org/1999/xhtml",
                      attributes: { href: href },
                      children: [
                          title
                      ]
                    },true,true);
                    link.className = "topic";
                }
                entry = entry.next;
           }
           topics.sort(function(a,b) {
                return a.title.localeCompare(b.title);
           });
           for (var i=0; i<topics.length; i++) {
                   var div = DOMUtil.element(current.navigation,
                    { localName: "div",
                      namespace: "http://www.w3.org/1999/xhtml",
                      children: [
                         { localName: "a",
                           attributes: { href: topics[i].href },
                           children: [
                               topics[i].title
                           ]
                        }
                      ]
                    },true,true);
                    div.className = "topic";
           }
       },
       onFailure: function() {
       }
    });
}