 # Application script
 application = ->
   # application object
-  app = window.app = (event) ->
-    type = event.type
-    id = if event.target?.id then event.target.id else ''
-    unless app[id]
-      app[id] = event.target
-      app[id]._sync = true
-      app[id]._remote = {}
-    sender = app[id]
+  app = window.app = (event, selector) ->
     window.event = event
-    return sender._remote[type]() if (typeof sender._remote[type] is 'function') and (sender._sync isnt true)
-
+    app._url = "#{location.pathname}?selector=#{selector}&_id=event&_key="
+    return app._selector[selector]() if typeof app._selector[selector] is 'function'
     # getting event script
-    app._callback = null
-    $.getScript location.pathname + 
-      '?_id=EVENT' +
-      '&_key=' +
-      '&id=' + id +
-      '&type=' + type
-    .done ->
-      sender._sync = app._sync
-      sender._remote[type] = app._callback
-      sender._remote[type]() if typeof sender._remote[type] is 'function'
+    $.getScript app._url + 'script'
+    .done -> app._selector[selector]() if typeof app._selector[selector] is 'function'
 
   # application events
-  app.click = -> app _event
-  app.keypress = -> app _event
+  app._selector = {}
+  app.selector = (selector) -> app _event, selector
+
+  # core data
+  app.core = {
+    # base data carrier and catcher
+    base: (model) ->
+      # get value from dom element
+      get = (elem, name) ->
+        switch name.toString()
+          when 'text' then value = elem.text()
+          when 'html' then value = elem.html()
+          when 'value' then value = elem.val()
+          else value = elem.attr name
+        return value
+      # user data
+      data = {}
+      for id, attrs of model
+        elem = $('#' + id)
+        data[id] = {}
+        unless $.isArray attrs then data[id] = get elem, attrs
+        else data[id][name] = get elem, name for name in attrs
+      # set data
+      carrier = (data) ->
+        # set value to dom element
+        set = (elem, name, value) ->
+          switch name.toString()
+            when 'text' then elem.text value
+            when 'html' then elem.html value
+            when 'value' then elem.val value
+            else elem.attr name, value
+        # user data
+        for id, attrs of model
+          elem = $('#' + id)
+          continue unless data[id]
+          unless $.isArray attrs then set elem, attrs, data[id]
+          else set elem, name, data[id][name] for name in attrs
+      # post data
+      $.post app._url + 'data', (JSON.stringify data), carrier, 'json'
+  }
 
   # getting user event
   window.constructor::__defineGetter__ '_event', ->

      throw new TypeError typeof(callback) + ' is not a function'
    @_error = callback

  # Sync client callback
  sync: -> @_sync = true

  # User event script
  script: (callback, argv) ->
    if typeof callback isnt 'function'
      throw new TypeError typeof(callback) + ' is not a function'
    @_callback += callback.script argv

  # Respond client
  _do: (req, @_buffer) ->

    urls = url.parse req.url
    @query = querystring.parse urls.query
    @query.raw = urls.query
    # response buffer
    @_buffer.removeAllListeners 'error'
    @_buffer.on 'error', (err) => @_error? err
    # default content
    @writeHead 200, "Content-Type": "text/plain;charset=utf-8"
    return @render() if !@query.raw or @query.raw.length < 1
    # application content
    return @end application.script() if @query.raw is 'script'
    # post data
    raw = ''
    req.on 'data', (chunk) -> raw += chunk
    req.on 'end', =>
      @data = @parse raw if raw.length > 0
      switch @query._id
        when 'event'
          selector = @query.selector
          @[selector]() if typeof @[selector] is 'function'
          @end()
