var Common = Common ? Common : {};

Common.Object = {
	
	create : function(s, p)
	{
		var t = this, sp, ns, cn, scn, c, de = 0;
	
		// Parse : <prefix> <class>:<super class>
		s = /^((static) )?([\w.]+)(:([\w.]+))?/.exec(s);
		cn = s[3].match(/(^|\.)(\w+)$/i)[2]; // Class name
	
		// Create namespace for new class
		ns = t.createNamespace(s[3].replace(/\.\w+$/, ''));
	
		// Class already exists
		if (ns[cn])
			return;
	
		// Make pure static class
		if (s[2] == 'static') {
			ns[cn] = p;
	
			if(p.init)
				p.init();
	
			return;
		}
	
		// Create default constructor
		if (!p[cn]) {
			p[cn] = function() {};
			de = 1;
		}
	
		// Add constructor and methods
		ns[cn] = p[cn];
		t.extend(ns[cn].prototype, p);
	
		// Extend
		if (s[5]) {
			sp = t.resolve(s[5]).prototype;
			scn = s[5].match(/\.(\w+)$/i)[1]; // Class name
	
			// Extend constructor
			c = ns[cn];
			if (de) {
				// Add passthrough constructor
				ns[cn] = function() {
					return sp[scn].apply(this, arguments);
				};
			} else {
				// Add inherit constructor
				ns[cn] = function() {
					this.parent = sp[scn];
					return c.apply(this, arguments);
				};
			}
			ns[cn].prototype[cn] = ns[cn];
	
			// Add super methods
			t.each(sp, function(f, n) {
				ns[cn].prototype[n] = sp[n];
			});
	
			// Add overridden methods
			t.each(p, function(f, n) {
				// Extend methods if needed
				if (sp[n]) {
					ns[cn].prototype[n] = function() {
						this.parent = sp[n];
						return f.apply(this, arguments);
					};
				} else {
					if (n != cn)
						ns[cn].prototype[n] = f;
				}
			});
		}
	
		// Add static methods
		t.each(p['static'], function(f, n) {
			ns[cn][n] = f;
		});
	},
	
	createNamespace : function(n) {
		var i, v;

		var o = window;

		n = n.split('.');
		
		for (i=0; i<n.length; i++)
		{
			v = n[i];

			if (!o[v])
				o[v] = {};
			
			o = o[v];
		}

		return o;
	},
	
	extend : function(o, e) {
		var i, a = arguments;

		for (i=1; i<a.length; i++) {
			e = a[i];

			Common.Object.each(e, function(v, n) {
				if (typeof(v) !== 'undefined')
					o[n] = v;
			});
		}

		return o;
	},
	
	resolve : function(n, o) {
		var i, l;

		o = o || window;

		n = n.split('.');
		for (i=0, l = n.length; i<l; i++) {
			o = o[n[i]];

			if (!o)
				break;
		}

		return o;
	},
	
	each : function(o, cb, s) {
		var n, l;

		if (!o)
			return 0;

		s = s || o;

		if (typeof(o.length) != 'undefined') {
			// Indexed arrays, needed for Safari
			for (n=0, l = o.length; n<l; n++) {
				if (cb.call(s, o[n], n, o) === false)
					return 0;
			}
		} else {
			// Hashtables
			for (n in o) {
				if (o.hasOwnProperty(n)) {
					if (cb.call(s, o[n], n, o) === false)
						return 0;
				}
			}
		}

		return 1;
	}
};
