/**
 * jsClass
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Licensed under BSD (LICENSE.txt)
 * Date: 6/10/2008
 * @projectDescription Light and extensible Base Class for Javascript.
 * @version 1.0.0 RC1
 * @author Ariel Flesler
 */

function Class( extensions, ctor ){
	if( this instanceof Class )// calling new Class();
		return this;
	
	var data = {
		c : ctor, // Constructor
		m : new Class, // Members, all classes really inherit from Class
		s : Class.copy( { }, Class.staticsList ), // Statics
		a : Class.after.concat() // After (callbacks)
	};
		
	if( !ctor )
		data.c = extensions;
	else
		for( var name in extensions )
			if( Class._exts_[name] )
				Class._exts_[name]( data, extensions[name] );
	
	data.m.clazz = data.c;
	data.c.prototype = data.m;
	Class.copy( data.c, data.s );
	
	var i = 0;
	while( data.a[i] )
		data.a[i++]( data );
	
	return data.c;
};

/**
 * Copies the properties from any amount of objects to a source object
 * @param {Object, Function} src Source object that will receive all the attributes.
 * @param {Object} ... The objects from where to take the attributes.
 * @returns {Object, Function} the src element.
 */
Class.copy = function( src ){
	for( var i = 1, l = arguments.length, obj; i < l; )
		if( obj = arguments[i++] )
			for( var attr in obj )
				src[attr] = obj[attr];
	return src;
};

Class.copy( Class, {
	// multi-use getter/setter
	generic:function( obj, key, value ){
		if( value !== undefined )
			obj[key] = value;
		if( typeof key == 'string' )
			return obj[key];
		if( !key )
			return obj;
		Class.copy( obj, key );
	},
	// allows addition of new extension for class creating
	extensions:function( key, value ){
		return Class.generic( Class._exts_, key, value );
	},
	// create a new instance from a function or a prototype, without actually calling the constructor
	instantiate:function( fn ){
		var f = function(){};
		f.prototype = fn.prototype || fn;
		return new f;
	},
	// all the items found in this map, are copied to every class
	staticsList:{
		members:function( key, value ){
			return Class.generic( this.prototype, key, value );
		},
		statics:function( key, value ){
			return Class.generic( this, key, value );
		}
	},
	// callback functions can be added to this array, and will be called for each created class
	after:[ ],
	// internal
	_exts_:{ },
	// all the items found in this map, can be used by any instance.
	prototype:{ }
});

Class(Class); // Class is a class too.