/*****************************

Initialize possibility to create java-style class inheritance in javascript.
Parts from http://www.crockford.com/

*****************************/
Function.prototype.inherits = function(parentName)
{
	if((typeof parentName) != 'string')
	{
		write('The attribute to the method \'inherits\' must be the *string* name of the class to inherit.\n\nMyObject.inherits(\'MyBaseObject\');');
		return false;
	}
	else
	{
		var regex = /function\s+(\w+)[\(\s]*/;
		var className = this.toString().match(regex)[1];
		this.className = className;
		var self = this;
	
		var parent = eval(parentName);
//					var d = [];
//					var p = (this.prototype = new parent());
		var d = [], p = (this.prototype = new parent());
		this.prototype[parentName] = parent;
		this.prototype.pconstructor = parent;
	
		//alert(className + ' extends ' + parentName);	

		this.prototype.base = function base(name) {
			if(d[name] == undefined)
			{
				d[name] = 0;
			}
			
			var f, r, t = d[name], v = parent.prototype;

			while (t) {
				v = v.pconstructor.prototype;
				t--;
			}
			f = v[name];
			d[name]++;
			if(!f)
			{
				alert('Method \''+name+'\' does not exist for object '+className);
			}
			else
			{
				r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
			}
			d[name]--;
			return r;
		}
	
		return this;
	}
}

