
Object.extend(Class, {
	create: function (proto)
   {
		var inner = function ()
		{
			if (this.initialize && arguments.callee.caller != Class.extend)
			{
				this.__class__ = arguments.callee.prototype;
				Object.extend(this, Class.Methods);
				this.initialize.apply(this, arguments);
			}
		};
		inner.prototype = proto || {};
		inner.extend = Class.extend;
		return inner;
	},
	extend: function (subobj)   // implementation of inheritance
	{
		var subproto = new this;
		Object.extend(subproto, subobj);
		subproto.__super__ = this.prototype;
		return Class.create(subproto);
	},
	getMethod: function (aClass, args)  // obtain method name being called
	{
		var c = args.callee.caller;
		for (var method in aClass)
			if (aClass[method] == c)
				return method;
		return null;
	},
	callSuper: function (superclass, self, method, args)
	{
		if (superclass && superclass[method])
		{
			var __class__  = self.__class__;
			self.__class__ = superclass;
			self.__super__ = superclass.__super__;
	
			try
			{
				superclass[method].apply(self, args);
			}
			finally
			{
				// Restore values
				self.__class__ = __class__;
				self.__super__ = superclass;
			}
		}
	}
});

Class.Methods =
{
    extend: function ()
    {
        var i = arguments.length;
        while (--i >= 0)
            Object.extend(this, arguments[i]);
        return this;
    },
    SUPER: function ()
    {
        var method = Class.getMethod(this.__class__, arguments);
        Class.callSuper(this.__super__, this, method, arguments);
    }
};

