/**
 * Copyright 2015, Government of Canada.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license.
 *
 * @providesModule Collection *
 */

var Events = require('./Events');


function Router() {

    this.events = new Events(['change'], this);

    this.refreshRoute = function () {

        var fragment = location.hash.replace('#', '').split("/");

        var base = fragment.slice(0, 2);

        if (base.length === 1) base += this.childKeys(base[0])[0];

        var children = _.find(this.structure, {key: base[0]}).children;
        
        this.route = {
            fragment: fragment,
            base: base,
            extra: fragment.slice(2),
            parent: base[0],
            child: base[1],

            children: children,
            baseComponent: _.find(children, {key: base[1]}).component
        };

        this.emit('change', this.route);

        return this.route;
    };

    // Change the route when there is the URL in the address bar is changed.
    window.onhashchange = (function () {
        // Update active states
        this.clearActive();
        this.refreshRoute();
    }).bind(this);

    // An object describing all of the routes for the application.
    this.structure = [
        
        {
            key: 'home',
            icon: 'home',

            children: [
                {key: 'welcome', component: require('virtool/js/components/Home/Welcome.jsx')}
            ]
        },

        {
            key: 'jobs',
            icon: 'briefcase',

            children: [
                {key: 'active', component: require('virtool/js/components/Jobs/Active.jsx')},
                {key: 'archived', component: require('virtool/js/components/Jobs/Archived.jsx')}
            ]
        },

        {
            key: 'samples',
            icon: 'filing',

            children: [
                {key: 'active', component: require('virtool/js/components/Samples/Active.jsx')},
                {key: 'archived', component: require('virtool/js/components/Samples/Archived.jsx')}
            ]
        },

        {
            key: 'viruses',
            icon: 'search',

            children: [
                {key: 'manage', component: require('virtool/js/components/Viruses/Manage.jsx')},
                {key: 'history', component: require('virtool/js/components/Viruses/History.jsx')},
                {key: 'index', component: require('virtool/js/components/Viruses/Index.jsx')}
            ]
        },

        {
            key: 'hosts',
            icon: 'leaf',

            children: [
                {key: 'manage', component: require('virtool/js/components/Hosts/Manage.jsx')}
            ]
        },

        {
            key: 'options',
            icon: 'wrench',

            children: [
                {key: 'general', component: require('virtool/js/components/Options/General.jsx')},
                {key: 'server', component: require('virtool/js/components/Options/Server.jsx')},
                {key: 'data', component: require('virtool/js/components/Options/Data.jsx')},
                {key: 'jobs', component: require('virtool/js/components/Options/Jobs.jsx')},
                {key: 'users', component: require('virtool/js/components/Options/Users.jsx')}
            ]
        }

    ];

    this.structure.forEach(function (base, index) {
        base.active = index === 0;

        base.children.forEach(function (child) {
            child.active = index === 0;
        });
    });

    this.setParent = function (parentKey) {
        var fragment = [parentKey, _.find(this.structure, {key: parentKey}).children[0].key];
        location.hash = fragment.join("/");
    };

    this.setChild = function (childKey) {
        location.hash = [this.route.parent, childKey].join("/");
    };

    this.setExtra = function (extra) {
        var fragment = this.route.fragment.slice(0, 2).concat(extra);
        location.hash = fragment.join("/");
    };

    this.clearExtra = function () {
        this.setExtra([]);
    };

    // Clear active states of all routes
    this.clearActive = function () {
        this.structure.forEach(function (parent) {
            parent.active = false;

            parent.children.forEach(function (child) {
                child.active = false;
            });
        });
    };

    this.route = this.refreshRoute();
}

module.exports = Router;
@@ -434,58 +434,95 @@
 base
-.length === 1) base += this.childKeys(base%5B0%5D)%5B0%5D;
+%5B0%5D === %22%22) %7B%0A            location.hash = %22home/welcome%22;%0A            return;%0A        %7D
 %0A%0A  
@@ -591,24 +591,25 @@
 ildren;%0A
+%0A
         
 %0A       
@@ -600,25 +600,49 @@
         
-%0A
+if (base.length %3E 1) %7B%0A  
         
 this.rou
@@ -629,24 +629,26 @@
 %7B%0A          
+  
 this.route =
@@ -650,16 +650,20 @@
 ute = %7B%0A
+    
         
@@ -694,24 +694,28 @@
             
+ 
+   
 base: base,%0A
@@ -714,16 +714,20 @@
 : base,%0A
+    
         
@@ -764,24 +764,28 @@
             
+    
 parent: base
@@ -789,16 +789,20 @@
 ase%5B0%5D,%0A
+    
         
@@ -826,32 +826,36 @@
 %5D,%0A%0A            
+    
 children: childr
@@ -858,16 +858,20 @@
 ildren,%0A
+    
         
@@ -936,24 +936,28 @@
 ent%0A        
+    
 %7D;%0A%0A        
@@ -952,24 +952,28 @@
 %7D;%0A%0A        
+   
+ 
 this.emit('c
@@ -998,24 +998,28 @@
 );%0A%0A        
+    
 return this.
@@ -1025,16 +1025,86 @@
 .route;%0A
+        %7D%0A%0A%0A%0A        location.hash = base%5B0%5D + %22/%22 + children%5B0%5D.key;%0A
     %7D;%0A%0A

Handle truncated URI fragments gracefully