+var parseXML = function(xml) {
+ return $(xml)[0];
+};
+
+var Document = function(xml) {
+ this.loadXML(xml);
+};
+
+$.extend(Document.prototype, Backbone.Events, {
+ ElementNodeFactory: ElementNode,
+ TextNodeFactory: TextNode,
+
+ createElementNode: function(from) {
+ if(!(from instanceof HTMLElement)) {
+ from = $('<' + from.tagName + '>');
+ }
+ return new this.ElementNodeFactory(from, this);
+ },
+
+ createTextNode: function(nativeNode) {
+ return new this.TextNodeFactory(nativeNode, this);
+ },
+
+ loadXML: function(xml) {
+ defineDocumentProperties(this, $(parseXML(xml)));
+
+ this.trigger('contentSet');
+ },
+
+ toXML: function() {
+ return this.root.toXML();
+ }
+});
+
+var defineDocumentProperties = function(doc, $document) {
+ Object.defineProperty(doc, 'root', {get: function() {
+ return doc.createElementNode($document[0]);
+ }, configurable: true});
+ Object.defineProperty(doc, 'dom', {get: function() {
+ return $document[0];
+ }, configurable: true});
+};
+