 SaveAll = {
   replaceSnippets: (html) ->
     obj = $('<div>' + html + '</div>')
-    obj.find('div[data-snippet]').each -> $(this).replaceWith("[#{$(this).data('snippet')}]")
+    obj.find('div[data-snippet-data]').each -> $(this).replaceWith("[#{$(this).data('snippet')}]")
     obj.html()
+
+  instanceData: (instance) ->
+    console.log 'INSTANCE DATA'
+
+    snippets = {}
+
+    for id, widget of instance.widgets.instances
+      console.log widget
+      snippet_id = $(widget.wrapper.$).data('cke-widget-id')
+      snippets["snippet_#{snippet_id}"] = widget.data
+      snippets["snippet_#{snippet_id}"]['class_name'] = widget.name
+
+      snippet_data = $(widget.element.$).data('snippet-data')
+      snippet_data['id'] = snippet_id
+      $(widget.element.$).data('snippet-data', snippet_data)
+
+      console.log "WIDGET HTML"
+      console.log $(widget.element.$).data('snippet-data')
+
+    content = instance.getData()
+
+    console.log "CONTENT:"
+    console.log content
+
+    {
+      content: instance.getData(),
+      snippets: snippets
+    }
 
   exec: (editor) ->
     data = {}
-
-    for name, instance of CKEDITOR.instances # All CKEditors on the whole page
-      snippets_data = {}
-      for name, widget of instance.widgets.instances
-        console.log name
-        console.log widget
-        console.log widget.data
-        snippets_data[widget.snippet_id()] = widget.data 
-
-      data[name] = {
-        content: @replaceSnippets(instance.getData())
-        snippets: snippets_data
-      }
+    data[name] = @instanceData(instance) for name, instance of CKEDITOR.instances
 
     url = window.location.protocol + '//' + window.location.host + '/edit' + window.location.pathname
+
+    console.log "Saving..."
+    console.log data
 
     $.ajax
       url: url

      dataType: 'json'
      data: { effective_regions: data }
      async: false
      success: (data) -> 
        console.log 'success!'
        console.log data
}

Regions = {

  build: (editor, name, values) ->
    snippet = {}

    snippet['configured'] = false
    snippet['template'] = "<div></div>" # This gets replaced when we click on the Submit Dialog button
    snippet['dialog_url'] = values.dialog_url
    snippet['dialog'] = name if values.dialog_url
    snippet['requiredContent'] = "div(#{name})"
