 	removeClass: (className) ->
 		@classes = _.filter @classes, (item) -> item isnt className
 	
-	# @private
 	_insertElement: ->
 		
-		# If we are loaded we insert the node immediately, if not we wait
-		if document.readyState is "complete" or document.readySate is "loaded"
+		# Insert the element to the dom, wait for it if it's not loaded
+		# TODO: There might still be some bug here
+		
+		if document.readyState in ["complete"]
 			@__insertElement()
 		else
-			document.addEventListener "DOMContentLoaded", @__insertElement
-	
-	# @private
+			document.addEventListener "DOMContentLoaded", 
+				@__insertElement
+				
 	__insertElement: =>
-		document.body.appendChild @_element
+		@_rootElement.appendChild @_element
 
 
 	#############################################################################

{Frame} = require "../primitives/frame"
{Matrix} = require "../primitives/matrix"

{EventTypes} = require "../primitives/events"
{EventClass} = require "../primitives/events"

{EventEmitter} = require "../eventemitter"
{Animation} = require "../animation"

exports.ViewList = []

# Base class for all views.
#
# @example How to subclass an animal
# 	class Lion extends Animal
# 		move: (direction, speed): ->
#
class View extends Frame
	
	
	
	constructor: (args) ->
		
		args ?= {}
		
		View.Views.push @
		@id = View.Views.length
		
		@_element = document.createElement "div"
		@_element.id = @id
		
		@addClass "uilayer"
		# @addClass "textureBacked"
		# @addClass "animated"
		
		@clip = args.clip or View.Properties.clip

		@_subViews = []
		@_currentAnimations = []
		
		@properties = args
		
		if not args.superView
			@_insertElement()
		
		# Override this prototype to change all behaviour
		@_postCreate()
	
	# @private
	_postCreate: ->
	
	# Helpers
	
	# @property [String] The views name
	# 
	@define "name",
		get: ->
			@_name or @id

			return if value is @_superView
			
			# Cancel previous pending insertions
			# document.removeEventListener "DOMContentLoaded", @__insertElement
			
			# Remove from previous superview subviews
			if @_superView
