module Gravatar
  module FormHelpers
  
    def gravatar_image_tag(options = {})
      raise "Please specify gravatar options." unless options[:gravatar]

      source, options = process_gravatar_options(options)

      image_tag(source, options)
    end

    #secure, email, size, default_image, rating
    def process_gravatar_options(opts)
      host = GravatarExtensions.url(opts[:gravatar][:secure] ||= false)
      hash = GravatarExtensions.account_key(opts[:gravatar][:email])

      url_params = []
      ["size", "default_image", "rating"].each do |k|
        key = GravatarExtensions.gravatar_param_key(k)
        value = opts[:gravatar][k.to_sym] ||= GravatarExtensions.gravatar_default_value(k)
        url_params << "#{key}=#{value}" 
      end

      url = []
      url << host
      url << hash
      url << url_params.join("/")

      return url.join("/"), opts.delete(:gravatar)
    end

  end
end