function _clone( obj )
{
	var o = (obj instanceof Array) ? [] : {};
	
	for( var i in obj )
	{
		if( !obj.hasOwnProperty( i ) )
			continue;
		
		if( obj[i] && typeof( obj[i] ) == 'object' )
			o[i] = _clone( obj[i] );
		else
			o[i] = obj[i];
	}
	
	return o;
};

function _debugObj( obj, args )
{
	var r = [],
		t = '',
		c = 0,
		x;
	
	var defaults = {
		max: 3,
		tab: 0,
		count: 0,
		functions: true,
		objects: true
	}
	
	if( !args )
		args = {};
	
	for( x in defaults )
	{
		if( !defaults.hasOwnProperty( x ) )
			continue;
			
		if( typeof( args[x] ) == 'undefined' )
			args[x] = defaults[x];
	}

	if( args['count'] > args['max'] )
		return obj.toString();

	if( args['tab'] > 0 )
		for( var i = 1; i <= args['tab']; i++ )
			t += '\t';

	try
	{
		for( x in obj )
		{
			if( !obj.hasOwnProperty( x ) )
				continue;
			c++;
			
			if( typeof( obj[x] ) == 'function' && !args['functions'] )
				continue;
			
			if( typeof( obj[x] ) == 'object' && !args['objects'] )
				continue;
				
			r.push( t + x );
			if( obj[x] != null )
			{
				r.push( ': ' );
				
				if( typeof( obj[x] ) == 'function' )
				{
					var f = obj[x].toString().match( /function ([^\{]+)\s?\{([^\}]*?)\}/ );
					r.push( 'function ' + f[1] + ' { ... }\n' );
					continue;
				}
				
				if( typeof( obj[x] ) == 'object' )
				{
					try
					{
						var a = _copy( args );
						a.tab++;
						a.count++;
						var o = _debugObj( obj[x], a );
						r.push( o.replace( '\\', '|' ).replace( '\n', '\n' + t ) ); //escape( o ); //o.replace( '\n', '\n' + t );
					}
					catch( e )
					{
						r.push( '[Error expanding: + ' + e + ']' );
					}
					r.push( '\n' );
					continue;
				}			
				r.push( obj[x].toString() + '\n' );
			}
			else
			{
				r.push( ': (null)\n' );
			}
		}
	}
	catch (e)
	{
		r.push( t + '[Error expanding: ' + e + ']\n' );
		//throw new Error( e );
	}

	r.unshift( typeof( obj ) + ' (' + c + ') {\n' );
	r.push( '}\n' );
	return r.join( '' );
}

function debugObj( obj, args )
{
	var win = window.open('', '', 'width=400,height=600,scrollbars=yes');
	
	win.document.write( '<html><head><title>Properties</title></head><body><pre>' );
	win.document.write( 'Loading...' );
	try
	{
		win.document.write( '\n--------------------\n\n' + _debugObj( obj, args ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' ) );
	}
	catch (e)
	{
		win.document.write( '\n\nFailed:\n' + e );
		alert( e );
	}
	win.document.write( '</pre></body></html>' );
}
