/**
 * delta.js -- changes the website!
 * Does some things.
 */
 
/**
 * THIS WAS RIPPED FROM ANOTHER SCRIPT I WROTE
 * THEY DID SIMILAR THINGS BUT NOT REALLY WELL MAYBE
 */
 
/**
 * IN THE END THIS IS ALL TEMPORARY ANYWAY
 * EVERYTHING IS TEMPORARY
 */
 
(function($) {
  $(function() { delta.init(); });
  
  var delta = {
    bind: function() {
      var self = this;
      
      $('a').each(function() {
        if (this.href.indexOf('#') > -1) {
          $(this).unbind('click').click(function() { 
            self.load(self.hash(this.href)); 
          });
        }
        else if (this.innerHTML.substr(-1) != '\u2197') {
          $(this).append(' \u2197');
        }
      });
    },
    
    find: function(uri, tree, d) {
      tree = tree || window.tree;
      d = d || ['/', ''];
      
      uri = typeof uri == 'string' ? uri.split('/') : uri;
        
      for (var i = 0; i < tree.length; i++) {
        var c = tree[i];
        
        if (c.name == uri[0]) {
          d[0] += uri[0];
          d[1] += ' \u00bb ' + c.title;
          
          if (c.type == 'tree') {
            d[0] += '/';
            
            if (uri.length - 1) {
              return this.find(uri.slice(1), c.tree, d);
            }
          }
          
          return { name: d[0], title: d[1] };
        }
      }
    },
    
    hash: function(str) {
      str = String(str || window.location.hash);
      var i = str.indexOf('#/');

      if (i >= 0) {
        return str.substr(i + 2);
      }
    },
    
    init: function() {
      /*
      // NO
      
      $('div#nfo .tin').tin({ spacing: 1 });
      
      // Colorbox integration!!
      $('div#nfo .tin ul li').each(function() {});
      */
    
      /*
      // MENU IS STATIC IN HTML
      // WTH IS THE POINT OF THIS SCRIPT I CANT REMEMBER
      
      $('div#box').prepend('<ul id="nav"></ul>');
      
      // Generates navigation.
      $('ul#nav').append(this.menu());

      $('ul#nav li ul').each(function(s) {
        (s = $(this)).width(s.parent('li').width() + 20 + 2);
      });
      */
      
      this.bind();
      this.load(this.hash() || window.tree[0].name);
    },
    
    load: function(uri) {
      var doc = this.find(uri), self = this;
      
      if (doc) {
        this.puts('').toggleClass('load', !0);
        
        if (doc.name.substr(-1) == '/') {
          doc.name += 'index';
        }
        
        $.ajax({
          success: function(data) {
            self.puts(data).toggleClass('load', !1);
            
            // Sets title (path)...
            document.title = 'Team 116' + doc.title;
            
            self.bind();
          },
          
          url: 'docs' + doc.name
        });
      }
    },
    
    menu: function(tree, d) {
      /* WHY IS THIS HERE */
    },
    
    puts: function(html) {
      return $('div#out').html(html);
    }
  };
})
(jQuery);
