 #
 #
 
-WiceGridProcessor = (name, base_request_for_filter, base_link_for_show_all_records, link_for_export, parameter_name_for_query_loading, parameter_name_for_focus, environment) ->
-
-  this.checkIfJsFrameworkIsLoaded =  ->
+class WiceGridProcessor
+  constructor: (@name, @base_request_for_filter, @base_link_for_show_all_records, @link_for_export, @parameter_name_for_query_loading, @parameter_name_for_focus, @environment) ->
+    @filter_declarations = new Array();
+    @checkIfJsFrameworkIsLoaded()
+
+  checkIfJsFrameworkIsLoaded :  ->
     if ! jQuery
       alert "jQuery not loaded, WiceGrid cannot proceed!"
 
-
-  this.checkIfJsFrameworkIsLoaded()
-  this.name = name
-  this.parameter_name_for_query_loading = parameter_name_for_query_loading
-  this.parameter_name_for_focus = parameter_name_for_focus
-  this.base_request_for_filter = base_request_for_filter
-  this.base_link_for_show_all_records = base_link_for_show_all_records
-  this.link_for_export = link_for_export
-  this.filter_declarations = new Array()
-  this.environment = environment
-
-  this.toString =  ->
-    "<WiceGridProcessor instance for grid '" + this.name + "'>"
-
-
-  this.process = (dom_id_to_focus)->
-    window.location = this.build_url_with_params(dom_id_to_focus)
-
-
-  this.set_process_timer = (dom_id_to_focus)->
-
-    if this.timer
-      clearTimeout(this.timer)
-      this.timer = null
+  toString :  ->
+    "<WiceGridProcessor instance for grid '" + @name + "'>"
+
+
+  process : (dom_id_to_focus)->
+    window.location = @build_url_with_params(dom_id_to_focus)
+
+
+  set_process_timer : (dom_id_to_focus)->
+
+    if @timer
+      clearTimeout(@timer)
+      @timer = null
 
     processor = this
 
-    this.timer = setTimeout(
+    @timer = setTimeout(
       -> processor.process(dom_id_to_focus)
       1000
     )
 
-  this.reload_page_for_given_grid_state = (grid_state)->
-    request_path = this.grid_state_to_request(grid_state)
-    window.location = this.append_to_url(this.base_link_for_show_all_records, request_path)
-
-
-  this.load_query = (query_id)->
-    request = this.append_to_url(
-      this.build_url_with_params()
-      this.parameter_name_for_query_loading +  encodeURIComponent(query_id)
+  reload_page_for_given_grid_state : (grid_state)->
+    request_path = @grid_state_to_request(grid_state)
+    window.location = @append_to_url(@base_link_for_show_all_records, request_path)
+
+
+  load_query : (query_id)->
+    request = @append_to_url(
+      @build_url_with_params()
+      @parameter_name_for_query_loading +  encodeURIComponent(query_id)
     )
 
     window.location = request
 
-  this.save_query = (field_id, query_name, base_path_to_query_controller, grid_state, input_ids)->
+  save_query : (field_id, query_name, base_path_to_query_controller, grid_state, input_ids)->
     if input_ids instanceof Array
       input_ids.each (dom_id) ->
         grid_state.push(['extra[' + dom_id + ']', $('#'+ dom_id)[0].value])
 
 
-    request_path = this.grid_state_to_request(grid_state)
+    request_path = @grid_state_to_request(grid_state)
 
     jQuery.ajax
       url: base_path_to_query_controller

          results.push(param)
    )

    res = this.base_request_for_filter
    if  results.length != 0
      all_filter_params = results.join('&')
      res = this.append_to_url(res, all_filter_params)

    if dom_id_to_focus
      res = this.append_to_url(res, this.parameter_name_for_focus + dom_id_to_focus)

    res



  this.reset = ->
    window.location = this.base_request_for_filter


  this.export_to_csv = ->
    window.location = this.link_for_export


  this.register = (func)->
    this.filter_declarations.push(func)


  this.read_values_and_form_query_string = (filter_name, detached, templates, ids)->
    res = new Array()

    for i in [0 .. templates.length-1]

      success:  -> $('#' + field_id).val('')
      type: 'POST'

  grid_state_to_request : (grid_state)->
    jQuery.map(
      grid_state
      (pair) -> encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1])
    ).join('&')


  append_to_url : (url, str)->

    sep = if url.indexOf('?') != -1
      if /[&\?]$/.exec(url)
