/* js
   
   This file is part of arena.

   arena is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License or, at your
   option, any later version.

   arena is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with arena.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This file defines the base class, from which all others are derived.
*/

// The base class
function AnObject() {
  return this;
}

// Retrieve the name of the class
AnObject.prototype.className = function() {
  return this.constructor.name;
}

// Uppercase letters
const upLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Lowercase letters
const lowLetters = "abcdefghijklmnopqrstuvwxyz";

// Construct a pretty "display name for the class"
AnObject.prototype.displayName = function() {
  // Get the class name
  myName = this.constructor.name;
  result = "";
  index = 0;
  char = "";
  
  for (index = 0; index < myName.length(); index++) {
    char = myName.charAt(index);

    // Insert a space into the output for each capital letter in the name
    if ( (upLetters.indexOf(char) >= 0) && (index > 0) ) {
      if (lowLetters.indexOf(myName.charAt(index + 1)) >= 0)
            result += " ";
    }

    // Remove articles such as "a" and "an"
    if ( (result == "a ") || (result == "an ") )
      result = "";

    result += char.toLowerCase();
  }
  
  return result;
}

// Determine whether the specified class inherits from the specified object
AnObject.prototype.isKindOf = function(obj) {
  return this instanceof obj;
}

// Construct a new object as a subclass of an existing object.
// From code listed at http://www.golimojo.com/etc/js-subclass.htm
function subclass(constructor, superConstructor) {
  function surrogateConstructor() {
  }

  surrogateConstructor.prototype = superConstructor.prototype;
  var prototypeObject = new surrogateConstructor();
  prototypeObject.constructor = constructor;

  constructor.prototype = prototypeObject;
} 
