module CalendarHelper

  def today_date(period)
    if Date.today > period.date_from && Date.today < period.date_to
      return Date.today
    else
      return period.date_from
    end
  end

  def calendar_field_tag(object, date, html_options = {}, calendar_options = {})
    _calendar(object, nil, date, html_options, calendar_options)
  end

  def calendar_field(object, method, html_options = {}, calendar_options = {})

    _calendar(object, method, nil, html_options, calendar_options)
  end

  private

  def _calendar(object, method, date, html_options = {}, calendar_options = {})
    html_options[:size] = 12 unless html_options[:size]

    if html_options[:readonly] == false
      html_options[:readonly] = false
    else
      html_options[:readonly] = 'true'
    end
    button_image = html_options[:button_image] || 'calendar.gif'
    field_options = html_options.dup
    if method
      input_field_id = "#{object}_#{method}"
      calendar_id = "#{object}_#{method}_calendar"
      calendar_options[:showTime] = true
      calendar_options[:dateFormat] = "%d.%m.%Y %H:%M"

      field = text_field(object, method, field_options)
    else
      input_field_id = "#{object}"
      calendar_id = "#{object}_calendar"
      if !date.blank?

  if calendar_options[:with_time]
    date = date.strftime("%d.%m.%Y %H:%M")
    calendar_options[:showTime] = true
    calendar_options[:dateFormat] = "%d.%m.%Y %H:%M"
  else
    date = Russian::strftime(date.to_date)
    calendar_options[:dateFormat] = "%d.%m.%Y"
  end
      else
  calendar_options[:dateFormat] = "%d.%m.%Y"
      end
      field = text_field_tag(object, date, field_options)
    end
      button_options = html_options.dup
      add_mandatories(button_options, :id => calendar_id)
      rename_option(button_options, :button_title, :title)
      remove_option(button_options, :field_title)
      calendar = image_tag(button_image, button_options)


    calendar_setup = calendar_options.dup
    add_mandatories(calendar_setup,
        :inputField => input_field_id,
        :trigger => calendar_id
    )
    if html_options[:change_materials]
      func = "ChangeMaterials();"
    else
      func = ""
    end


<<END
#{field}
#{calendar}
<script type="text/javascript">
      var cal = Calendar.setup({ #{format_js_hash(calendar_setup)},
          onSelect: function() {this.hide(); #{func}}
      });
</script>
END
  end

  def value(object_name, method_name)
    if object = self.instance_variable_get("@#{object_name}")
      object.send(method_name)
    else
      nil
    end
  end

  def add_mandatories(options, mandatories)
    options.merge!(mandatories)
  end

  def add_defaults(options, defaults)
    options.merge!(defaults) { |key, old_val, new_val| old_val }
  end

  def remove_option(options, key)
    options.delete(key)
  end

  def rename_option(options, old_key, new_key)
    if options.has_key?(old_key)
      options[new_key] = options.delete(old_key)
    end
    options
  end

  def format_js_hash(options)
    options.collect { |key,value| key.to_s + ':' + value.inspect }.join(',')
  end

end
