// ==UserScript==
// @name           Prototype JS Online Sidebar
// @namespace      http://people.n0i.net/altblue/greasemonkey/
// @description    Sidebar hack for Prototype JS' API
// @include        http://prototypejs.org/api?sidebar
// ==/UserScript==

var GM = {
  name:      'prototype-js-sidebar',
  namespace: 'http://people.fits.ro/altblue/n0i/greasemonkey/',

  initialize: function() {
    this.removeScripts();
    this.removeStyleSheets();

    this.jsTop = this.cssTop = document.getElementsByTagName('head')[0];
    this.jsREF = document.createElement('script');
    this.jsREF.type = 'text/javascript';
    this.cssREF = document.createElement('link');
    this.cssREF.type = 'text/css';
    this.cssREF.rel  = 'stylesheet';

	this.addStyleSheets(['sidebar.css']);
    this.addScripts(['prototype.js', 'effects.js', 'neodom.js'], true);
    this.addScripts(['sidebar.js']);
  },

  addScripts: function(names, shared) {
    names.forEach(function(name){
      var s = this.jsREF.cloneNode(true);
      s.src = this.namespace + (shared ? 'lib' : this.name) + '/' + name;
      this.jsTop.appendChild(s);
    }, this);
  },

  addStyleSheets: function(names, shared) {
    names.forEach(function(name){
      var s = this.cssREF.cloneNode(true);
      s.href = this.namespace + (shared ? 'lib' : this.name) + '/' + name;
      this.cssTop.appendChild(s);
    }, this);
  },

  removeScripts: function() {
    Array.forEach(
      document.getElementsByTagName('script'),
      function(s) {
        s.parentNode.removeChild(s);
      }
    );
  },

  removeStyleSheets: function() {
    Array.forEach(
      document.getElementsByTagName('style'),
      function(s) {
        s.parentNode.removeChild(s);
      }
    );
    Array.forEach(
      document.getElementsByTagName('link'),
      function(s) {
        if (s.getAttribute('type') == 'text/css')
          s.parentNode.removeChild(s);
      }
    );
  }

};

GM.initialize();

